How to run Gitlab CI only for specific branches and tags?

Bartłomiej Semańczyk picture Bartłomiej Semańczyk · Oct 16, 2018 · Viewed 23k times · Source

I would like to setup my project_dev CI only for 3 branches and specific kind of tags like: dev_1.0, dev_1.1, dev_1.2.

How can I achieve that?

This is what I have now:

project_dev:
  stage: dev
  script:
    - export
    - bundle exec pod repo update
    - bundle exec pod install
    - bundle exec fastlane crashlytics_project_dev
  after_script:
    - rm -rf ~/Library/Developer/Xcode/Archives || true
  when: manual
  only:
    - develop
    - release
    - master
    - //here I need to add condition to fire that stage additionally only for specific tags. How can I setup regexp here?
  tags:
    - iOS

When I type it like:

  only:
    - branches
    - /^dev_[0-9.]*$/

It also runs the CI for tags like: dev1.2 but it should not. Why? Is there a regexp for tags at all?

Answer

Gasol picture Gasol · Oct 17, 2018

Sounds like a regular expression question. I just created a project on gitlab.com for the regular expression.

File: .gitlab-ci.yml

project_dev:
  # Irrelevant keys is skipped
  script:
    - echo "Hello World"
  only:
    - develop
    - release
    - master
    - /^dev_[0-9]+(?:.[0-9]+)+$/ # regular expression

I was pushed all of tags you mentioned to test this regular expression.

Tags

As you can see , It will match tags like dev_1.0, dev_1.1, but the job project_dev will not be triggered by tag dev1.2, You can check the result on pipeline pages

Pipelines