64

In Gitlab CI, how do you trigger a build only if changes happen on particular set of files?

Is there a way to either include or exclude certain files from triggering a build? For eg: updating README.md, .gitignore files should not cause a build to trigger.

Ishan
  • 3,931
  • 11
  • 37
  • 59

5 Answers5

55

UPDATE 2

Only/except seem to be an unstable feature. The doc recommends using rules:changes instead:

job:
  script:
    - build # replace with your build command
  rules:
    - changes:
      - /*.{java, py} # ... or whatever your file extension is
      - dockerfiles/**/* # all files under folder 'dockerfiles'

UPDATE 1:

Like Gajus mentioned in the comments, it is now possible!

Merge Request: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21981

Documentation: https://docs.gitlab.com/ee/ci/yaml/#onlyexcept-basic (Thanks to Connor Shea for the tip in the comments)

Original post:

No, it is not possible – not now!

I think this is the issue you are looking for: https://gitlab.com/gitlab-org/gitlab-ce/issues/19232 – It is a very high rated issue (over 100 thumps-up).

The milestone has changed to Next 3-6 months, 4 months ago. (https://gitlab.com/gitlab-org/gitlab-ce/issues/19232#note_31943850) I hope we will see this function soon.

Liam
  • 27,717
  • 28
  • 128
  • 190
mhellmeier
  • 1,982
  • 1
  • 22
  • 35
6

Update: This is now possible as of 11.4, see https://docs.gitlab.com/ee/ci/yaml/#ruleschanges

Original post:

There's not currently any way to trigger a build in GitLab CI conditionally based on which files have been edited.

I've thought about this feature myself before, and I think it could be very useful for a number of different use cases.

The closest issue I could find for this is https://gitlab.com/gitlab-org/gitlab-ce/issues/23010

Anyway, to answer your question:

You can't really do this right now – even manually – since there's no way to determine if a merge request has changed a file or not since CI has no concept of a merge request.

Pipelines "understanding" what Merge Requests are should be added sometime soon.

Noah W. Smith
  • 79
  • 1
  • 7
Connor Shea
  • 795
  • 7
  • 22
1

This works for me when combines with only rule

build-web-dev:
  stage: build
  script:
    - build.sh
  only:
    - develop
    - changes
      web/*
      Dockerfile.web
Hai Nguyen
  • 1,675
  • 19
  • 14
1

I was browsing the Net without any working solution, then I returned back to the official gitlab docu and tried what they write: https://docs.gitlab.com/15.7/ee/ci/jobs/job_control.html#onlychanges--exceptchanges-examples

And it turned out like a working Solution in my case, the magic was just to add: except rule into all related jobs, like this:

build:
  script: npm run build
  except:
    changes:
      - "*.md"

Updating docu (.md), does not trigger the whole pipeline anmore.

Cheers,

Pety
  • 39
  • 6
0

That feature has been available since version 11.4.

See docs.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
NobbyNobbs
  • 1,341
  • 1
  • 11
  • 17