gitlabgitlab-cistatus

.gitlab-ci.yml after_script section: how can I tell whether the task succeeded or failed?


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?


Solution

  • 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