only
to rules
Using only
:
job:
script:
- echo something...
only:
- master
Using rules
:
job:
script:
- echo something...
rules:
- if: $CI_COMMIT_BRANCH == "master"
Variables come from several different places, and used in different places.
Come from:
used in:
When are pipelines run?
You may want to avoid multiple pipelines being started by different triggers.
jobname:
rules:
# if statements can reference variables
# including the predefined ones
- if: $CI_MERGE_REQUEST_IID
script:
- echo "I only run inside MRs"
NOTE: when using rules
, do NOT use only
or except
(these are deprecated ones).
if
changes
- when some files where changedexists
$VAR_NAME
- true if non-empty==
!=
=~
- equals regex!~
- not equals regex&&
||
when
- tip: read this as "then" (if-then)allow_failure
- booleanstart_in
when
options:
always
- ignores thenever
- job is never startedon_success
- previous stages finished with successon_failure
manual
delayed
- ???when: on_success
- previous stage resulted in successallow_failure: false
when: on_success
when: delayed
when always
when: on_success
when: delayed
when: always
when: on_success
when: delayed
when: always
when: never
notes:
when
with no if
Trying to explain with pseudcode.
TARGET DECK: DevOps/gitlab-ci
gitlab-ci rules
defaults: #flashcard
# if no attributes defined:
when: on_success
allow_failure: false
#
# job goes to the pipeline if
rules && (when: on_success || delayed || always)
!rules && (when: on_success || delayed || always)
#
# job does NOT goes to the pipeline if
!rules && !(when: on_success || delayed || always)
job:
script: "echo Hello, Rules"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_PIPELINE_SOURCE == "schedule"'
There's no when
clauses, so it's considered when: always
.
job:
script: "echo Hello, Rules"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- if: '$CI_PIPELINE_SOURCE == "schedule"'
when: never
- when: on_success
The job will not be added to the pipeline if those if
clauses match (because of the when: never
)
Cases not addressed by those if
s will make the job be added to the pipeline.
If the standalone when: on_success
didn't exist, the last when: never
would be the "default when" clause, resulting in job not going to the pipeline at all.
doubt: could those two if
s be replaced with this?:
- if: '$CI_PIPELINE_SOURCE "merge_request_event" || $CI_PIPELINE_SOURCE "schedule"'
when: never
docker build:
script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
rules:
- if: '$VAR == "string value"'
changes:
- Dockerfile
- docker/scripts/*
when: manual
If that $VAR == "string value"
and there were changes in any of those paths, then the job will be added to the pipeline and wait for a manual approval.
docker build:
script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
when: delayed
start_in: '3 hours'
allow_failure: true
If the commit happened in the master branch, then the job will be delayed for 3 hours and will be allowed to fail.