Gitlab CI - $CI_COMMIT_TAG 为空

Gitlab CI - $CI_COMMIT_TAG is empty

构建时,我需要在 gitlab-ci.yml 中最后推送的 git 提交的标签值。在构建过程中,我构建了一个 docker 图像,在构建之后,我想推送这个图像,并使用与我的 git 提交相同的标签进行标记。到目前为止,我的理解是环境变量 $CI_COMMIT_TAG 应该完成这项工作。然而,当我在 gitlab-ci.yml 中回显 $CI_COMMIT_TAG 时,它只是空的吗?

这是我的 gitlab-ci.yml:

    stages:
      - build
    
    build-dev:
      stage: build
      environment: development
      only:
        - master
      tags:
        - ms-doorman
      script:
        - echo $CI_COMMIT_TAG

此处 git 命令启动作业。

$ git commit -am "test git tags"
$ git tag test-tag
$ git push --tags origin master

我在 Gitlab 中找到了一个很好的 issue,它很好地描述了这种行为:

When you will push a commit to GitLab, then it will start a pipeline without CI_BUILD_TAG variable. When you make a tag on this commit and push this tag to GitLab, then another pipeline (this time for the tag, not for the commit) will be started. In that case CI_BUILD_TAG will be present.

也许您可以使用 workflow:rules 来避免错误。

only on tags:
  rules:
    - if: '$CI_COMMIT_TAG != null'
  script:
    - echo only on tags
    - env