Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create release tag and release note. | |
on: | |
pull_request: | |
types: [closed] | |
branches: | |
- main | |
jobs: | |
create-release-tag: | |
# PRがマージされたときのみ実行 | |
if: github.event.pull_request.merged == true | |
runs-on: ubuntu-latest | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
TZ: "Asia/Tokyo" | |
steps: | |
- uses: actions/checkout@v4 | |
# 前回のリリースタグを取得する | |
- name: Get previous tag | |
id: pre_tag | |
run: | | |
echo "pre_tag=$(curl -H 'Accept: application/vnd.github.v3+json' -H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .tag_name)" >> $GITHUB_OUTPUT | |
# タグを生成する 「{YYYY.MM.DD}-{当日リリース回数}」 | |
- name: Generate tag | |
id: release_tag | |
run: | | |
today=$(date +'%Y.%m.%d') | |
pre_release_date=$(echo ${{ steps.pre_tag.outputs.pre_tag }} | awk -F'-' '{print $1}') | |
pre_release_count=$(echo ${{ steps.pre_tag.outputs.pre_tag }} | awk -F'-' '{print $2}') | |
if [[ ! $pre_release_date = $today ]]; then | |
pre_release_count=0 | |
fi | |
echo "release_tag=$today-$(($pre_release_count + 1))" >> $GITHUB_OUTPUT | |
# PRのDescriptionを取得しマークダウン形式に変換する | |
- name: Get pr description | |
id: pr_description | |
run: | | |
echo "pr_description=$(curl -H 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \ | |
'https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number}}' \ | |
| jq .body | awk '{if ($0 == "null") print ""; else print}')" >> $GITHUB_OUTPUT | |
# 前回リリースからの差分をもとに、変更点を取得する | |
- name: Generate release note changes | |
id: changes | |
run: | | |
echo "changes=$( | |
curl -X POST \ | |
-H 'Accept: application/vnd.github.v3+json' \ | |
-H 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \ | |
https://api.github.com/repos/${{ github.repository }}/releases/generate-notes \ | |
-d '{ | |
"tag_name": "${{ steps.release_tag.outputs.release_tag }}", | |
"previous_tag_name": "${{ steps.pre_tag.outputs.pre_tag }}", | |
"target_commitish": "main" | |
}' | jq .body | |
)" >> $GITHUB_OUTPUT | |
# リリースノートの本文を作成する | |
- name: Create release note body | |
id: release_note_body | |
run: | | |
echo "release_note_body=$(echo \ | |
${{ steps.pr_description.outputs.pr_description }} \ | |
${{ steps.changes.outputs.changes }} \ | |
| sed 's/\\"//g' | sed 's/["“]//g')" >> $GITHUB_OUTPUT | |
# タグを切り、リリースノートを作成する | |
# PRのラベルに応じてカテゴライズする場合は、使用先のリポジトリで下記を定義する | |
# https://docs.github.com/ja/repositories/releasing-projects-on-github/automatically-generated-release-notes#configuring-automatically-generated-release-notes | |
- name: Create Release | |
run: | | |
response=$(curl -X POST \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-d "{ \ | |
\"tag_name\": \"${{ steps.release_tag.outputs.release_tag }}\", \ | |
\"target_commitish\": \"main\", \ | |
\"name\": \"${{ steps.release_tag.outputs.release_tag }}\", \ | |
\"body\": \"${{ steps.release_note_body.outputs.release_note_body }}\" \ | |
}" \ | |
-w "%{http_code}" \ | |
-o response_body.txt \ | |
https://api.github.com/repos/${{ github.repository }}/releases) | |
status_code=$(tail -n1 <<< "$response") | |
echo "Status Code: $status_code" | |
body=$(cat response_body.txt) | |
echo "Response Body: $body" | |
if [ $status_code -ne 201 ]; then | |
echo "Failed to create release" | |
exit 1 | |
fi |