Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 421
Daniel Griscom Avatar asked Apr 17 '18 00:04

Daniel Griscom


People also ask

What is used to restrict when a job is executed on your pipeline?

Run jobs for scheduled pipelines To configure a job to be executed only when the pipeline has been scheduled, use the rules keyword.

How does GitLab CI Yml work?

gitlab-ci. yml file to your repository, GitLab detects it and an application called GitLab Runner runs the scripts defined in the jobs. In this example, the build-code-job job in the build stage runs first. It outputs the Ruby version the job is using, then runs rake to build project files.

What are tags in GitLab CI?

In Git, within your repository, tags are used to mark a specific commit. It is often used to tag a version. The two concepts can be mixed up when you use tags (in Git) to start your pipeline in GitLab CI. In your . gitlab-ci.

What is Ci_commit_tag?

CI_COMMIT_TAG - The commit tag name. Present only when building tags. Therefore in the variables section CI_COMMIT_TAG is not defined, hence equals to "". So if you want to use CI_COMMIT_TAG use in job where tags are defined. See https://docs.gitlab.com/ee/ci/yaml/README.html#tags.


2 Answers

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)" 
like image 96
Carl Bélanger Avatar answered Sep 25 '22 15:09

Carl Bélanger


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

like image 40
Nepoxx Avatar answered Sep 21 '22 15:09

Nepoxx