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

GitHub Actionsのworkflow内でgitを操作したり、Pull Requestを作成する方法がわからなかった&詰まったのでメモ。
例えば、Gitで管理しているデータをGitHub Actionsを使って定期的に更新したい場合に使える(かも?

workflow内でPRの作成をする場合には以下のactionsが使える
ちなみにpeter-evans/create-pull-request@v7
ではbranchの作成、git commit、pushまで行うことができる。

具体的には以下のような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を漁っていたところ、以下のようにリポジトリの設定をしたところ、うまく実行できた。
このスクラップは4日前にクローズされました