From e3564eeebea947680159f538aa92bef8dd814296 Mon Sep 17 00:00:00 2001 From: Scott Date: Wed, 11 Dec 2019 11:12:51 -0500 Subject: [PATCH] Script mode set -xe by default when no shebang used Fixes #1735 When I use script mode without a #! the current behaviour is to insert one for me - `#!/bin/sh`. This is helpful and removes an unneccessary line from my script. In addition I'd like two things by default: First I want the script to error out if any of the commands error out. Second I'd like to see the commands the script runs before their output. These two features are baked into `sh` with the `set -xe` flags. After this change the default behaviour of script mode when no shebang is provided is to insert both the default shebang as well as the flags required to make shell errors and shell echos happen. --- docs/tasks.md | 15 ++++++++++----- pkg/pod/script.go | 8 ++++---- pkg/pod/script_test.go | 1 + 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/tasks.md b/docs/tasks.md index 4d561b70fa5..863521760c6 100644 --- a/docs/tasks.md +++ b/docs/tasks.md @@ -156,11 +156,16 @@ If this field is present, the step cannot specify `command`. When specified, a `script` gets invoked as if it were the contents of a file in the container. Any `args` are passed to the script file. -Scripts that do not start with a shebang -[shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) line will use a default -value of `#!/bin/sh`, although users can override this by starting their script -with a shebang to declare what tool should be used to interpret the script. -That tool must then also be available within the step's container. +Scripts that do not start with a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) +line will use the following default preamble: + +```bash +#!/bin/sh +set -xe +``` +Users can override this by starting their script with a shebang to declare what +tool should be used to interpret the script. That tool must then also be +available within the step's container. This allows you to execute a Bash script, if the image includes `bash`: diff --git a/pkg/pod/script.go b/pkg/pod/script.go index 27ae7d10104..492a8e5e8a8 100644 --- a/pkg/pod/script.go +++ b/pkg/pod/script.go @@ -27,9 +27,9 @@ import ( ) const ( - scriptsVolumeName = "scripts" - scriptsDir = "/tekton/scripts" - defaultShebang = "#!/bin/sh\n" + scriptsVolumeName = "scripts" + scriptsDir = "/tekton/scripts" + defaultScriptPreamble = "#!/bin/sh\nset -xe\n" ) var ( @@ -77,7 +77,7 @@ func convertScripts(shellImage string, steps []v1alpha1.Step) (*corev1.Container script := s.Script if !hasShebang { - script = defaultShebang + s.Script + script = defaultScriptPreamble + s.Script } // At least one step uses a script, so we should return a diff --git a/pkg/pod/script_test.go b/pkg/pod/script_test.go index 3c16e75b0a3..cd2ac86583d 100644 --- a/pkg/pod/script_test.go +++ b/pkg/pod/script_test.go @@ -101,6 +101,7 @@ tmpfile="/tekton/scripts/script-3-j2tds" touch ${tmpfile} && chmod +x ${tmpfile} cat > ${tmpfile} << 'script-heredoc-randomly-generated-vr6ds' #!/bin/sh +set -xe no-shebang script-heredoc-randomly-generated-vr6ds `},