简体   繁体   English

无法在 gitlab ci 管道中推送 git 标签

[英]unable to push git tags in gitlab ci pipeline

In my gitlab ci pipeline, I want to push a tag wherever pipeline run for the master branch.在我的 gitlab ci 管道中,我想在主分支运行管道的任何地方推送一个标签。 But the issue is that I am unable to push the tag on the repository.但问题是我无法将标签推送到存储库中。

I am trying to push the git tag using the GITLAB_TOKEN我正在尝试使用 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

But the problem is that when i try to push the tag i get this error但问题是当我尝试推送标签时出现此错误

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

We had the same problem, but using after_script or SSH did not satisfy our requirements.我们遇到了同样的问题,但是使用after_scriptSSH并不能满足我们的要求。

Here is our solution outline:这是我们的解决方案大纲:

  • Use OAuth2 and Personal Access Token in origin to enable write access from CI job在源中使用 OAuth2 和个人访问令牌来启用来自 CI 作业的写入访问
  • Use ci.skip git option to skip retriggering pipeline after pushing a tag使用 ci.skip git 选项在推送标签后跳过重新触发管道

Details:细节:

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

Our objective was to push a tag as part of build-rpm job, which should be initiated manually as part of multi-stage CI pipeline.我们的目标是将标签作为 build-rpm 作业的一部分推送,该作业应作为多阶段 CI 管道的一部分手动启动。

The pipeline can be started on schedule or manually.管道可以按计划手动启动。

It must be possible to generate unique tag from git commit timestamp.必须可以从 git 提交时间戳生成唯一标签。

Update Jun 2020: 2020 年 6 月更新:

A new due date for a GitLab feature to address pushing tags in pipeline is 22-Jul-2020: GitLab 功能解决管道中推送标签的新截止日期是 2020 年 7 月 22 日:

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

the above issue was due to the repo url on which i was trying to push the git tag .上述问题是由于我试图在其上推送git tag的 repo url 造成的。

The issue was fixed by adding .git extension in the repo url, the example is given below:该问题已通过在仓库 url 中添加.git扩展名得到解决,示例如下:

git remote set-url origin https://$USER_NAME:$GITLAB_TOKEN@${CI_PROJECT_URL:8}.git

In my related work, we used slightly different approach.在我的相关工作中,我们使用了稍微不同的方法。

create-git-tag:
  image: alpine/git
  stage: tagging
  script:
    - git config user.email "${GITLAB_USER_EMAIL}"
    - git config user.name "${GITLAB_USER_NAME}"
    - git remote add tag-origin https://oauth2:${GITLAB_ACCESS_TOKEN}@gitlab.com/${CI_PROJECT_PATH}
    - git tag -a "dev-1.0.1" -m "Dev Tag Created Automatically"
    - git push tag-origin "dev-1.0.1"
    - echo "Git Tag created successfully"
  rules:
    - if: '$CI_COMMIT_TAG == null'

Above job runs automatically as part of tagging stage.以上作业作为标记阶段的一部分自动运行。 It won't run when a GIT Tag is raised, otherwise it will go in infinite loop as the job itself is creating the Git Tag.当引发 GIT 标签时,它不会运行,否则它将 go 无限循环,因为作业本身正在创建 Git 标签。 This is just an example to show the git tagging using the oauth url of GitLab.这只是一个例子,使用 GitLab 的 oauth url 来显示 git 标记。

GITLAB_ACCESS_TOKEN is variable defined in Settings -> CI/CD -> Variables and value is the GitLab Toke (Created separately from Profile -> Access Token) You can also use Project Access Token (better approach than using personal token. https://docs.gitlab.com/ee/user/project/settings/project_access_tokens.html#bot-users-for-projects GITLAB_ACCESS_TOKEN 是在 Settings -> CI/CD -> Variables 中定义的变量,值为 GitLab Toke(与 Profile -> Access Token 分开创建)您还可以使用项目访问令牌(比使用个人令牌更好的方法。https://docs .gitlab.com/ee/user/project/settings/project_access_tokens.html#bot-users-for-projects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM