首页 > 解决方案 > 无法在 gitlab ci 管道中推送 git 标签

问题描述

在我的 gitlab ci 管道中,我想在为主分支运行的管道中推送一个标签。但问题是我无法将标签推送到存储库上。

我正在尝试使用 GITLAB_TOKEN 推送 git 标签

image:
  name: X
  entrypoint: [""]


stages:
  - deploy
deploy:
  stage: deploy

  script:
    #  Generating new tag version using stk utility
    - git config --global user.email $USER_MAIL
    - git config --global user.name $USER_NAME
    - git config --global http.postBuffer 52428800
    - git remote set-url origin https://$USER_NAME:$GITLAB_TOKEN@${CI_PROJECT_URL:8}

    - export NEW_TAG_VERSION=<generating new git tag>
    - echo $NEW_TAG_VERSION

    - if [ $CI_COMMIT_REF_NAME == "master" ]; then \
    -       git tag -a v$NEW_TAG_VERSION -m "[skip ci] new tag"; \
    -       git tag --list; \
    -       git push origin --tags; \
    # I have also tried the command given below
    # -       git push origin HEAD:$CI_COMMIT_REF_NAME v$NEW_TAG_VERSION; \

    - else \
    -       echo "non-master"; \
    - fi

但问题是当我尝试推送标签时出现此错误

error: RPC failed; result=22, HTTP code = 404
fatal: The remote end hung up unexpectedly
fatal: The remote end hung up unexpectedly

标签: gitgitlabgitlab-ci

解决方案


我们遇到了同样的问题,但是使用after_scriptSSH并不能满足我们的要求。

这是我们的解决方案大纲:

  • 在源中使用 OAuth2 和个人访问令牌来启用来自 CI 作业的写入访问
  • 推送标签后使用 ci.skip git 选项跳过重新触发管道

细节:

image: alpine

stages:
  - build
  - test
  - package

make:
  stage: build
  script:
    - env
    - echo "complete" > complete.txt
  artifacts:
    paths:
      - complete.txt

  # All dependent jobs must include conditions of the parent job to
  # avoid pipeline entry with gitlab-ci.yml error upon code commit.
  #
  only:
    - schedules

test1:
  stage: test
  script:
    - echo "test1"
  needs:
    - job: make
      artifacts: true
  only:
    - schedules

# Other tests follow test1 structure

build_rpms:
  stage: package
  script:
    - echo "Build RPMs. Add tag v1.9d"
    - apk add git
    - git config --list

    # --force is needed for both tag and push to allow job replay
    - git tag v1.9d --force

    # Enable pushing from CI pipeline:
    #
    # At that point git origin points to CI_REPOSITORY_URL=
    # https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/acme/my-project.git
    # This setup does not allow modifications (i.e git push will be rejected).
    #
    # Setting SSH environment is what many developers do to execute git commands here,
    # but it is complex and requires submitting SSH private keys to Gitlab (cringe).
    #
    # Private Gitlab tokens are deprecated.
    #
    # We use Gitlab Personal Access Token with 'write' access. This token shall
    # be generated via Gitlab user settings and then it shall be added as a masked
    # environment variable for this project CI settings.
    #
    # Use "oauth2" as user. For example for CI_PROJECT_URL=https://gitlab.com/acme/my-project
    #   set origin to https://oauth2:wSHnMvSmYXtTfXtqRMxs@gitlab.com/acme/my-project.git
    #
    - git remote set-url origin ${CI_PROJECT_URL/gitlab.com/oauth2:${PERSONAL_ACCESS_TOKEN}@gitlab.com}.git
    - git remote -v

    # Don't trigger pipeline again:
    # -o ci.skip is not well known Gitlab Git option which allows skipping new CI.
    # Without ci.skip option CI would be triggered recursively by tag push.
    #
    - git push origin v1.9d --force -o ci.skip
  when:
    manual

我们的目标是将标签作为 build-rpm 作业的一部分推送,该作业应作为多阶段 CI 管道的一部分手动启动。

管道可以按计划手动启动。

必须可以从 git commit 时间戳生成唯一标签。

2020 年 6 月更新:

GitLab 功能解决管道中推送标签的新截止日期是 2020 年 7 月 22 日:

https://gitlab.com/gitlab-org/gitlab/-/issues/16290#note_357065731


推荐阅读