Skip to content

Commit

Permalink
Script mode set -xe by default when no shebang used
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Scott authored and tekton-robot committed Dec 12, 2019
1 parent 24c2131 commit 3c973fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
15 changes: 10 additions & 5 deletions docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down
8 changes: 4 additions & 4 deletions pkg/pod/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/pod/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`},
Expand Down

0 comments on commit 3c973fc

Please sign in to comment.