Skip to content

Conversation

Queen-esther01
Copy link

This pull request contains example additions to the sh step. I added a few use cases in which shell script can be used in pipelines and escaped or recognized in groovy interpolation.

Issue link on Jira https://issues.jenkins.io/browse/JENKINS-65396?filter=-1

@MarkEWaite

<b>Escaping script content from groovy interpretation </b>>
<p>The triple-double-quote (""") string literal syntax allows for variable/expression substitution (interpolation), so the backslash (\) is interpreted as a special character "escape".</p>
<p>Since the first open parentheses is not such a special character, Groovy compilation fails. If your intent is to have literal backslashes in the resulting string, you need to escape the backslashes. That is, use a double-backslash (\\) to substitute for one literal backslash</p>
<p><code>sh ("""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that we want to document scripted pipeline, any other opinions for other reviewers ? I would prefer to only have declarative pipeline in help.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be helpful to a few people, i specifically added it because of feedback from a user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a mix of scripted and declarative is most helpful to readers, with preference to use declarative when we can

…e_task/ShellStep/help-script.html

Co-authored-by: A. Jard <[email protected]>
Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First comments.

Queen-esther01 and others added 2 commits May 3, 2021 19:10
…e_task/ShellStep/help-script.html

Co-authored-by: Mark Waite <[email protected]>
…e_task/ShellStep/help-script.html

Co-authored-by: Mark Waite <[email protected]>
Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible additional changes

Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments for consideration

'''
</pre>
</p>
<b>Escaping script content from groovy interpolation </b>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra trailing >

Suggested change
<b>Escaping script content from groovy interpolation </b>>
<b>Escaping script content from groovy interpolation </b>

<b>Escaping script content from groovy interpretation </b>>
<p>The triple-double-quote (""") string literal syntax allows for variable/expression substitution (interpolation), so the backslash (\) is interpreted as a special character "escape".</p>
<p>Since the first open parentheses is not such a special character, Groovy compilation fails. If your intent is to have literal backslashes in the resulting string, you need to escape the backslashes. That is, use a double-backslash (\\) to substitute for one literal backslash</p>
<p><code>sh ("""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a mix of scripted and declarative is most helpful to readers, with preference to use declarative when we can

Comment on lines +43 to +45
sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")</code>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternate example that shows a multi-line shell script using triple double quotes.

Suggested change
sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")</code>
sh """
echo 'This text is single quoted'
echo "This text is double quoted"
echo "This text doesn't contain a double quote"
echo This text doesn\\'t include an embedded double quote, it has an embedded single quote
"""
</code>

Comment on lines +40 to +41
<p>The triple-double-quote (""") string literal syntax allows for variable/expression substitution (interpolation), so the backslash (\) is interpreted as a special character "escape".</p>
<p>Since the first open parentheses is not such a special character, Groovy compilation fails. If your intent is to have literal backslashes in the resulting string, you need to escape the backslashes. That is, use a double-backslash (\\) to substitute for one literal backslash</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to use a simpler example.

Alternate example attached for you to consider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants