GitHub Actionsからpushする際に権限でエラーが出た場合に確認するポイント【GitHub Actions】

初めに

個人で自動化する際にGitHub Actionsを使用しています(*1)。動かす際に権限周りでハマったので,メモしておきます

(*1) GitHub Actionsをサーバーレスで使うことは,以下の範囲であれば2023/2/4時点での利用規約上大丈夫みたいです。 使い過ぎには気をつけましょう

当社のサーバーに負担をかける行為で、その負担がユーザーに提供する利益と不釣り合いなもの(例えば、コンテンツ配信ネットワークやサーバーレスアプリケーションの一部としてActionsを使用しないこと、ただし、低負荷であれば低利益のActionでもよい)

docs.github.com

実行内容

まず処理内容ですが,以下のような処理を 特定の GitHub Actionsフロー(yml)内で行っています。普通にcommitしてpushするだけです。

      - name: Commit And Push contents info file
        run: |
          git remote set-url origin https://github-actions:${GITHUB_TOKEN}@github.com/~
          git config --global user.name "name"
          git config --global user.email "email" 
          git add コミットするファイル
          git commit -m "Update file"
          git push
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

エラー内容

上記の処理を実行した際に以下のようなエラーが表示されていました。

remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/~': The requested URL returned error: 403
Error: Process completed with exit code 128.

対応方法

Setting → Actions → General → Workflow permissions から Actionsの権限を Read and Write に変更します