LoginSignup
11
4

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

Last updated at Posted at 2023-04-21

はじめに

GitHub Actions で処理を実行した後に diff が生じたら Pull request を作成する方法を調べて検証していたのでまとめました。

GitHub Actions 実行後に diff が生じたら Pull request を作成する

利用する action

Pull request を作成するために create-pull-request action を利用します。

create-pull-request action 以外にも様々 action は存在していますが、
diff があれば commit → pull request 作成まで行ってくれるのは create-pull-request action がシンプルかつ簡単に実現できました。

作成する yml

今回は、以下条件等を実現する Yaml を作成します

  • 実行条件
    • スケジュール実行: cron "0 0 * * 6" (土曜日 00:00 UTC)
    • 手動実行
  • 権限
    • contents: write
    • pull-requests: write
  • ジョブ定義
    • 実行環境: ubuntu-latest
    • 実行内容
      • 最新の default branch のコードを取得する
      • Node.js の install
      • yarn install を実行する
      • yarn run xxxx を実行する
      • peter-evans/create-pull-request を利用し Pull request を作成する
        • Pull request の title や commit メッセージ、 reviewer の定義は必要であれば行う
.github/workflows/create_pr.yml
name: Create a pull request if there is a difference

on:
  schedule:
    - cron: "0 0 * * 6"
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  create_pr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: main
      - uses: actions/setup-node@v3
        with:
          cache: "yarn"
      - run: yarn install
      - run: yarn run xxxx
      - uses: peter-evans/create-pull-request@v5
        with:
          commit-message: Update xxxx file
          delete-branch: true
          title: Update xxxx file
          reviewers: mziyut

実行結果

定義した yml が実行され diff が生じている場合は ↓ のような Pull request が作成されます。

create_pr .png

最後に

今回は Node.js の実行結果を対象にしていますが、 Node.js 以外でも Git に diff が生じていれば利用出来ます。
gh コマンドなどを利用し、他 repository から差分を持ってきたり等の活用も出来そうです。

References

11
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
4