🌟

GitHub Actionsで特定ブランチのPull Requestを自動approve & mergeする

2022/10/10に公開

ブランチ名が foo/** のPull Requestだったらapprove & mergeするGitHub Actionsの例

name: approve & merge

on:
  pull_request:
    types:
      - opened

jobs:
  approve_merge:
    if:  startsWith(github.head_ref, 'foo/')
    runs-on: ubuntu-latest
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      PR_NUMBER: ${{ github.event.number }}
    steps:
      - uses: actions/checkout@v3
      - run: |
          gh pr review $PR_NUMBER --approve
          gh pr merge $PR_NUMBER --merge

特定ブランチでGitHub Actionsを起動させる方法は以下を参照

https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-your-workflow-based-on-the-head-or-base-branch-of-a-pull-request

approveとmergeはGitHub CLIを使う

https://cli.github.com/manual/gh_pr_review
https://cli.github.com/manual/gh_pr_merge

Discussion