簡體   English   中英

無法在 gitlab ci 管道中推送 git 標簽

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

在我的 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

我們遇到了同樣的問題,但是使用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 提交時間戳生成唯一標簽。

2020 年 6 月更新:

GitLab 功能解決管道中推送標簽的新截止日期是 2020 年 7 月 22 日:

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

上述問題是由於我試圖在其上推送git tag的 repo url 造成的。

該問題已通過在倉庫 url 中添加.git擴展名得到解決,示例如下:

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

在我的相關工作中,我們使用了稍微不同的方法。

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'

以上作業作為標記階段的一部分自動運行。 當引發 GIT 標簽時,它不會運行,否則它將 go 無限循環,因為作業本身正在創建 Git 標簽。 這只是一個例子,使用 GitLab 的 oauth url 來顯示 git 標記。

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