如何使用个人访问令牌从 CircleCI 构建推送到 Github [英] How to push a commit to Github from a CircleCI build using a personal access token

查看:14
本文介绍了如何使用个人访问令牌从 CircleCI 构建推送到 Github的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CircleCI 中为 git 存储库 giantswarm/docs-content 执行构建时,我想将提交推送到另一个存储库 giantswarm/docs.

When executing a build for git repository giantswarm/docs-content in CircleCI, I'd like to push a commit to another repository giantswarm/docs.

我在 circle.ymldeployment 部分有这个:

I have this in the deployment section of circle.yml:

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

失败在最后一条命令中出现此错误消息:

This fails in the very last command with this error message:

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

GITHUB_PERSONAL_TOKEN 环境变量设置为用户的个人访问令牌,该令牌已在 repo 范围内创建以访问私有 repo giantswarm/docs.此外,我将用户添加到对该 repo 具有管理员权限的团队.

The GITHUB_PERSONAL_TOKEN environment variable is set to a user's personal access token, which has been created with repo scope to access the private repo giantswarm/docs. In addition, I added the user to a team that has admin permissions for that repo.

当我在新的 Ubuntu VM 中执行这一系列命令时,它运行良好.知道为什么它不在 CircleCI 上吗?

That series of commands works just fine when I execute it in a fresh Ubuntu VM. Any idea why it doesn't on CircleCI?

推荐答案

感谢 Ali Amin 的提示我现在有这个可行的解决方案:

Thanks to the hint by Ali Amin I now have this working solution:

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 的 个人访问令牌 目标存储库的范围.
  • The git clone is first.
  • All subsequent git commands have to be executed in the clone directory. working_directory simplifies this a great deal.
  • The token DOCS_GITHUB_TOKEN is a personal access token with repo scope for the target repository.

这篇关于如何使用个人访问令牌从 CircleCI 构建推送到 Github的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆