|
| 1 | +# ワークフローの名前 |
| 2 | +name: Semantic Release on Merge |
| 3 | + |
| 4 | +# ワークフローが実行されるトリガー |
| 5 | +on: |
| 6 | + pull_request: |
| 7 | + types: |
| 8 | + - closed # PRがクローズされた時 |
| 9 | + |
| 10 | +# 実行されるジョブを定義 |
| 11 | +jobs: |
| 12 | + release: |
| 13 | + # PRがマージされ、かつ対象ブランチがmasterの場合のみ実行 |
| 14 | + if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'master' |
| 15 | + runs-on: ubuntu-latest |
| 16 | + # contents: write はコミット&プッシュとリリース作成に必要 |
| 17 | + permissions: |
| 18 | + contents: write |
| 19 | + pull-requests: read |
| 20 | + |
| 21 | + steps: |
| 22 | + # 1. リポジトリのコードをチェックアウトする |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + # 2. 最新のセマンティックバージョンのタグを取得する |
| 30 | + - name: Get previous tag |
| 31 | + id: previous_tag |
| 32 | + uses: WyriHaximus/github-action-get-previous-tag@v1 |
| 33 | + with: |
| 34 | + fallback: v0.0.0 # タグが一つも無い場合は v0.0.0 とする |
| 35 | + |
| 36 | + # 3. PRのラベルからバージョンアップのレベルを決定する |
| 37 | + - name: Determine version bump |
| 38 | + id: semver_bump |
| 39 | + run: | |
| 40 | + labels='${{ toJSON(github.event.pull_request.labels.*.name) }}' |
| 41 | + if echo "$labels" | grep -q -e "new feature" -e "enhancement"; then |
| 42 | + echo "level=minor" >> $GITHUB_OUTPUT |
| 43 | + echo "run_release=true" >> $GITHUB_OUTPUT |
| 44 | + elif echo "$labels" | grep -q -e "bug" -e "developer update"; then |
| 45 | + echo "level=patch" >> $GITHUB_OUTPUT |
| 46 | + echo "run_release=true" >> $GITHUB_OUTPUT |
| 47 | + else |
| 48 | + echo "run_release=false" >> $GITHUB_OUTPUT |
| 49 | + echo "No relevant label found. Skipping release." |
| 50 | + fi |
| 51 | +
|
| 52 | + # 4. 新しいバージョン番号を計算する |
| 53 | + - name: Calculate new version |
| 54 | + if: steps.semver_bump.outputs.run_release == 'true' |
| 55 | + id: new_version |
| 56 | + run: | |
| 57 | + npm install semver |
| 58 | + new_tag=$(node -e "console.log('v' + require('semver').inc('${{ steps.previous_tag.outputs.tag }}', '${{ steps.semver_bump.outputs.level }}'))") |
| 59 | + echo "tag=$new_tag" >> $GITHUB_OUTPUT |
| 60 | + echo "version_number=$(echo $new_tag | sed 's/v//')" >> $GITHUB_OUTPUT |
| 61 | + echo "New tag is: $new_tag" |
| 62 | +
|
| 63 | + # 5. config/version.php ファイルを更新する |
| 64 | + - name: Update version file |
| 65 | + if: steps.semver_bump.outputs.run_release == 'true' |
| 66 | + run: | |
| 67 | + sed -i "s/'cc_version' => '.*',/'cc_version' => '${{ steps.new_version.outputs.version_number }}',/" config/version.php |
| 68 | + echo "Updated config/version.php" |
| 69 | + cat config/version.php |
| 70 | +
|
| 71 | + # 6. 変更をコミットしてプッシュする |
| 72 | + - name: Commit and push changes |
| 73 | + if: steps.semver_bump.outputs.run_release == 'true' |
| 74 | + run: | |
| 75 | + git config --global user.name 'github-actions[bot]' |
| 76 | + git config --global user.email 'github-actions[bot]@users.noreply.github.com' |
| 77 | + git add config/version.php |
| 78 | + git commit -m "chore: Bump version to ${{ steps.new_version.outputs.tag }}" |
| 79 | + git push |
| 80 | +
|
| 81 | + # 7. ★(ここを変更) GitHub Releaseを作成する |
| 82 | + - name: Create GitHub Release |
| 83 | + if: steps.semver_bump.outputs.run_release == 'true' |
| 84 | + uses: softprops/action-gh-release@v2 |
| 85 | + with: |
| 86 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 87 | + tag_name: ${{ steps.new_version.outputs.tag }} |
| 88 | + name: Release ${{ steps.new_version.outputs.tag }} |
| 89 | + # bodyを手動で設定する代わりに、リリースノートを自動生成する |
| 90 | + generate_release_notes: true |
| 91 | + # このリリースを "Latest" として設定する (デフォルト動作ですが明記) |
| 92 | + make_latest: true |
| 93 | + draft: false |
| 94 | + prerelease: false |
0 commit comments