如何只在带标记的分支上运行 gitlab-ci. yml 作业?

如何只在带标记的 Master 分支上运行. gitlab-ci. yml 作业?

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

如果存在以下任一条件,上面的代码都将运行: 一个 Master 分支或一个带标记的提交。

我的目标是在生产部署中运行这个命令,但是它需要在 Master 分支上运行,并且需要对其进行标记(使用版本)。否则,我将有另一个作业,如果它丢失了一个标记,它将推送到暂存。

107499 次浏览

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

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

I made it work with this 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

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 😕

My solutions was

job:
script:
- echo "Do something"
only:
refs:
- master
- tags
variables:
- $CI_COMMIT_BRANCH == "master"

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

I have faced the same issue, this is how I tried to solve it

my_job:
stage: build
services:
- name: docker:dind
image: docker:latest
script:
- ...
rules:
- if: $CI_COMMIT_BRANCH == 'master' && $CI_COMMIT_TAG == null
cache: {}

this job runs only when there is a commit on the master branch (excluding any other commit on personal/feature branch). In the very same way I trigger builds on tags:

  script:
- ...
    

rules:
- if: $CI_COMMIT_BRANCH == 'master' && $CI_COMMIT_TAG != null

I had the same problem. I wanted to trigger a deploy to our staging-environment on a push or merge, and only when applying a tag deploy it our production-environment.

We need 2 variables for this: $CI_COMMIT_BRANCH and $CI_COMMIT_TAG. With these variables we could deduct if the pipeline was triggered by a commit or a tag. Unfortunately the first variable is only set when committing on a branch, while the second variable only is set on applying a Tag. So this was no solution...

So I went for the next-best setup by only do a production-release when a tag is set by specified conventions and by a manual trigger. This is my .gitlab-ci.yml file:

stages:
- deploy:staging
- deploy:prod


deploy-to-staging:
stage: deploy:staging
rules:
- if: $CI_COMMIT_BRANCH == 'master'
script:
- echo "Deploying to Staging..."


deploy-to-production:
stage: deploy:prod
rules:
- if: $CI_COMMIT_TAG =~ /^v(?:\d+.){2}(?:\d+)$/
when: manual
script:
- echo "Deploying to Production..."

If you really want to automate this, you have to do a little scripting to find out if the applied tag actually belongs to a commit that is on the master-branch. Check this comment on the GitLab issuetracker for more information: https://gitlab.com/gitlab-org/gitlab-foss/-/issues/31305#note_28580169

Years later, still trying to launch a job on tags on master branch...

The issue at Gitlab has been closed : https://gitlab.com/gitlab-org/gitlab-foss/-/issues/27818

It's not possible to spot a tag on master branch as Git does not work this way. Branches and tags are separate references, each pointing to a commit. So, a tag has no relation with a branch.

My solution is to check the tag name to detect that it represents a PRODUCTION release :

deploy-prod:
stage: deploy-manual
only:
variables:
- $CI_COMMIT_TAG =~ /^v\d+.\d+.\d+-?.*$/
when: manual

The regexp matches semver tag names like :

  • v1.2.0
  • v2.0.0-beta.1
  • ...

Docs actually say that only and except are not being developed and that users should use rules instead.
Here is an example which will only run on a tag.

deploy-to-prod:
stage: deploy
rules:
- if: $CI_COMMIT_TAG
environment:
name: production
variables:
API_ENVIRONMENT: prod
when: manual