Driving Job Execution, Timing, and Dependencies with Conditions and Rules
55%
Controlling Based on Job Status
Here, the goal is to show how a job's execution can be gated not just on conditions, but also on the status of earlier jobs and the context of the pipeline, using when in combination with rules.
Example:
You want to run a job only when the target branch of the merge request is protected and no previous jobs have failed:
build_job:
stage: build
script:
- echo "running build_job"
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_PROTECTED == "true"'
when: on_success
However, for this special case, since the default when is always on_success, you can omit it and we'll have the same behavior:
build_job:
stage: build
script:
- echo "running build_job"
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_PROTECTED == "true"'
Example:
You want to run a job only when the user who triggered the pipeline has an email that ends with @company.local and at least one of the previous jobs has failed:
build_job:
stage: build
script:
-Cloud Native CI/CD with GitLab
From Commit to Production ReadyEnroll now to unlock all content and receive all future updates for free.
