Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 771
Bartłomiej Semańczyk Avatar asked Oct 16 '18 08:10

Bartłomiej Semańczyk


People also ask

How do I run a pipeline with a specific commit?

You can't run a GitLab pipeline for a specific commit, since the same commit may belong to multiple branches. To do what you want, you need to create a branch from the commit you want to run the pipeline for. Then you can run the manual pipeline on that branch.

What is Before_script in GitLab CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.

What is the use of tags in GitLab CI Yml?

gitlab-ci. yml , you can specify some jobs with the tag testing . If a runner with this tag associated is available, it will pickup the job. In Git, within your repository, tags are used to mark a specific commit.

What is artifacts in GitLab CI?

Artifacts are files created as part of a build process that often contain metadata about that build's jobs like test results, security scans, etc. These can be used for reports that are displayed directly in GitLab or can be published to GitLab Pages or in some other way for users to review.


2 Answers

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

like image 96
Gasol Avatar answered Oct 24 '22 09:10

Gasol


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.

like image 20
Jakob Liskow Avatar answered Oct 24 '22 10:10

Jakob Liskow