Reputation: 21
In order to stop the current default Gitlab behaviour of starting pipelines on branch creation I am trying to add a check in each job so that only merge requests trigger jobs when they have changes.
This is what I got so far:
rules:
- if: '[$CI_PIPELINE_SOURCE == "merge_request_event"] && [! git diff-index --quiet HEAD --]'
I am not quite familiar with bash which is surely the problem because I am currently encountering a 'yaml invalid' error :d
PS: Is there maybe a better way to do this instead of adding the check to each task?
Upvotes: 2
Views: 1183
Reputation: 4400
I am not quite familiar with bash which is surely the problem because I am currently encountering a 'yaml invalid' error :d
The issue seems to be with
[! git diff-index --quiet HEAD --]
You can not
use bash syntax in Gitlab rules
but to script
section you can, as the name implies
In order to stop the current default Gitlab behaviour of starting pipelines on branch creation
If this is your goal I would recommend the following rules
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
when: never
- if: $CI_PIPELINE_SOURCE == "push"
Let's break down the aforementioned rules
The following rule will be true
for merge requests
if: $CI_PIPELINE_SOURCE == "merge_request_event"
The predefined variable CI_COMMIT_BEFORE_SHA
will be populated with this value 0000000000000000000000000000000000000000
, when you create a new pipeline or a merge request
Therefore, the following rule will stop
the execution of a new pipeline and of a merge request
.
- if: $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
when: never
BUT
the merge requests are accepted from the previous rule, taking into account how gitlab evaluates them.
Quoting from https://docs.gitlab.com/ee/ci/jobs/job_control.html#specify-when-jobs-run-with-rules
Rules are evaluated in order until the first match. When a match is found, the job is either included or excluded from the pipeline
Finally, the following rule will be true for a new pushed commit
- if: $CI_PIPELINE_SOURCE == "push"
PS: Is there maybe a better way to do this instead of adding the check to each task?
The aforementioned rules dont
have to be added for each job, but instead are configured once for each pipeline take a look
https://docs.gitlab.com/ee/ci/yaml/workflow.html
Basicaly, add the workflow rules
statement at the start of your gitlab.yml and you are good to go
Upvotes: 0
Reputation: 522
i don't know if it can be useful, but Gitlab-ci provide the only
job keyword that you can combine with changes
and insert a path to files, in this way you can execute jobs only if there are changes on the code you are interested on.
Example
docker build:
script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
only:
refs:
- branches
changes:
- Dockerfile
- docker/scripts/*
- dockerfiles/**/*
- more_scripts/*.{rb,py,sh}
- "**/*.json"
DOC: https://docs.gitlab.com/ee/ci/yaml/#onlychanges--exceptchanges
Upvotes: 0