Conditional Jobs and Pipelines
Conditional Execution Based on Job Status
The when keyword allows you to define when a job should run. Let's try different values for the when keyword and see how they work.
stages:
- stage-0
- stage-1
- stage-2
- stage-3
- stage-4
- stage-5
- stage-6
- stage-7
job-0:
stage: stage-0
script:
- echo "running job-0"
job-1:
stage: stage-1
script:
- echo "running job-1"
when: on_success
job-2:
stage: stage-2
script:
# This job will fail
- echo "running job-2"
- exit 1
job-3:
stage: stage-3
script:
- echo "running job-3"
when: on_failure
job-4:
stage: stage-4
script:
- echo "running job-4"
job-5:
stage: stage-5
script:
- echo "running job-5"
when: always
job-6:
stage: stage-6
script:
- echo "running job-6"
when: manual
job-7:
stage: stage-7
script:
- echo "running job-7"
when: delayed
start_in: 1 minute
Based on the configuration above, this is what will happen:
job-0will run successfully.job-1will run only whenjob-0has succeeded because of thewhen: on_successclause. Theon_successvalue means that the job will run only when no jobs in earlier stages fail or haveallow_failure: true. In this case,job-1will run. It's worth noting that theon_successvalue is the default value for thewhenkeyword.job-2will fail because we forced it to fail (exit 1).job-3will run only when only when at least one of the previous jobs has failed because of thewhen: on_failureclause. In this case,job-3will run.job-4will not run because the previous job has failed (by default, thewhenkeyword is set toon_success).job-5will run regardless of the status of jobs in earlier stages because of thewhen: alwaysclause.job-6will run only when triggered manually because of thewhen: manualclause. It will not run automatically. The only way to run it is to trigger it manually using the GitLab UI.job-7would run after 1 minute because of thewhen: delayedclause. However, since there are earlier jobs that have failed, it will not run at all.
Cloud Native CI/CD with GitLab
From Commit to Production ReadyEnroll now to unlock all content and receive all future updates for free.
