【问题标题】:Authorize bash to access GitHub protected branch授权 bash 访问 GitHub 保护的分支
【发布时间】:2020-04-23 13:45:10
【问题描述】:

这可能是一个愚蠢的问题,但经过两天的头撞后,我在这里 ????

我目前正在从事一项 GitHub Actions 工作,这将在某个时候将 git push 转移到受保护的分支(实际上是 master)。但是,一旦进入这一步,作业就会失败,并显示“You are not authorized to push”错误消息:

错误代码 GH006

在我的情况下,这是第一次发生,这意味着工人(CLI,bash)根本没有被授权。所以我去一个专用的 GitHub 帐户登录它,这就是我现在被困几天的地方。

那么,如何通过 CLI 登录 GitHub 帐户?


我尝试过的事情:

手动推送到master

这可以正常工作,因为授权是可以的,但它显然不是自动化的,这就是我所追求的。

几个细节:

  • 我使用的是 Windows 10,而作业设置为在 "ubuntu-18.04" 上运行;
  • 在执行git push 之前,我已从 Windows 凭据管理器中删除了所有与 GitHub 相关的登录,并通过 GitHub 登录对话框窗口重新设置它们。

在未经授权的凭据下手动推送失败,正如预期的那样。

git remote set-url origin "https://$username:$token@github.com/my/repo"

这似乎没有任何效果。我已经尝试过设置现有遥控器的 URL 和删除而不是添加具有不同 URL 的遥控器,这两种方法似乎工作(不工作)相同。

以下配置均无效:

steps:
  - run: git remote set-url origin "https://$username:$token@github.com/my/repo"
  - run: git push origin master
steps:
  - run: git remote remove origin
  - run: git remote add origin "https://$username:$token@github.com/my/repo"
  - run: git fetch origin --all # with and without this step
  - run: git push origin master

curl -u "$username:$token" https://api.github.com/user

这是suggested in the docs,它确实成功了,但登录不会持续到git push — 即使推送发生在同一步骤中。我怀疑,可能有一个与 cookie 相关的解决方案,但我不确定它们在非浏览器环境中是如何工作的。另外,我相信这个 API 是为不同的目的而设计的。

这两个配置都失败了:

steps:
  # separate processes
  - run: curl -u "$username:$token" https://api.github.com/user
  - run: git push origin master
steps:
  # same process
  - run: |
      curl -u "$username:$token" https://api.github.com/user
      git push origin master

【问题讨论】:

  • 我认为您无法使用 CLI 将其关闭?

标签: bash git authorization github-actions


【解决方案1】:

actions/checkout@v2 现在将在设置 token 输入时配置并保持身份验证。您不需要手动配置源 URL。

      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.PAT }}
      - name: Create a change
        run: echo "test" > test.txt
      - name: Commit change
        run: |
          git config --global user.name 'Your Name'
          git config --global user.email 'your-username@users.noreply.github.com'
          git add -A
          git commit -m "Add test file"
          git push

根据 GitHub 论坛上的 this comment,必须从 admin/org 所有者帐户创建 PAT。具有写入权限的协作者不足以推送到受保护的分支。

【讨论】:

  • 工作就像一个魅力,谢谢!一件事是给它一个具有写访问权限的 PAT 就足够了(用户在 org 中拥有“Writer”权限;他们的 PAT 的范围为repo_public;特别提到允许用户推送到受保护的分支) .
猜你喜欢
  • 1970-01-01
  • 2019-05-05
  • 2023-02-09
  • 2017-07-27
  • 1970-01-01
  • 2022-10-06
  • 1970-01-01
  • 2021-03-28
  • 1970-01-01
相关资源
最近更新 更多