如何在具有写入存储库权限的运行器中设置 gitlab CI 令牌

How to set a gitlab CI token in runner with rights to write to repository

我想标记一个构建,当我尝试推送标记时,我在 CI

中收到以下错误

如何创建 CI 令牌并在可以将标签推送到存储库的构建环境中设置。

[01:59:14]: Exit status of command 'git push origin --tags' was 128 instead of 0.
remote: You are not allowed to upload code.
fatal: unable to access 'https://gitlab-ci-token:[MASKED]@gxx.yy.zz.git/': The requested URL returned error: 403

您可以执行以下两个选项:

  1. 使用具有 write_repository 权限的 personal access token

    • 将其另存为 custom CI/CD Variable 并确保它是 masked
    • 在您的 .gitlab-ci.yml 文件中使用自定义 CI/CD 变量:
    script:
      - git remote add https-origin https://gitlab-ci-token:${YOUR_PERSONAL_TOKEN}@gitlab.com/group/sub-group/project.git
      - git tag <some tag>
      - git push https-origin -o ci.skip refs/tags/<some tag>
    

请注意 -o ci.skip 不启动新管道,但这取决于您的情况。

如果你创建了一个机器人账户,这个选项肯定更好,这样你就可以更好地控制机器人账户可以访问哪些存储库,否则,任何维护者或更高级别的维护者都可以轻松地从查找中检索到 write_repository 密钥在设置中。


  1. 如果您可以通过 tags 访问构建 运行 的特定运行程序,您可以使用 Deploy Keys,这样可以节省您使用机器人帐户或您自己的个人访问令牌。

    • 这需要在 gitlab-runner 机器上创建一个 SSH-key,并将其复制到存储库 Settings -> Repository -> Deploy Keys 并在其中粘贴 public 密钥(以及滴答作响 Write access allowed).
    • 然后您应该可以像以前一样使用标准 git push origin --tags 命令。

GitLab 正在寻求改善 Epic 中的权限问题:

write_repository 使用管道令牌的具体问题是:

正如 Rekovni 所说,首先,你应该创建个人访问令牌,我使用 ********************* 来引用你的个人访问 token.Then 转到 setting -> CI/CD -> Variables,将 ********************* 添加到变量,设置键名为YOUR_PERSONAL_TOKEN.

Type Key Value Protected Masked Environments
Variable YOUR_PERSONAL_TOKEN ********************* × All (default)

在脚本中设置 git 远程 url:

  script:
    - CI_PUSH_REPO=`echo "$CI_REPOSITORY_URL" | sed 's/^.*@/@/g'`
    - git remote set-url --push origin "https://gitlab-ci-token:${YOUR_PERSONAL_TOKEN}$CI_PUSH_REPO"
#   - git push xxx

对我有用