如何使用个人访问令牌从CircleCI构建将提交推送到Github

19
当在CircleCI中为git存储库“giantswarm/docs-content”执行构建时,我希望将提交推送到另一个存储库“giantswarm/docs”。 我在“circle.yml”的“deployment”部分中有以下内容:
git config credential.helper cache
git config user.email "<some verified email>"
git config user.name "Github Bot"
git clone --depth 1 https://${GITHUB_PERSONAL_TOKEN}:x-oauth-basic@github.com/giantswarm/docs.git
cd docs/
git commit --allow-empty -m "Trigger build and publishing via docs-content"
git push -u origin master

这个命令在最后一步失败,并显示以下错误消息:

ERROR: The key you are authenticating with has been marked as read only.
fatal: Could not read from remote repository.

GITHUB_PERSONAL_TOKEN 环境变量被设置为用户的个人访问令牌,该令牌已使用 repo 范围创建,以访问私有存储库 giantswarm/docs。此外,我将用户添加到一个团队,该团队具有该存储库的管理权限。

在新的 Ubuntu VM 中执行该系列命令时,一切都工作正常。不知道为什么在 CircleCI 上却不能工作?


@FelicianoTech 我也试过了,是的。但在 git clone 过程中出现错误:ERROR: Repository not found. fatal: Could not read from remote repository. - Marian
你使用了GitHub UI中提供的SSH地址来访问仓库吗? - FelicianoTech
@FelicianoTech 是的,我使用了地址 git@github.com:giantswarm/docs.git 以SSH方式。 - Marian
更新:其他Travis CI用户报告了与我相同的问题(#8680#8686),但我仍然不确定解决方法是什么。 - Todd Owen
更新:我现在意识到 git push 实际上是使用 SSH(因为 Travis 最初克隆了仓库),而不是我之前认为的 HTTPS。因此,个人访问令牌甚至都没有被使用。 - Todd Owen
显示剩余2条评论
3个回答

21
我已使用。
git push -q https://${GITHUB_PERSONAL_TOKEN}@github.com/<user>/<repo>.git master

然后它就可以工作了。 将其更新为:

# Push changes
git config credential.helper 'cache --timeout=120'
git config user.email "<email>"
git config user.name "<user-name>"
git add .
git commit -m "Update via CircleCI"
# Push quietly to prevent showing the token in log
git push -q https://${GITHUB_PERSONAL_TOKEN}@github.com/giantswarm/docs.git master

1
谢谢!这帮助我解决了我的问题,不过我不得不稍微改动一下。请参见答案 https://dev59.com/A1cP5IYBdhLWcg3wLnRC#48763205 - Marian
1
谢谢,当运行 git push 时,您可以使用 ${CIRCLE_BRANCH} - Effi Bar-She'an
1
请注意,即使出现错误,仍会记录您的令牌。 - Corbfon
1
如果使用SSH而不是HTTPS,这会是什么样子? - Turnipdabeets
@Corbfon 通过在你的 git push 命令末尾添加 > /dev/null 2>&1 来摆脱错误日志。在这里了解更多相关信息。 - Paul Razvan Berg

15

多亏了Ali Amin的提示,我现在有了这个可行的解决方案:

version: 2
jobs:
  build:
    machine: true
    steps:
      - run:
          name: Clone docs
          working_directory: ~/workdir
          command: |
            git clone --depth 1 https://${DOCS_GITHUB_TOKEN}@github.com/giantswarm/docs.git
      - deploy:
          name: Trigger docs deployment
          working_directory: ~/workdir/docs
          command: |
            git config credential.helper 'cache --timeout=120'
            git config user.email "<email>"
            git config user.name "Deployment Bot"
            git commit --allow-empty -m "Trigger deployment"
            # Push quietly to prevent showing the token in log
            git push -q https://${DOCS_GITHUB_TOKEN}@github.com/giantswarm/docs.git master

一些注意事项:

  • git clone是第一步。
  • 所有后续的git命令必须在克隆目录中执行。working_directory可以大大简化此过程。
  • 令牌DOCS_GITHUB_TOKEN是一个具有目标存储库repo范围的个人访问令牌

2
你如何为特定的仓库创建个人访问令牌?我无法在任何地方找到该选项。 - FredyC
1
@FredyC 你解决了吗?如果没有,你可以按照这里的步骤进行操作:https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line。 - msamprz

2
尽管将令牌嵌入命令对于此案例有效,但可能并不适用于所有情况,并且不能回答问题。
其他情况包括不公开直接访问git命令的脚本。它们依赖于设置了GH_TOKEN变量,您无法像示例中那样注入它。
这并没有回答问题:
“为什么在CircleCI上不起作用?”
在CircleCI支持论坛上有一个关于此的答案。

https://support.circleci.com/hc/en-us/articles/360018860473-How-to-push-a-commit-back-to-the-same-repository-as-part-of-the-CircleCI-job

运行git push时会出现“错误:您正在进行身份验证的密钥已被标记为只读”。默认情况下,当您在CircleCI上添加项目时,该项目配置的部署密钥仅具有读取访问权限,因此需要配置具有写入权限的密钥以避免上述错误消息。请确保为项目配置了用户密钥或读写部署密钥。

https://circleci.com/docs/2.0/gh-bb-integration/#creating-a-github-deploy-key

经过这个过程,你应该拥有一个具有写入权限的部署密钥,允许推送操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接