経緯
CI/CDを何か実装してみたくなったので、GitHub Actionsを使用してPR作成を自動化してみようと思い、実装した内容を共有します。
目的
- PRの作成を自動化することで、手動でPRを作成する手間を省く
実装内容
- .github/workflows/auto-pr.ymlの作成
GitHub Actionsを実行するための実行ファイルです。
developブランチにpushすると、mainブランチへのPRが作成されるようになっています。
ファイルの中身は以下です。
name: Auto PR on Push
on:
push:
branches:
- develop
jobs:
create-pull-request:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get commit message
id: commit
run: echo "msg=$(git log -1 --pretty=%s)" >> $GITHUB_OUTPUT
- name: Create Pull Request
run: |
gh pr create \
--base main \
--head ${{ github.ref_name }} \
--title "${{ steps.commit.outputs.msg }}" \
--body-file .github/pull_request_template.md \
--label "automated-pr" || echo "PR already exists."
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
--body-fileで、PRの本文内容は、.github/pull_request_template.mdを使用するようにしています。
- .github/pull_request_template.mdの作成
PRの本文内容を記載するファイルです。
terraformでawsリソースを管理するリポジトリなので、terraformに合わせたレビューチェックリストになっています。
ファイルの中身は以下です。
## 🤖 Auto Pull Request
This pull request was automatically created by GitHub Actions.
---
### Source Branch
<!-- branch name -->
### Target Branch
main
### Triggered By
<!-- actor -->
### Latest Commit
<!-- commit message -->
---
### Review Checklist
- [ ] Code review
- [ ] Terraform plan confirmed
- [ ] No sensitive information included
---
This PR was generated automatically.
以上です。