[求助] github action: Permission to git denied to github-actions

2022-04-26 19:27:11 +08:00
 Doragd

各位 v 友好,我最近在学习 github actions, 但是不清楚为啥会在自动 push 的时候报错~ 就是 push 到当前 repo 的 main 分支的时候报错了 我也搜索了相关的 issue ,但是还是没法解决: https://github.com/ad-m/github-push-action/issues/96

特来求助各位呜呜呜

我的报错信息是:

Push to branch main
remote: Permission to ... denied to github-actions[bot].
fatal: unable to access '...': The requested URL returned error: 403
Error: Invalid exit code: 128
    at ChildProcess.<anonymous> (/home/runner/work/_actions/ad-m/github-push-action/master/start.js:29:21)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1022:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5) {
  code: 128
}
Error: Invalid exit code: 128
    at ChildProcess.<anonymous> (/home/runner/work/_actions/ad-m/github-push-action/master/start.js:29:21)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1022:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5)

配置文件如下:

name: learn

env:
  PYTHON_VERSION: '3.8'               # set this to the Python version to use

on:
  issues:
    types:
      - labeled

jobs:
  learning:
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - uses: actions/checkout@v2
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.
	  
      ...
          
      - name: Commit files
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git commit -m "Add changes" -a
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

2106 次点击
所在节点    问与答
12 条回复
imzcg2
2022-04-26 19:31:51 +08:00
GITHUB_TOKEN ,你配置了吗,不会配百度啊
Doragd
2022-04-26 19:35:57 +08:00
@imzcg2 我以为这个 GITHUB_TOKEN 是一个上下文变量来着呜呜呜,我懂了,是要去 secrets 里面添加是吧
ch2
2022-04-26 19:36:33 +08:00
试试用这个,首先生成一个 token 填到 settings->secrets 里,取名为 action_token
- name: GitHub push
run: |
git config --global user.email "js-deliver@qq.com"
git config --global user.name "js-deliver"
git config --global github.user js-deliver
git config --global github.token $GITHUB_TOKEN
git remote add publish https://你的用户名:$GITHUB_TOKEN@github.com/你的用户名 /你的仓库名 /.git
git checkout --orphan publish
git commit -m "Auto commit version $version"
git config -l | grep 'http\..*\.extraheader' | cut -d= -f1 | xargs -L1 git config --unset-all
git push --force https://你的用户名:$GITHUB_TOKEN@github.com/你的用户名 /你的仓库名 /.git publish:main
env:
GITHUB_TOKEN: ${{ secrets.action_token }}
imzcg2
2022-04-26 19:36:53 +08:00
@Doragd 随便百度一个教程都会教你这样做
Doragd
2022-04-26 19:42:08 +08:00
@imzcg2 谢谢您!!我现在明白了,之前确实自己没有去百度~用的谷歌,自己英文比较菜,没有很好地描述好问题,可能就没检索到,谢谢!
Doragd
2022-04-26 19:42:40 +08:00
@ch2 谢谢您!我试试啦~
Doragd
2022-04-26 19:49:52 +08:00
@imzcg2 @ch2 您刚才说的应该是 PAT ?我看到文档里面写到:At the start of each workflow run, GitHub automatically creates a unique GITHUB_TOKEN secret to use in your workflow. You can use the GITHUB_TOKEN to authenticate in a workflow run.
我想问问这不是自动创建的吗?
AoEiuV020CN
2022-04-26 19:55:30 +08:00
GITHUB_TOKEN 是有自动创建的,但有些限制,比如无法修改 workflows 相关,或者是有其他限制触发了,
Doragd
2022-04-26 20:01:41 +08:00
@AoEiuV020CN 谢谢您回复,我就是修改了 repo 的一些文件,然后 commit ,然后想 push 到这个 repo 来应用更改~这样也没有权限吗呜呜呜
Doragd
2022-04-26 20:12:44 +08:00
@AoEiuV020CN @ch2 @imzcg2 谢谢大家的帮忙,我发现是我自己忘记把 contents 的 permission 设置为 write 了~呜呜呜真抱歉给大家造成麻烦~(当然 GITHUB_TOKEN 确实是不用配置的~TUT
imzcg2
2022-04-26 20:15:05 +08:00
Doragd
2022-04-26 20:20:14 +08:00
@imzcg2 好滴!!谢谢您!我之前检查过这个是 read & write 权限,我以为您说的是让我自己去生成 github token~
最后发现是我自己写 yml 的时候忘记写这个了呜呜呜
```
permissions:
- contents: write
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/849423

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX