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?
The issue seems to be with
You can
not
use bash syntax in Gitlabrules
but toscript
section you can, as the name impliesIf this is your goal I would recommend the following rules
Let's break down the aforementioned rules
The following rule will be
true
for merge requestsThe predefined variable
CI_COMMIT_BEFORE_SHA
will be populated with this value0000000000000000000000000000000000000000
, when you create a new pipeline or a merge requestTherefore, the following rule will
stop
the execution of anew pipeline and of a merge request
.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
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.htmlBasicaly, add the
workflow rules
statement at the start of your gitlab.yml and you are good to go