57

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.

5 Answers 5

50

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.

3
  • 1
    On every README edit we need to bump version of the lib or else pipeline will fail.. i mean what are they thinking :(
    – holms
    Jul 15, 2018 at 17:05
  • Does not seem like this fires when the file in question is first added.
    – Mark
    Jan 8, 2021 at 19:50
  • only/accept are deprecated in favour of rules
    – Liam
    Oct 15, 2022 at 5:25
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.

1

This works for me when combines with only rule

build-web-dev:
  stage: build
  script:
    - build.sh
  only:
    - develop
    - changes
      web/*
      Dockerfile.web
1
  • 2
    only/accept are deprecated in favour of rules
    – Liam
    Oct 15, 2022 at 5:27
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,

0

That feature has been available since version 11.4.

See docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.