Search code examples
gitlabgitlab-ci

Run job only when changes in directory, where directory is passed in via parallel matrix (Gitlab CI)


There are a few ways to run jobs only when there are changes in a specific directory (only, rules, custom logic in a script block). However, I would like to combine this concept with a list of directories inside a parallel matrix, i.e,

image: alpine

stages:
- run

my-job:
  stage: run
  rules:
    - changes:
      -  $DIR
  script:
    - echo "In $DIR"
  parallel:
    matrix:
      - DIR: path/to/A/*
      - DIR: path/to/B

The intention being that my-job is only added to the pipeline and run for path/to/A when it contains changes, and for path/to/B when it contain changes. Trying this solution, the jobs are never added to the CI -- presumably because the rules are processed before the parallel matrix is.

I do not want to add logic to the script block; I want the job to be excluded from the pipeline altogether unless changes in $DIR exist.

How can I achieve my desired behavior?


Solution

  • As pointed out in comments on the original post, this should be possible.

    And it is.

    The example is not an exact reflection of the code I was using. In that code, there was a rule being copied in via extends that was overwriting the rules:changes that I thought was not working properly.

    For posterity, here is a related GitLab issue that was given to me in the comments -- the example code there works: https://gitlab.com/gitlab-org/gitlab/-/issues/363668