Closed7

GitHub Actionsでgit commit, git pushさせる方法

はるのんはるのん

Github actionsを使ってgit commit, git pushするとき

To https://github.com/ORG/REPO
 ! [remote rejected] release/v3.7.0 -> release/v3.7.0 (refusing to allow a GitHub App to create or update workflow `.github/workflows/deliver.yml` without `workflows` permission)
error: failed to push some refs to 'https://github.com/ORG/REPO'

https://github.com/orgs/community/discussions/35410

みたいなエラーになってしまったのでその解決法をメモ

はるのんはるのん
  1. Repository accessでRepositoryを選択する(個人の趣味なら全部選択していいかもだが一旦個別選択する)
  2. Repository permissionsを選ぶ(Actions→Read&Write、Contents→Read&Write、Metadata→Read-only、Workflows→Read&Write)
  3. Update押す
  4. 画面戻ってTokenが発行されるのでコピーするなどで控えておく
はるのんはるのん

GHAで入れたいRepositoryに遷移する

https://github.com/{ORG}/{REPOSITORY}/settings/secrets/actions
はるのんはるのん

Repository secretsでNew repository secretボタン押して、以下のように追加する

Name: WORKFLOW_TOKEN
Value: 先程コピーして控えたValue

はるのんはるのん

.github/workflows/xxx.yml

...


jobs:
  xxx:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
          token: ${{ secrets.WORKFLOW_TOKEN }}

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 18.x

      - name: Install dependencies
        run: npm ci

      - name: Commit changes
        run: |
          git config --global user.email "actions@github.com"
          git config --global user.name "GitHub Actions"
          git diff --quiet && git diff --staged --quiet || (git add . && git commit -m "Automated fixes by GitHub Actions" && git pull --rebase && git push origin HEAD:${{ github.head_ref }})

これで最初のエラーが無くなるはず

このスクラップは5ヶ月前にクローズされました