From 021ec664d4124b47d1c6a9e536907396d7e9375c Mon Sep 17 00:00:00 2001 From: ejidike esther <ejidikeesther@gmail.com> Date: Mon, 19 Apr 2021 18:03:40 +0100 Subject: [PATCH 1/5] add examples to sh step --- .../durable_task/ShellStep/help-script.html | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html index 908c2653..e5a67ef3 100644 --- a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html +++ b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html @@ -9,4 +9,33 @@ 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> + <p>A few examples of usage include:</p> + + <b>Creating an output folder</b> + <p><code>sh "mkdir -p output"</code></p> + + <b>Returning the current directory Pipeline is running in.</b> + <p><code>sh "ls -la ${pwd()}"</code></p> + + <b>Making HTTP requests </b> + <p><code>sh "curl -X POST --data-urlencode \'payload=${payload}\' ${slackURL}"</code></p> + + <b>Running tests in the same workspace that the project was built </b> + <p><code>sh 'mvn test'</code></p> + + <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 (""" + sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g" + AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r + """)</code> + </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> \ No newline at end of file From 1e9657fd4a8ed209bb7a06bf53817ef856f5b503 Mon Sep 17 00:00:00 2001 From: Ejidike Esther <ejidikeesther@gmail.com> Date: Mon, 26 Apr 2021 18:28:35 +0100 Subject: [PATCH 2/5] Update src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html Co-authored-by: A. Jard <angelique.jard@gmail.com> --- .../steps/durable_task/ShellStep/help-script.html | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html index e5a67ef3..d605c1b5 100644 --- a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html +++ b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html @@ -22,7 +22,16 @@ <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 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> @@ -38,4 +47,4 @@ <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> \ No newline at end of file +</div> From 6cf1fce431d57b9d52f08ee08adfeab9c89fb38f Mon Sep 17 00:00:00 2001 From: Ejidike Esther <ejidikeesther@gmail.com> Date: Mon, 3 May 2021 19:10:42 +0100 Subject: [PATCH 3/5] Update src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html Co-authored-by: Mark Waite <mark.earl.waite@gmail.com> --- .../workflow/steps/durable_task/ShellStep/help-script.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html index d605c1b5..389114fb 100644 --- a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html +++ b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html @@ -14,8 +14,8 @@ <b>Creating an output folder</b> <p><code>sh "mkdir -p output"</code></p> - <b>Returning the current directory Pipeline is running in.</b> - <p><code>sh "ls -la ${pwd()}"</code></p> + <b>Reporting the current directory of the Pipeline step.</b> + <p><code>sh 'pwd'</code></p> <b>Making HTTP requests </b> <p><code>sh "curl -X POST --data-urlencode \'payload=${payload}\' ${slackURL}"</code></p> From 1cf016fe6aea48bbc6073777cec21b73a7923a5d Mon Sep 17 00:00:00 2001 From: Ejidike Esther <ejidikeesther@gmail.com> Date: Mon, 3 May 2021 19:11:02 +0100 Subject: [PATCH 4/5] Update src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html Co-authored-by: Mark Waite <mark.earl.waite@gmail.com> --- .../workflow/steps/durable_task/ShellStep/help-script.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html index 389114fb..990b19f2 100644 --- a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html +++ b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html @@ -12,7 +12,7 @@ <p>A few examples of usage include:</p> <b>Creating an output folder</b> - <p><code>sh "mkdir -p output"</code></p> + <p><code>sh 'mkdir -p output'</code></p> <b>Reporting the current directory of the Pipeline step.</b> <p><code>sh 'pwd'</code></p> From 0f1f9a2f6c6e606f21eb1582865200a2d429c782 Mon Sep 17 00:00:00 2001 From: Ejidike Esther <ejidikeesther@gmail.com> Date: Mon, 3 May 2021 19:26:49 +0100 Subject: [PATCH 5/5] Applying Mark's suggestions from code review Co-authored-by: Mark Waite <mark.earl.waite@gmail.com> --- .../steps/durable_task/ShellStep/help-script.html | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html index 990b19f2..d75fc8f1 100644 --- a/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html +++ b/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/ShellStep/help-script.html @@ -17,8 +17,12 @@ <b>Reporting the current directory of the Pipeline step.</b> <p><code>sh 'pwd'</code></p> - <b>Making HTTP requests </b> - <p><code>sh "curl -X POST --data-urlencode \'payload=${payload}\' ${slackURL}"</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> @@ -32,7 +36,7 @@ ''' </pre> </p> - <b>Escaping script content from groovy interpretation </b>> + <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> <p><code>sh ("""