Closed5

GitHub Actions内でPRを作成する方法

なかやばしなかやばし

GitHub Actionsのworkflow内でgitを操作したり、Pull Requestを作成する方法がわからなかった&詰まったのでメモ。

例えば、Gitで管理しているデータをGitHub Actionsを使って定期的に更新したい場合に使える(かも?

なかやばしなかやばし

具体的には以下のようなworkflowを書く

jobs:
  update_data:
      ...
      - uses: peter-evans/create-pull-request@v7
        with:
          commit-message: "chore: updated json files" # commit msg
          title: "update json files" # PRのtitle
          branch: "feat/json-update" # 作成するbranch
          # 以下に追加するファイルを指定
          add-paths: | 
            static/*.json
なかやばしなかやばし

ただし、このままではActionsからpush、PR作成ができないので、以下のような権限設定をworkflowに書く必要がある

permissions:
  id-token: write
  contents: write
  pull-requests: write
なかやばしなかやばし

ただ、このままではActions実行中に Error: GitHub Actions is not permitted to create or approve pull requests. - https://docs.github.com/rest/pulls/pulls#create-a-pull-request と言われて落ちた。

issueを漁っていたところ、以下のようにリポジトリの設定をしたところ、うまく実行できた。

https://github.com/peter-evans/create-pull-request/issues/3979

このスクラップは4日前にクローズされました