50

I'm using Gitlab CI, and so have been working on a fairly complex .gitlab-ci.yml file. The file has an after_script section which runs when the main task is complete, or the main task has failed somehow. Problem: I need to do different cleanup based on whether the main task succeeded or failed, but I can't find any Gitlab CI variable that indicates the result of the main task.

How can I tell, inside the after_script section, whether the main task has succeeded or failed?

2
  • So are you referring to conditional after_script or you are referring to result of the main task. You can consider storing the result into a variable. docs.gitlab.com/ee/ci/variables/README.html Commented Apr 17, 2018 at 7:30
  • @ArihantGodha I'm pretty sure that variables set in one phase aren't preserved in another. Worth a test, though; even though I accepted the answer below, this would be my preferred solution. (I'd set a variable succeeded to 0, and then set it to 1 at the very end of the main script; I'd then test it in the after_script to see if the process completed.) Commented Apr 18, 2018 at 12:44

5 Answers 5

73

Since gitlab-runner 13.5, you can use the CI_JOB_STATUS variable.

test_job:
  # ...
  after_script:
    - >
      if [ $CI_JOB_STATUS == 'success' ]; then
        echo 'This will only run on success'
      else
        echo 'This will only run when job failed or is cancelled'
      fi

See GitLab's documentation on predefined_variables: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

7
42

The accepted answer may apply to most situations, but it doesn't answer the original question and will only work if you only have one job per stage.

Note: There currently a feature request opened (issues/3116) to handle on_failure and on_success in after_script.

It could be possible to use variables to pass the job status to an after_script script, but this also has a feature request (issues/1926) opened to be able to share variables between before_script, script and after_script.

One workaround will be to write to a temporary file that will be accessed during the after_script block.

test_job:
  stage: test
  before_script:
    - echo "FAIL" > .job_status
  script:
    - exit 1
    - echo "SUCCESS" > .job_status
  after_script:
    - echo "$(cat .job_status)"
6
  • 11
    exit 1 will stop the script and echo "SUCCESS" > .job_status will never run
    – Arith
    Commented Apr 24, 2020 at 3:57
  • this is the best solution for now
    – user319862
    Commented May 28, 2020 at 20:22
  • Wouldn't exporting env variable work equally well ? export JOB_STATUS=FAIL; echo $JOB_STATUS Commented Aug 2, 2021 at 10:07
  • 2
    This does not work for me, because after_script runs on a different context than before_script/script
    – gibertoni
    Commented Jan 4, 2022 at 13:26
  • 8
    @Arith yeah, and it's exactly what was intended - you got FAIL in .job_status then. This is only for visualising how to achieve the flow, normally you put your actual code into script section and store job status at the end, so you can check against it in after_script.
    – Wirone
    Commented Mar 7, 2022 at 15:51
12

Instead of determining whether or not the task succeeded or failed in the after_script, I would suggest defining another stage, and using the when syntax, where you can use when: on_failure or when: on_success.


Example from the documentation:

stages:
- build
- cleanup_build
- test
- deploy
- cleanup

build_job:
  stage: build
  script:
  - make build

cleanup_build_job:
  stage: cleanup_build
  script:
  - cleanup build when failed
  when: on_failure

test_job:
  stage: test
  script:
  - make test

deploy_job:
  stage: deploy
  script:
  - make deploy
  when: manual

cleanup_job:
  stage: cleanup
  script:
  - cleanup after jobs
  when: always
7
  • Unfortunately cleanup_job would not if deploy_job is retried, and then fails again. Commented Mar 5, 2019 at 19:12
  • 1
    What if you wanted to perform an after_script that required the same CI_JOB_ID? Commented Sep 24, 2019 at 15:56
  • Or you want to inspect the docker containers running that failed... =(
    – user319862
    Commented May 28, 2020 at 20:20
  • 1
    that was not the question he asked and it does not help if I need to execute something in after script only when the job was successful or not
    – iRaS
    Commented Aug 15, 2021 at 8:56
  • This would normally be my preferred method, but there is no guarantee the subsequent jobs will happen on the same runner if you use a pool of N runners and need the job to clean up the runner.
    – James Harr
    Commented Mar 1, 2022 at 14:26
1

Just another way to handle this if you want to setup if failure behavior.

scripts:
  - ./script_that_fails.sh > /dev/null 2>&1 || FAILED=true
  - if [ $FAILED ]
    then ./do_something.sh
    fi

Note: Other examples also worked for me, but I find this implementation more faster and suitable for me

0

As an option go from opposite direction. When the script is failed, then stop working and continue in other cases

Build:
  stage: build
  script:
    - echo "This is build stage"
  after_script:
    - >
        if [ $CI_JOB_STATUS != 'success' ]; then
          echo -e "\033[0;31mBUILD FAILED! Exit without after scipt code"
          exit 1
        fi

    - !reference [.after-script-code]


.after-script-code:
  - echo "some code"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.