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?
Asked
Active
Viewed 8.8k times
65

veben
- 19,637
- 14
- 60
- 80

Victor Martinez
- 980
- 1
- 8
- 14
5 Answers
114
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.
-
11The not so funny thing is we can't read more in the documentation. The documentation actually has less information than you give here. I still wonder why just typing the name of a directory (somedir, or somedir/) doesn't work, nor why somedir/** wouldn't work, and why only somedir/**/* would work even though somedir/**.extension is supported. Add to that the big confusion regarding whether paths may be relative or only absolute (include local), and it's quite an unpleasant experience that could have been made better with decent documentation. – Johan Boulé Jan 13 '22 at 10:19
-
6just a fyi.. to get file folder recursion I had to add `- foldername/**/*` – Erik Mar 13 '22 at 09:30
-
2Try this doc link @Johan Boulé (and anyone else). This tell you more. https://docs.gitlab.com/ee/ci/jobs/job_control.html#onlychanges--exceptchanges-examples – Max O. Oct 18 '22 at 10:11
-
1`only` is obsolete now https://docs.gitlab.com/ee/ci/jobs/job_control.html#specify-when-jobs-run-with-only-and-except – n.podbielski Jul 13 '23 at 12:02
27
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 existence. Rules Documentation.

Josh Correia
- 3,807
- 3
- 33
- 50

Pezhvak
- 9,633
- 7
- 29
- 39
20
You can do this with rules:changes
keywords:
rules:
- changes:
- directory/**/*
3
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

MUHAHA
- 1,597
- 2
- 16
- 25
-2
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.

djuarezg
- 2,307
- 2
- 20
- 34
-
Hi I am interested in your last sentence, how do you restrict what CI does depending on which files are modified? – Alejandro Mar 29 '19 at 09:51
-
1@Alejandro Well, maybe this is not the best approach but you could compare commit diffs yourself and decide what to do depending on the last changes. – djuarezg Mar 29 '19 at 10:14