Job fails if last command ends with "exit 1"

Summary

In one of my CI jobs, I want to assert a variable has the right value. Else, I make the job fail with the exit 1 command. However, the job always fails...

I found out that adding another command in the script fix the issue.

Steps to reproduce

.gitlab-ci.yml
Failing job: 
  script:
    - var="foo"
    - echo $var
    - '[[ $var != "foo" ]] && exit 1'

Successfull job: 
  script:
    - var="foo"
    - echo $var
    - '[[ $var != "foo" ]] && exit 1'
    - echo $?

Actual behavior

See this pipeline

Expected behavior

Both jobs should be successful

Relevant logs and/or screenshots

image

job log
$ var="foo"
$ echo $var
foo
$ [[ $var != "foo" ]] && exit 1
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1

Environment description

It occurs both on our on-premise installation and on GitLab.com

Used GitLab Runner version

On premise:

Running with gitlab-runner 13.4.1 (e95f89a0)
"docker" executor

GitLab.com:

Running with gitlab-runner 13.5.0 (ece86343)
  on docker-auto-scale 72989761
Resolving secrets
00:00
Preparing the "docker+machine" executor
00:32
Using Docker executor with image ruby:2.5 ...

Possible fixes

Workaround: add a dummy command at the end of the gitlab-ci.yml file.

Edited Nov 16, 2020 by Benoit Vander Stappen
Assignee Loading
Time tracking Loading