68

How do I run a .gitlab-ci.yml job only on a tagged Master branch?

job:
  script:
  - echo "Do something"
  only:
  - master
  - tags

The above code will run if either condition exists: a Master branch or a tagged commit.

My goal is to have this run for a production deploy, but it would require that it be on the Master branch and that it be tagged (with a version). Otherwise, I'll have another job that will push to staging if its missing a tag.

Carson Cole
  • 3,534
  • 4
  • 21
  • 32
  • 2
    How about running it for tags and checking if tag belongs to master branch in a script? – Jakub Kania Mar 15 '17 at 00:12
  • I thought it should also work with Variables and tried it with `rules: - if: $CI_COMMIT_TAG != null' && $CI_COMMIT_BRANCH == "master"` and also with `only: refs: - test variables: - $CI_COMMIT_TAG != null`, but without success... – Wolfson Sep 02 '20 at 10:45
  • @Wolfson The problem with that is that if the pipeline is triggered by a tag, `CI_COMMIT_BRANCH` is not defined, and viceversa with a commit trigger and `CI_COMMIT_TAG` – Anakhand Mar 11 '21 at 22:13

6 Answers6

22

This behavior will be introduced in version 12.

Open issue was recently update:

Jason Lenny @jlenny changed title from {-Update .gitlab-ci.yml to support conjunction logic for build conditions-} to Conjunction logic for build conditions MVC · 2 days ago

Jason Lenny @jlenny changed milestone to 12.0 · 2 days ago

(fingers crossed)

A solution is to use the except keyword to exclude all the branches, in conjunction with only to run on tags, in this way you run your pipeline only on tag in master branch:

  only:
    - tags
  except:
    - branches

I'm using version 11.3.4

16

Thanks to others like Matt Alioto who posted about the open issue (which is labeled Product Vision 2019 so hopefully they knock it out this year).

Specific to Carlson Cole's question, this would work:

job_for_master_no_tags:
  stage: deploy
  script:
  - echo "Release to Staging"
  only:
  - master

job_for_master_tags_only:
  stage: deploy
  script:
  - echo "Release to Production"
  only:
  - tags
  except:
  - /^(?!master).+@/    # Ruby RegEx for anything not starting with 'master'
  • To see how this RegEx works check out https://rubular.com/r/1en2eblDzRP5Ha
  • I tested this on GitLab version 11.7.0 and it works
    • Note: If you try to use - /^(?!master).+/ (without the @) it doesn't work - learned that the hard way
Eric D. Johnson
  • 7,181
  • 6
  • 30
  • 41
9

I made it work and this is my working code snippet, all others were not working for me

only:
 - tags  # please mention the 's' compared to Sergio Tomasello's solution
except:
 - branches

I use 11.4.3

hannes ach
  • 8,318
  • 3
  • 39
  • 50
6

This behavior is not yet supported by gitlab-ci, although there is an open issue to add it.

In the meantime I've also heard anecdotal reports that

only:
  - master
only:
  - tags

will get the job done (as well as anecdotal reports that it won't).

bPratik
  • 6,428
  • 3
  • 31
  • 65
Matt Alioto
  • 349
  • 1
  • 10
  • I've tried that and it didn't seem to seem to make any difference. Thanks for the note on the open issue. – Carson Cole Mar 18 '17 at 17:48
  • The only other thing I can suggest is hacking something together with regular expressions like the users [here](https://github.com/gitlabhq/gitlab-ci/issues/639) are experimenting with. – Matt Alioto Mar 18 '17 at 17:54
  • I just tried the proposed solution and it is indeed working! – Ali Jun 30 '17 at 19:59
  • 2
    @MattAlioto too bad that link doesn't exist anymore, always try to include a brief example in case the original source goes away ;) – Jonas D. Jul 25 '17 at 07:51
  • @JonasD. This seems to be the link you're looking for: https://gitlab.com/gitlab-org/gitlab-ce/issues/27818 – Riga Aug 16 '17 at 20:53
  • 4
    Per https://gitlab.com/gitlab-org/gitlab-ce/issues/27818#note_38463332 second only just overrides the first. – bbodenmiller Jul 20 '18 at 04:18
0

My solutions was

job:
  script:
  - echo "Do something"
  only: 
    refs:
      - master
      - tags
    variables:
      - $CI_COMMIT_BRANCH == "master"
Julio Ramos
  • 61
  • 1
  • 3
  • Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now, please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – Ruli Nov 30 '20 at 22:03
0

There is no proper build in solution in gitlab so far for this problem. To keep track of the development of a proper solution and to keep a working workaround updated I created: Gitlab CI: Run Pipeline job only for tagged commits that exist on protected branches

Kound
  • 529
  • 3
  • 17