4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[2022年05月時点] [対策] GitHub-Actionでgit pushを実行すると、「The requested URL returned error: 403」が発生してPushに失敗する

Posted at

GitHub Action を使って、次のようにリポジトリ上の a.txt を更新して push を試みると、
エラー「The requested URL returned error: 403」が発生し、Push に失敗する.

このときの対策方法が見つけ難かったので、書き出しておく.

エラー発生時の状況

実行環境

GitHub-hosted runner

.github/workflows/gitpush.yml

name: push text file
on: push 
jobs:
  push_a:
    runs-on: ubuntu-latest
    steps:
      - name: Push a.txt
        run: |
          git init
          git remote add origin "https://$GITHUB_ACTOR:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY.git"
          git config --global user.email "mybot@example.com"
          git config --global user.name "mybot"
          git fetch
          git checkout master
          git branch --set-upstream-to=origin/master
          git pull
          date >> a.txt
          git add -A
          git commit -m "update a.txt"
          git push

Action 実行結果

git push origin master
remote: Permission to xxxx/yyyy.git denied to github-actions[bot].
fatal: unable to access 'https://github.com/xxxx/yyyy.git/': (見易さのために改行する)
The requested URL returned error: 403🛑
Error: Process completed with exit code 128.

 

対策

次の行頭「+」行のように追加してやると、Push に成功した.

.github/workflows/gitpush.yml

  (略)
  push_a:
+   permissions:
+     actions: write
+     checks: write
+     contents: write
+     deployments: write
+     issues: write
+     packages: write
+     pull-requests: write
+     repository-projects: write
+     security-events: write
+     statuses: write
    runs-on: ubuntu-latest
    steps:
      - name: Push a.txt
  (略)

 

該当ドキュメント

https://docs.github.com/ja/actions/using-jobs/assigning-permissions-to-jobs

 

以上

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?