How to run a gitlab-ci.yml job only on a tagged branch?

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.


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


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 😕

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


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).