如何为分支主机上的标记提交触发 GitLab 管道?

How to trigger a GitLab pipeline for a tagged commit on branch master?

我想在将标记的提交推送到分支主机后触发管道。虽然,我不确定它是否有效,因为到目前为止我没有成功。

.gitlab-ci.yml:

variables:
  JEKYLL_ENV: production
  LC_ALL: C.UTF-8

stages:
  - publish
  - release
  # - deploy

pages:
  stage: publish
  image: ruby
  rules:
    - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG != ""
      when: always
  before_script:
    - gem install bundler
    - bundle install
  script:
    - bundle exec jekyll build -d public
  artifacts:
    paths:
    - public

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  rules:
    - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG != ""
      when: always
  script:
    - echo 'running release job'
  release:
    name: 'Release $CI_COMMIT_TAG'
    description: 'Created using gitlab-ci.yml. $EXTRA_DESCRIPTION'
    tag_name: '$CI_COMMIT_TAG' 
    ref: '$CI_COMMIT_TAG'
  artifacts:
    paths:
    - public
    expire_in: 1 day

我尝试过的:

`- if: '$CI_COMMIT_TAG && $CI_COMMIT_BRANCH == "master"'`

不过,最后一条规则根本不会触发管道。

您可以检查提交是否有标签

rules:
  - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG != ""
    when: manual

或者如果您的代码遵循 tag-1tag-2 等约定,请使用正则表达式模式

rules:
  - if: $CI_COMMIT_BRANCH == "master" && $CI_COMMIT_TAG =~ /tag\-[0-9]/
    when: manual