git - 如何将标签推送到 CI 中的分支?

标签 git gitlab bitrise

我想将手 Action 业添加到我的 pull 请求中,以在运行手 Action 业时标记我的源分支。该标签将触发我的 bitrise 配置的构建。

但是,当我尝试推送我的标签时,我遇到了这个问题。 注意:我尝试将标签推送到的分支不 protected 。

$ git checkout $CI_COMMIT_REF_NAME
Switched to a new branch 'feature/gitlab-ci'
Branch feature/gitlab-ci set up to track remote branch feature/gitlab-ci from origin.
$ git tag build-bitrise
$ git push --tags
remote: You are not allowed to upload code.
fatal: unable to access 'https://gitlab-ci-token:[MASKED]@gitlab.com/my-app/my-app.git/': The requested URL returned error: 403
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1

我的工作就是这样做:

    - git remote show origin
    - git fetch
    - git checkout $CI_COMMIT_REF_NAME
    - git tag build-bitrise
    - git push --tags

在我的“before_scripts”步骤中,我这样做:

before_script:
    # Install ssh-agent through openssh-client if not present
    - 'which ssh-agent || ( apt-get update -qy && apt-get install openssh-client -qqy )'
    # Add the private key to this user
    - eval $(ssh-agent -s) && ssh-add <(echo "$SSH_PRIVATE_KEY") && mkdir -p ~/.ssh
    # Config git to avoid first usage questions. Set the identity
    - git config --global user.email "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="670a1e4a1402041502134a020a060e0b27000a060e0b4904080a" rel="noreferrer noopener nofollow">[email protected]</a>" && git config --global user.name "Louis Lecocq"

其中 SSH_PRIVATE_KEY 是一个变量,它是我在 ENV 中的 GITLAB 配置文件的复制/粘贴。

感谢您的阅读和您的时间

最佳答案

我认为您当前的方法不起作用,因为它仍然使用 https 而不是 ssh 来根据错误执行 git tag消息,因此未使用您的 SSH_PRIVATE_KEY:

fatal: unable to access 'https://gitlab-ci-token:[MASKED]@gitlab.com/my-app/my-app.git/': The requested URL returned error: 403

在执行git push --tags之前,您可以通过手动更新git remote来使其工作(未经测试),即:

git remote set-url origin <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83e4eaf7c3e4eaf7efe2e1ade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>:my-group/my-app/my-app

使用 SSH_PRIVATE_KEY 的替代方法是使用 API key 。您可以通过 https://gitlab.com/-/profile/personal_access_tokens 的 API 访问创建个人访问 token ,然后将 key 添加到 CI/CD 变量,例如 API_KEY

然后在你的脚本部分,你可以有类似的东西:

script:
    - # something to do before pushing the tag
      # sometimes the remote might already exist (if using the same runner), let's just remove it and don't fail
    - git remote remove https-origin || true
      # add new https-origin remote which uses the API_KEY
    - git remote add https-origin https://gitlab-ci-token:${API_KEY}@gitlab.com/my-group/my-app.git
      # tag your build
    - git tag build-bitrise
      # push only the build-bitrise tag using the https-origin ref, and skip CI build
    - git push https-origin -o ci.skip refs/tags/build-bitrise

注意,建议对 API_KEY 使用机器人帐户,否则 API_KEY 将具有与您的用户相同的权限,并且可能会被能够在 CI/CD 变量等中看到 key 的其他维护者泄露。

关于git - 如何将标签推送到 CI 中的分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64857051/

相关文章:

ios - 未找到 Bitrise.io 方案

ios - FaSTLane 无法在 Bitrise 上找到配置文件

git - checkout 并重置后恢复未提交的工作

variables - 我们可以在 gitlab-ci.yml 中使用动态作业名称吗?

git - 如何在 git 提交损坏的网站中查找特定文件的代码?

运行 helm 命令时出现 Gitlab 问题 - 错误 : unknown command "sh" for "helm"

设置 SSH 后 Github 权限被拒绝(公钥)

android - assemble 与 assembleDebug 与 assembleRelease 之间的区别

git - 为什么 Git 创建一个没有文件更改的 merge 提交?

git - git@github.com :. .. 和 https ://github. com/... 之间的区别?