Skip to content

Gitlab CI script doesn't pass exit code of previous command properly

Example gitlab-ci.yml:

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

Current build result:

gitlab-ci-multi-runner 1.3.2 (0323456)
Using Shell executor...
Running on gitlabci-runner03...
Fetching changes...
HEAD is now at 9f64dee Test commit
From https://myserver/gdubicki/gitlab-ci-test
 * [new branch]      test-false-and-true -> origin/test-false-and-true
Checking out 810aa8c5 as test-false-and-true...
$ false && true
$ echo $?
0

Build succeeded

Expected build result:

like above but echo $? should print 1 instead of 0.

Proposal

  • This functionality needs a feature flag.

  • Implement CheckForErrors() for the BashWriter, this would need to be called in Command(). Something like:

    func (b *BashWriter) CheckForErrors() {
    	b.Line("_runner_exit_code=$?; if [[ $_runner_exit_code -ne 0 ]]; then exit $_runner_exit_code; fi")
    }
  • Remove set -eo pipefail as a default option.

This means script commands do not rely on the set -e mechanism, but also means that subshells will no longer automatically get the set -eo pipefail behaviour.

We do not solve the problem of $? being overwritten by echo statements we add for debugging, but try to side-step the issue by offering a more consistent way of exiting on failure.

Edited by Tomasz Maczukin