首页 > 解决方案 > 无法推送到 GitHub Action 中的受保护分支

问题描述

我创建了一个 GitHub 操作,以便创建一个新版本并将其发布给我们的 JS 存储库。它看起来与此相似

steps:
  # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
  - uses: actions/checkout@v2

  - uses: actions/setup-node@v1
    with:
      node-version: 12.18.3
      registry-url: https://npm.pkg.github.com/
      scope: '<redacted>'

  - name: Install Dependencies
    run: npm ci

  - name: Build
    run: npm run build

  - name: Bump Version & Push
    run: |
      git config --local user.email "<redacted>"
      git config --local user.name "<redacted>"
      npm version patch
      git push https://${{ secrets.KEY }}@github.com/<redacted> HEAD:master --follow-tags

我使用的 KEY 是我从我的帐户创建的个人访问令牌。我已经设置了 repo,以便我可以推送到 master 分支。当我使用访问令牌从我的机器尝试推送命令时,它可以正常工作。但是每次我在 GitHub Action 中看到这个

remote: error: GH006: Protected branch update failed for refs/heads/master.        
remote: error: You're not authorized to push to this branch. Visit https://docs.github.com/articles/about-protected-branches/ for more information.

我一直在绞尽脑汁想弄清楚这一点,我也想出主意。如果我删除分支保护,此操作可以正常工作。

标签: github-actions

解决方案


我认为这是因为actions/checkout. 它存储在extraheader配置选项中,该选项优先于您手动设置的凭据。

尝试不保留身份验证:

  - uses: actions/checkout@v2
    with:
      persist-credentials: false

或者:

  - uses: actions/checkout@v2
    with:
      token: ${{ secrets.KEY }}

我知道这一点是因为我过去曾遇到过覆盖此配置选项的问题。


推荐阅读