Gitlab-CI rules If/Changes do not work as expected

Viewed 12004

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) only and only if we are in a merge request not a simple git branch, as well as only if *.py files shows changes. However, from what we have seen on the documentation we seems to be okay but not really as it do not work well at the moment. 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.

Please, if you have any ideas/tips, We would be very grateful.

Thank you very much for your understanding, Bw.

---> Gitlab CI Yaml configuration:

.gitlab-ci.yml:

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]
1 Answers

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.

  • Create MR develop -> main with file change
1. rule ✅
2. rule ✅
3. rule ❌
---
job evaluation = ✅ 
  • Create MR develop -> main without file change
1. rule ❌
2. rule ✅
3. rule ❌
---
job evaluation = ✅ 
  • Create MR test1 -> main without file change
1. rule ❌
2. rule ❌
3. rule ❌
---
job evaluation = ❌
  • Push to the develop branch
1. rule ❌
2. rule ✅
3. rule ❌
---
job evaluation = ✅ 
  • Push to the main branch
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.

Related