Search code examples
gitlab-ci

Gitlab CI Rules: I expected this to only run if source code files were changed


I have a job in my .gitlab-ci.yaml file with the following rules:

rules:
  - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    changes:
      - src/**/*
  - if: $CI_COMMIT_BRANCH
    changes:
      - src/**/*
  - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
    when: never

I was expecting it to only run in two scenarios:

  1. on any commit made to an open merge request
  2. on any commit to a branch, but only if the MR was not yet open (trying to avoid duplicate runs)

And specifically, only when files inside my source code folder were changed. And this is the part that doesn't seem to be working for me. I just made a commit to a feature branch adding new files to a new folder at the root of the repository. I made no changes to the source code directory. But this job fired on that branch commit. Can anyone decipher why?

It's been a few months, so I don't remember where I got this rules block from, but it was informed by research I did here on SO. And not that I think it affects my current issue (running when no source code was changed), but I am starting to think that the last bullet should be moved to the top. If the rules evaluate in order, I would want to rule out the branch commits while an MR is open, first.


Solution

  • Yes, rules are evaluated in the order written in the spec, from the top. How to avoid surprises is to start with the most specific rules and continue with more generic rules.

    rules:
      - if: $CI_PIPELINE_SOURCE == "merge_request_event"
        changes:
          - src/**/*
      - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
        when: never
      - if: $CI_COMMIT_BRANCH
        changes:
          - src/**/*
    

    However, even when using your rules definition it shouldn't start the job unless you had a MR opened and any previous commits in the MR had changes to src/**/*, because in MR pipelines the diff is generated against target branch, not previous commit in source branch.