Skip to content

Add explicit bash shell error checks

Arran Walker requested to merge ajwalker/fix-bash-exit-code into master

What does this MR do?

Performs an error check after each Bash script command is executed, and exits if the previously executed command returned a non-zero exit code. This aligns the behaviour more with the other shells, regardless of what Bash error handling options have been set.

Why was this MR needed?

With set -e enabled, it is expected that shells exit immediately should the last command fail. However, this isn't true for all cases, and whilst this is by design, how shells handle it is inconsistent.

Typically, in this situation, it is on the user to check the outcome of the last command by checking the exit code. However, due to the way our shell scripts are written, any code the user writes to check the last command's exit code, will instead be checking the exit code of the echo statements we add to append information to the job's trace.

We've opted to side-step this issue by just ensuring that we always exit when a command returns a non-zero exit code. This is how other shells are already being handled. It's worth noting, that this doesn't affect scripts in subshells, only those defined in the scripts section of a job.


This MR deviates slightly from the proposal in #25394 (closed) in that it doesn't change any Bash options. set -eo pipeline is still being used. I don't think it's necessary that we change this option yet, as it doesn't affect the additional check we're now performing. Modifying the default options can be done as an additional MR.

What's the best way to test this MR?

The following job should fail if FF_ENABLE_BASH_EXIT_CODE_CHECK is enabled.

image: busybox:latest
variables:
  FF_ENABLE_BASH_EXIT_CODE_CHECK: "true"

build_1:
  script:
    - false && true
    - echo $?

What are the relevant issue numbers?

closes #25394 (closed)

Edited by Tomasz Maczukin

Merge request reports