|
| 1 | +name: Tag and Release After Merge |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +jobs: |
| 12 | + create-tag-and-release: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v5 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Get new version from package.json |
| 21 | + run: | |
| 22 | + VERSION=$(jq -r .version package.json) |
| 23 | + echo "version=$VERSION" >> $GITHUB_ENV |
| 24 | +
|
| 25 | + - name: Get last tag (if exists) |
| 26 | + run: | |
| 27 | + LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 28 | + LAST_TAG_STRIPPED=${LAST_TAG#v} |
| 29 | + echo "last_tag=$LAST_TAG_STRIPPED" >> $GITHUB_ENV |
| 30 | +
|
| 31 | + - name: Check release type (skip patch) |
| 32 | + run: | |
| 33 | + NEW=${{ env.version }} |
| 34 | + OLD=${{ env.last_tag }} |
| 35 | +
|
| 36 | + NEW_MAJOR=$(echo $NEW | cut -d. -f1) |
| 37 | + NEW_MINOR=$(echo $NEW | cut -d. -f2) |
| 38 | + NEW_PATCH=$(echo $NEW | cut -d. -f3) |
| 39 | +
|
| 40 | + OLD_MAJOR=$(echo $OLD | cut -d. -f1) |
| 41 | + OLD_MINOR=$(echo $OLD | cut -d. -f2) |
| 42 | + OLD_PATCH=$(echo $OLD | cut -d. -f3) |
| 43 | +
|
| 44 | + if [ "$NEW_MAJOR" -gt "$OLD_MAJOR" ]; then |
| 45 | + echo "release_type=major" >> $GITHUB_ENV |
| 46 | + elif [ "$NEW_MINOR" -gt "$OLD_MINOR" ]; then |
| 47 | + echo "release_type=minor" >> $GITHUB_ENV |
| 48 | + else |
| 49 | + echo "release_type=patch" >> $GITHUB_ENV |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Skip if patch |
| 53 | + if: env.release_type == 'patch' |
| 54 | + run: echo "Patch release detected → no tag or release will be created." |
| 55 | + |
| 56 | + - name: Create and push git tag |
| 57 | + if: env.release_type != 'patch' |
| 58 | + run: | |
| 59 | + git config user.name "github-actions[bot]" |
| 60 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 61 | + git tag v${{ env.version }} |
| 62 | + git push origin v${{ env.version }} |
| 63 | +
|
| 64 | + - name: Extract release notes from CHANGELOG |
| 65 | + if: env.release_type != 'patch' |
| 66 | + id: changelog |
| 67 | + run: | |
| 68 | + # Match lines starting with "## vX.Y.Z " until the next "## " |
| 69 | + awk "/^## v${{ env.version }}[[:space:]]/{flag=1; next} /^## /{flag=0} flag" CHANGELOG.md > RELEASE_NOTES.md |
| 70 | + |
| 71 | + echo "notes<<EOF" >> $GITHUB_ENV |
| 72 | + if [ "${{ env.release_type }}" = "major" ]; then |
| 73 | + echo "## 🚀 Major Release" >> $GITHUB_ENV |
| 74 | + elif [ "${{ env.release_type }}" = "minor" ]; then |
| 75 | + echo "## ✨ Minor Release" >> $GITHUB_ENV |
| 76 | + fi |
| 77 | + echo "" >> $GITHUB_ENV |
| 78 | + cat RELEASE_NOTES.md >> $GITHUB_ENV |
| 79 | + echo "EOF" >> $GITHUB_ENV |
| 80 | +
|
| 81 | + - name: Create GitHub Release |
| 82 | + if: env.release_type != 'patch' |
| 83 | + uses: softprops/action-gh-release@v2 |
| 84 | + with: |
| 85 | + tag_name: v${{ env.version }} |
| 86 | + name: "Release v${{ env.version }}" |
| 87 | + body: ${{ env.notes }} |
| 88 | + env: |
| 89 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments