Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab: Fail job in "after_script"?

Consider this .gitlab-ci.yml:

variables:
  var1: "bob"
  var2: "bib"

job1:
  script:
    - "[[ ${var1} == ${var2} ]]"

job2:
  script:
    - echo "hello"
  after_script:
    - "[[ ${var1} == ${var2} ]]"

In this example, job1 fails as expected but job2 succeeds, incomprehensibly. Can I force a job to fail in the after_script section?

Note: exit 1 has the same effect as "[[ ${var1} == ${var2} ]]".

like image 378
Brice Avatar asked Sep 04 '26 09:09

Brice


1 Answers

The status of a job is determined solely by its script:/before_script: sections (the two are simply concatenated together to form the job script).

after_script: is a completely different construct -- it is not part of the job script. It is mainly for taking actions after a job is completed. after_script: runs even when jobs fail beforehand, for example.

Per the docs: (emphasis added on the last bullet)

Scripts you specify in after_script execute in a new shell, separate from any before_script or script commands. As a result, they:

  • Have the current working directory set back to the default (according to the variables which define how the runner processes Git requests).
  • Don’t have access to changes done by commands defined in the before_script or script, including:
    • Command aliases and variables exported in script scripts.
    • Changes outside of the working tree (depending on the runner executor), like software installed by a before_script or script script.
  • Have a separate timeout, which is hard-coded to 5 minutes.
  • Don’t affect the job’s exit code. If the script section succeeds and the after_script times out or fails, the job exits with code 0 (Job Succeeded).
like image 193
sytech Avatar answered Sep 06 '26 16:09

sytech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!