We are working with GitLab CI and a strange behaviour just happened. We are trying to only run a given job (e.g, lint here below) if *.py files are changed and we are in a merge request, not a simple git branch. If, for example, I am going to push a change on the Readme.MD, the pipeline's job Lint here will be executed where it should have not.
.gitlab-ci.yml configuration:
image: python:3.7
stages:
- pre-build
- test
.ci_rules:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_IID'
changes:
- tests/*.py
- tests/*/*.py
- src/*.py
- src/*/*.py
- if: $CI_COMMIT_REF_NAME == "develop"
- if: $CI_COMMIT_REF_NAME == "main"
lint:
stage: pre-build
script:
- pip install flake8
- flake8
rules:
- !reference [ .ci_rules, rules ]
# [... other jobs not relevant for the issue]
The problem is in your job rules. Below are your numbered rules for better orientation:
1. $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_IID && changes
2. $CI_COMMIT_REF_NAME == "develop"
3. $CI_COMMIT_REF_NAME == "main"
Let's consider some examples, variables values and job evaluation result with your rules. Remember if you don't use when keyword in your rule definifon then the job evaluation needs only one rule from the rules set to trigger a job.
1. rule ✅
2. rule ✅
3. rule ❌
---
job evaluation = ✅
1. rule ❌
2. rule ✅
3. rule ❌
---
job evaluation = ✅
1. rule ❌
2. rule ❌
3. rule ❌
---
job evaluation = ❌
1. rule ❌
2. rule ✅
3. rule ❌
---
job evaluation = ✅
1. rule ❌
2. rule ❌
3. rule ✅
---
job evaluation = ✅
Do you see it already? 🙂 I think your problem is that you have rules 2. and 3. mixed with rule 1. Therefore if you create MR without file changes, but your source branch is develop, the job rule is evaluated as true (second example). I will suggest to have rule 1. in separated job (or template). Or use rules with when keyword and control it manually for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With