Ishan
Ishan

Reputation: 4339

Gitlab CI - How to trigger a build only if changes happen on particular set of files

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.

Upvotes: 81

Views: 110328

Answers (5)

Connor Shea
Connor Shea

Reputation: 870

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.

Upvotes: 8

Pety
Pety

Reputation: 39

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,

Upvotes: 1

NobbyNobbs
NobbyNobbs

Reputation: 1434

That feature has been available since version 11.4.

See docs.

Upvotes: 0

mhellmeier
mhellmeier

Reputation: 2281

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.

Upvotes: 80

Hai Nguyen
Hai Nguyen

Reputation: 1823

This works for me when combines with only rule

build-web-dev:
  stage: build
  script:
    - build.sh
  only:
    - develop
    - changes
      web/*
      Dockerfile.web

Upvotes: 0

Related Questions