V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Doragd
V2EX  ›  问与答

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

  •  
  •   Doragd · 2022-04-26 19:27:11 +08:00 · 2100 次点击
    这是一个创建于 720 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位 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 }}
    
    
    12 条回复    2022-04-26 20:20:14 +08:00
    imzcg2
        1
    imzcg2  
       2022-04-26 19:31:51 +08:00 via Android
    GITHUB_TOKEN ,你配置了吗,不会配百度啊
    Doragd
        2
    Doragd  
    OP
       2022-04-26 19:35:57 +08:00
    @imzcg2 我以为这个 GITHUB_TOKEN 是一个上下文变量来着呜呜呜,我懂了,是要去 secrets 里面添加是吧
    ch2
        3
    ch2  
       2022-04-26 19:36:33 +08:00
    试试用这个,首先生成一个 token 填到 settings->secrets 里,取名为 action_token
    - name: GitHub push
    run: |
    git config --global user.email "[email protected]"
    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://你的用户名:[email protected]/你的用户名 /你的仓库名 /.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://你的用户名:[email protected]/你的用户名 /你的仓库名 /.git publish:main
    env:
    GITHUB_TOKEN: ${{ secrets.action_token }}
    imzcg2
        4
    imzcg2  
       2022-04-26 19:36:53 +08:00 via Android
    @Doragd 随便百度一个教程都会教你这样做
    Doragd
        5
    Doragd  
    OP
       2022-04-26 19:42:08 +08:00
    @imzcg2 谢谢您!!我现在明白了,之前确实自己没有去百度~用的谷歌,自己英文比较菜,没有很好地描述好问题,可能就没检索到,谢谢!
    Doragd
        6
    Doragd  
    OP
       2022-04-26 19:42:40 +08:00
    @ch2 谢谢您!我试试啦~
    Doragd
        7
    Doragd  
    OP
       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
        8
    AoEiuV020CN  
       2022-04-26 19:55:30 +08:00 via Android
    GITHUB_TOKEN 是有自动创建的,但有些限制,比如无法修改 workflows 相关,或者是有其他限制触发了,
    Doragd
        9
    Doragd  
    OP
       2022-04-26 20:01:41 +08:00
    @AoEiuV020CN 谢谢您回复,我就是修改了 repo 的一些文件,然后 commit ,然后想 push 到这个 repo 来应用更改~这样也没有权限吗呜呜呜
    Doragd
        10
    Doragd  
    OP
       2022-04-26 20:12:44 +08:00
    @AoEiuV020CN @ch2 @imzcg2 谢谢大家的帮忙,我发现是我自己忘记把 contents 的 permission 设置为 write 了~呜呜呜真抱歉给大家造成麻烦~(当然 GITHUB_TOKEN 确实是不用配置的~TUT
    imzcg2
        11
    imzcg2  
       2022-04-26 20:15:05 +08:00 via Android
    Doragd
        12
    Doragd  
    OP
       2022-04-26 20:20:14 +08:00
    @imzcg2 好滴!!谢谢您!我之前检查过这个是 read & write 权限,我以为您说的是让我自己去生成 github token~
    最后发现是我自己写 yml 的时候忘记写这个了呜呜呜
    ```
    permissions:
    - contents: write
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5736 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 03:25 · PVG 11:25 · LAX 20:25 · JFK 23:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.