Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit a job in gitlab ci to a tag matching a pattern?

I want to be able to trigger a deployment to a special server every time a tag matching a pattern is pushed.

I use the following job definition:

# ...
deploy to stage:
  image: ruby:2.2
  stage: deploy
  environment:
    name: foo-bar
  script:
    - apt-get update -yq
    - apt-get install -y ruby-dev
    - gem install dpl
#    - ...
  only:
    - tags

Now my question: how can I limit this to tags that have a certain name, for example starting with "V", so that I can push a tag "V1.0.0" and have a certain job running?

like image 446
Sebastian Schuth Avatar asked May 23 '17 19:05

Sebastian Schuth


3 Answers

Only accepts regex patterns so for your use case it would be:

only:
  - /^V.*$/
except:
  - branches
  - triggers
like image 79
Jakub Kania Avatar answered Sep 20 '22 16:09

Jakub Kania


The best way to do is filtering by the Gitlab CI/CD variables matching your pattern

only
  variables:
    - $CI_COMMIT_TAG =~ /^my-custom-tag-prefix-.*/

As the documentation says:

  • CI_COMMIT_TAG: The commit tag name. Present only when building tags.
like image 24
maarkeez Avatar answered Sep 19 '22 16:09

maarkeez


Since only / except are now being deprecated, you should now prefer the rules keyword

Things get pretty simple, here's the equivalent using rules:

rules:
  # Execute only when tagged starting with V followed by a digit
  - if: $CI_COMMIT_TAG =~ /^V\d.*/
like image 28
CharlesB Avatar answered Sep 18 '22 16:09

CharlesB