-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvement: Add Examples To sh Step [JENKINS-65396] #154
base: master
Are you sure you want to change the base?
Changes from all commits
021ec66
1e9657f
6cf1fce
1cf016f
0f1f9a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,4 +9,46 @@ | |||||||||||||||||||||
Otherwise the system default shell will be run, using the <code>-xe</code> flags | ||||||||||||||||||||||
(you can specify <code>set +e</code> and/or <code>set +x</code> to disable those). | ||||||||||||||||||||||
</p> | ||||||||||||||||||||||
</div> | ||||||||||||||||||||||
<p>A few examples of usage include:</p> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<b>Creating an output folder</b> | ||||||||||||||||||||||
<p><code>sh 'mkdir -p output'</code></p> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<b>Reporting the current directory of the Pipeline step.</b> | ||||||||||||||||||||||
<p><code>sh 'pwd'</code></p> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<b>Making HTTP requests with curl (scripted Pipeline)</b> | ||||||||||||||||||||||
<p><pre> | ||||||||||||||||||||||
<code>def payload='payload to send' | ||||||||||||||||||||||
def slackURL='https://example.slack.com/archives/CCQU3EHYP' | ||||||||||||||||||||||
sh "curl -X POST --data-urlencode \"payload=${payload}\" ${slackURL}" | ||||||||||||||||||||||
</code></pre></p> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<b>Running tests in the same workspace that the project was built </b> | ||||||||||||||||||||||
<p><code>sh 'mvn test'</code></p> | ||||||||||||||||||||||
<b>Using multi-line syntax to run several lines of script:</b> | ||||||||||||||||||||||
<p> | ||||||||||||||||||||||
<pre> | ||||||||||||||||||||||
sh ''' | ||||||||||||||||||||||
echo "This is a first line of script" | ||||||||||||||||||||||
# This is a script comment | ||||||||||||||||||||||
echo "This is a second line" | ||||||||||||||||||||||
''' | ||||||||||||||||||||||
</pre> | ||||||||||||||||||||||
</p> | ||||||||||||||||||||||
<b>Escaping script content from groovy interpolation </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> | ||||||||||||||||||||||
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||||||
<p><code>sh (""" | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||
sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g" | ||||||||||||||||||||||
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r | ||||||||||||||||||||||
""")</code> | ||||||||||||||||||||||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||
</p> | ||||||||||||||||||||||
|
||||||||||||||||||||||
<b>Adding shell script as part of Groovy interpolation </b>> | ||||||||||||||||||||||
<p>For double quoted string, Groovy will do interpolation on the string firstly.</p> | ||||||||||||||||||||||
<p>Because the variables are runtime variables of the shell, rather than the variables of Groovy runtime. Groovy can't find the responding value from Groovy variable stack to replace the variables during interpolation.</p> | ||||||||||||||||||||||
<p>So you need to escape all $ if you use double quotes or simply use single quotes which does not support interpolation</p> | ||||||||||||||||||||||
<p><code>sh(returnStdout: true, script: "cd \$it; PLAN=\$(terragrunt plan --terragrunt-source-update | landscape); | ||||||||||||||||||||||
echo \$PLAN; CHANGES=\$(echo \$PLAN | tail -2); echo \$CHANGES"</code></p> | ||||||||||||||||||||||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra trailing
>