You can control when your jobs run using the only/except
keywords, or the more advanced rules
keyword along with the pipeline source variable.
Example with only
:
scheduled_test:
stage: tests
image: my_image:latest
only:
- schedules
script:
- ./run_some_things.sh
The only
keyword lets you define some conditions that mean "this job only runs if these conditions are true", and offers a shorthand to check the source of the pipeline. schedules
means that the pipeline was started from a schedule rather than a push, trigger, etc. The except
keyword is just the opposite. If it had except: schedules
the job would always run except if it was scheduled. You can see the full documentation for the only/except
keywords here: https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-basic
As of Gitlab version 12.3, the rules
keyword extends the possibilities of the only/except
options. We could get the same result from the example above like this:
scheduled_test:
stage: tests
image: my_image:latest
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
when: always
- if: '$CI_PIPELINE_SOURCE == "push"'
when: never
In this example, we check the predefined variable $CI_PIPELINE_SOURCE
to see what started this pipeline. If it's "schedule", we always run this job. As an example, if the source is "push" (so the pipeline was started by a git push
command), this job will never run. With the rules
keyword, all if
statements are OR'ed together, so the example above reads, if the source is schedule, always run OR if the source is a push, never run
. However, you can AND multiple conditionals together in the same if:
rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $MY_CUSTOM_VARIABLE == true'
when: manual
You can read the full documentation for the rules
keyword here: https://docs.gitlab.com/ee/ci/yaml/#rules
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…