How can I trigger a gitlab pipeline job for a specific branch but not if the commit contains only changes to certain files?

Viewed 575

For example, I want a deploy job triggered but not if the commit contains changes relating to a Makefile and/or docker-compose.yml. How can I specify those rules/conditions? Thank to anyone who is willing to help.

2 Answers

this can be achieved with the rules block and the fact that the rules are evaluated sequentially and stop as soon as one rule applies.

Rules are evaluated when the pipeline is created, and evaluated in order until the first match. When a match is found, the job is either included or excluded from the pipeline, depending on the configuration. https://docs.gitlab.com/ee/ci/yaml/#rules

this means you could do something like the following as a first rule, to ensure that it is never executed.

rules:
  - changes:
      - docker-compose.yml
    when: never

Disclaimer: i did not test this, but based on the documentation it should work like this. see https://docs.gitlab.com/ee/ci/yaml/#rules for further details

Combine if rule to test the branch, and use changes condition to test if the file is in the changeset. Be sure to order them correctly to make sure the rule with changes is first.

my_job:
  rules:
    - if: '$CI_COMMIT_REF_NAME == "main"'
      changes:  
        - makefile
        - docker-compose.yml
      when: never  # dont run when these files are changed on this branch
    - if: '$CI_COMMIT_REF_NAME == "main"'
      when: on_success  # otherwise run normally on this branch only

See rules:changes for more information.

Related