简体   繁体   中英

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

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?

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.

标签

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

管道

Instead of using only/except you can use rules which are more powerful.

Rules support regex pattern matching .

Your rule for excepting only specific kind of branches/tags like dev_1.0 , dev_1.1 , dev_1.2 should look like:

rules:
    - if: '$CI_COMMIT_BRANCH =~ /^dev_[0-9]+\.[0-9]+$/ || $CI_COMMIT_TAG =~ /^dev_[0-9]+\.[0-9]+$/'

Predefined environment variables like CI_COMMIT_BRANCH and CI_COMMIT_TAG are described here .

Gitlab.com ? You could try a combination of except and only . Something like

only: 
- tags 
- branches 
except:
- /^(?!(branch1|branch2|branch3|dev_[0-9.]*$)$).*$/

The idea being, allowing only branches and tags to trigger a job, with the exception of everything different from branch[1-3] and dev_ branches/tags

And here is the official documentation for this:

GitLab CI/CD pipeline configuration reference

There you find the section for only/except with the supported regex syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM