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

2020-05-15 08:41发布

问题:

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.

回答1:

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



回答2:

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