Skip to content
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

Script mode set -xe by default when no shebang used #1736

Merged
merged 1 commit into from Dec 12, 2019
Merged

Script mode set -xe by default when no shebang used #1736

merged 1 commit into from Dec 12, 2019

Conversation

ghost
Copy link

@ghost ghost commented Dec 11, 2019

Changes

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.

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

Reviewer Notes

If API changes are included, additive changes must be approved by at least two OWNERS and backwards incompatible changes must be approved by more than 50% of the OWNERS, and they must first be added in a backwards compatible way.

Release Notes

Script mode will now insert `set -xe` to scripts that don't include a shebang #!, which will in turn fail scripts with commands that error our and also print the command that is about to run. This default behaviour can be disabled by simply specifying your own #! at the top of the script.

@googlebot googlebot added the cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit label Dec 11, 2019
@tekton-robot tekton-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Dec 11, 2019
@ghost ghost requested review from dlorenc and imjasonh and removed request for afrittoli December 11, 2019 16:19
@imjasonh
Copy link
Member

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.
@tekton-robot tekton-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 11, 2019
@ghost
Copy link
Author

ghost commented Dec 11, 2019

Can you document this behavior at https://github.com/tektoncd/pipeline/blob/master/docs/tasks.md#step-script too?

Good call, done!

Copy link
Member

@imjasonh imjasonh left a comment

Choose a reason for hiding this comment

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

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Dec 11, 2019
@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ImJasonH

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 11, 2019
@ghost
Copy link
Author

ghost commented Dec 11, 2019

I1211 16:24:52.372] ----------------------------------------------
I1211 16:24:52.372] ---- Checking links in the markdown files ----
I1211 16:24:52.373] ----------------------------------------------
I1211 16:24:54.221] docs/tasks.md
I1211 16:24:54.221] 	ERROR	https://en.wikipedia.org/wiki/Shebang_(Unix
I1211 16:24:54.221] 		Not Found (HTTP error 404)

Hrm. Second time I've seen this today. The website works when I access it from a browser.

/test pull-tekton-pipeline-build-tests

@vdemeester
Copy link
Member

I1211 16:24:52.372] ----------------------------------------------
I1211 16:24:52.372] ---- Checking links in the markdown files ----
I1211 16:24:52.373] ----------------------------------------------
I1211 16:24:54.221] docs/tasks.md
I1211 16:24:54.221] 	ERROR	https://en.wikipedia.org/wiki/Shebang_(Unix
I1211 16:24:54.221] 		Not Found (HTTP error 404)

Hrm. Second time I've seen this today. The website works when I access it from a browser.

/test pull-tekton-pipeline-build-tests

The link check might be not as smart as we are hoping for 😛

@dibyom
Copy link
Member

dibyom commented Dec 11, 2019

The link check might be not as smart as we are hoping for

Yeah, its picking up the URL as : https://en.wikipedia.org/wiki/Shebang_(Unix which does indeed return a 404 response

@vdemeester
Copy link
Member

/test pull-tekton-pipeline-build-tests

Copy link
Member

@afrittoli afrittoli left a comment

Choose a reason for hiding this comment

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

Nice, thank you!
What about setting -o pipefail too? Otherwise piped commands can hide failures.
Or go even further: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/

The blog post is about bash but the pipefail option works fine with sh too - I verified on a busybox image:

/home # cat test.sh
cat test.sh
#!/bin/sh
set -euxo pipefail
MSG=${MYMSG:-hello!}
false | echo $MSG
/home # ./test.sh
./test.sh
+ MSG='hello!'
+ false
+ echo 'hello!'
hello!
/home # echo $?
echo $?
1

@dibyom
Copy link
Member

dibyom commented Dec 12, 2019

/test pull-tekton-pipeline-build-tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

script mode set -x by default when no #! provided
6 participants