CI 'on_failure' conditional not working using rules
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Summary
In the documentation the 'when'-section has an example how to run a (cleanup) job no matter the status of the pipeline, using the when: always.
That works, but I need to know the status in the cleanup script, so I created a cleanup job with rules and conditional variables:
cleanup_rules_job:
stage: cleanup
rules:
- when: on_success
variables:
PIPELINE_STATUS: succeeded
- when: on_failure
variables:
PIPELINE_STATUS: failed
script:
- echo "cleaning up"
- echo "The pipeline has ${PIPELINE_STATUS}!"
Steps to reproduce
Create a pipeline using the following configuration
# .gitlab-ci.yml
stages:
- build
- cleanup
build_job:
stage: build
script:
- echo "build failed"
- exit 1
cleanup_job:
stage: cleanup
rules:
- when: on_success
variables:
PIPELINE_STATUS: succeeded
- when: on_failure
variables:
PIPELINE_STATUS: failed
script:
- echo "cleaning up"
- echo "The pipeline has ${PIPELINE_STATUS}!"
Example Project
https://gitlab.com/maikelv/test-ci-cleanup-job
What is the current bug behavior?
The cleanup_job doesn't run:
https://gitlab.com/maikelv/test-ci-cleanup-job/-/pipelines/636279078
What is the expected correct behavior?
The cleanup_job also runs and in its script the PIPELINE_STATUS == failed (or succeeded on a successful pipeline).
Output of checks
This bug happens on GitLab.com (currently v15.4.0-pre f62de2bcba0)
Workaround
An ugly workaround is to create two jobs, one with when: on_success and the other with when: on_failure and set the status variable on each:
cleanup_job_on_success:
stage: cleanup
when: on_success
variables:
PIPELINE_STATUS: succeeded
script:
- echo "cleaning up"
- echo "The pipeline has ${PIPELINE_STATUS}!"
cleanup_job_on_failure:
extends: cleanup_job_on_success
when: on_failure
variables:
PIPELINE_STATUS: failed
See maikelv/test-ci-cleanup-job!1
- Cleanup on a failed pipeline: https://gitlab.com/maikelv/test-ci-cleanup-job/-/pipelines/636298150
- Cleanup on a successful pipeline: https://gitlab.com/maikelv/test-ci-cleanup-job/-/pipelines/636298499
This is not a very attractive solution because it creates two identical jobs while this should be just one. Also it complicates the .gitlab-ci.yml.

