高木のブログ

【GitHub Actions】create-pull-request を試す

· 93 words · 1 minutes to read

差分があればコミットしてプルリクエストを作ってくれるアクション、create-pull-request を見つけたので試してみた

使い方 🔗

date コマンドの結果を保存した date.txt ファイルがあり、それを GitHub Actions で更新して、プルリクエストを作成するというワークフローを作るとする

リポジトリの設定で GitHub Actions でプルリクエストを作成する権限を付与する 🔗

Settings -> Actions -> General -> Workflow permissions の Allow GitHub Actions to create and approve pull requests にチェックを付けて保存する

ワークフローファイルを作成する 🔗

name: Update date.txt

on:
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  create_pr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: date > date.txt
      - uses: peter-evans/create-pull-request@v5
        with:
          commit-message: Update date.txt
          delete-branch: true
          title: Update date.txt

最低限の設定はこんな感じ

実行結果 🔗

001.png https://github.com/ytkg/create-pull-request-sample/pull/3

ちゃんとプルリクエストが作成された

更新されるか 🔗

オープンされたプルリクエストをマージする前に、ワークフローが実行された時の挙動を確認
新しくプルリクエストを作成しないで、そのプルリクエストを更新して欲しい

002.png https://github.com/ytkg/create-pull-request-sample/pull/5

新しいコミットが作られ、フォースプッシュされていた

参考 🔗

GitHub Actions の job で処理を実行した後に diff が生じていたら Pull request を作成する #GitHub - Qiita

Categories


Tags