Victor Martinez
Victor Martinez

Reputation: 1170

GitLab CI/CD: Run jobs only when files in a specific directory have changed

I would like to run specific jobs on the .gitlab-ci.yaml if and only if files within specific directories of the repository have changed. Is there a way to do this with gilab's ci/cd tooling or would it be easier just to run a custom build script?

Upvotes: 83

Views: 135748

Answers (5)

Pezhvak
Pezhvak

Reputation: 10868

This is now possible as of GitLab 12.3:

rules:
    - changes:
      - package.json
      - yarn.lock
    - exists: 
      - node_modules
      when: never
    - when: always

You can either check for file changes or check for existence. read more at Rules Documentation.

Upvotes: 38

kelvinji
kelvinji

Reputation: 1524

Changes policy introduced in GitLab 11.4.

For example:

docker build:
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  only:
    changes:
      - Dockerfile
      - docker/scripts/*
      - dockerfiles/**/*
      - more_scripts/*.{rb,py,sh}

In the scenario above, if you are pushing multiple commits to GitLab to an existing branch, GitLab creates and triggers the docker build job, provided that one of the commits contains changes to either:

  • The Dockerfile file.
  • Any of the files inside docker/scripts/ directory.
  • Any of the files and subdirectories inside the dockerfiles directory.
  • Any of the files with rb, py, sh extensions inside the more_scripts directory.

You can read more in the documentation and with some more examples.

Upvotes: 139

veben
veben

Reputation: 22302

You can do this with rules:changes keywords:

rules:
  - changes:
    - directory/**/*

See: https://docs.gitlab.com/ee/ci/yaml/#rules

Upvotes: 33

MUHAHA
MUHAHA

Reputation: 1857

You can do something like this:

trigger_documentation:
  image: alpine:3.8
  stage: trigger_documentation
  script:
    - apk update && apk add git curl bash
    - |
      if git diff --name-only --diff-filter=ADMR @~..@ |grep ^doc/; then
            echo "triggering..."
      fi

Upvotes: 3

djuarezg
djuarezg

Reputation: 2541

It is not yet possible, see https://gitlab.com/gitlab-org/gitlab-ce/issues/19232

On the other hand if you prepare a custom script for CI, you could restrict what the CI does depending on which files/paths where modified.

Upvotes: -3

Related Questions