Search code examples
gitlab-ci

Tried to run a function only when source is committed to a limited path


I read gitlab document on rules(https://docs.gitlab.com/ee/ci/jobs/job_control.html) but I'm not sure I understood properly.


Goal - Run a function only when committed to a limited path.


My first script didn't work,

test:
  stage: test
  script: 
    - some commands
  rules:
    - changes:
      - limited/path/*
    - when: always

stages:
  - test

Below script worked as intended.

test:
  stage: test
  script: 
    - some commands
  only:
    changes:
      - limited/path/*

stages:
  - test

Is this because I left - if: $CI_COMMIT_BRANCH under rules:?
Should I remove when: always also? Can I assume that only is a sub-concept of rules?


Solution

  • You rule definition should be something like this:

    rules:
        - changes:
          path:
           - limited/path/*
          when: always
        - when: never
    

    From your original syntax the job run on change and always, this definition makes run always on change on the path and never for all other cases (you can see the single rule - when: never as the default option for the rules).