首页 > 解决方案 > Gitlab CI - $CI_COMMIT_TAG 为空

问题描述

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

这是我的 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-cigitlab-ci-runner

解决方案


我在 Gitlab 中发现了一个很好的问题,它很好地描述了这种行为:

当您将提交推送到 GitLab 时,它将启动一个没有 CI_BUILD_TAG 变量的管道。当您在此提交上创建标签并将此标签推送到 GitLab 时,将启动另一个管道(这次是针对标签,而不是针对提交)。在这种情况下 CI_BUILD_TAG 将出现。

也许您可以使用工作流:规则来避免错误。

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


推荐阅读