|
| 1 | +name: Auto Release on Main Merge |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: [closed] |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: ${{ github.event.pull_request.number || github.ref }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + auto_release: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + if: github.event.pull_request.merged == true |
| 20 | + steps: |
| 21 | + - name: Checkout |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + ref: main |
| 26 | + token: ${{ secrets.PAT }} |
| 27 | + |
| 28 | + - name: Check for release labels and determine version bumps |
| 29 | + id: check |
| 30 | + run: | |
| 31 | + labels='${{ toJson(github.event.pull_request.labels.*.name) }}' |
| 32 | + echo "PR Labels: $labels" |
| 33 | + |
| 34 | + has_release_label=false |
| 35 | + has_major=false |
| 36 | + has_minor=false |
| 37 | + has_patch=false |
| 38 | + |
| 39 | + # Check if release label exists |
| 40 | + if echo "$labels" | grep -q "release"; then |
| 41 | + has_release_label=true |
| 42 | + |
| 43 | + # Check for each type of version bump |
| 44 | + if echo "$labels" | grep -q "major"; then |
| 45 | + has_major=true |
| 46 | + fi |
| 47 | + if echo "$labels" | grep -q "minor"; then |
| 48 | + has_minor=true |
| 49 | + fi |
| 50 | + if echo "$labels" | grep -q "patch"; then |
| 51 | + has_patch=true |
| 52 | + fi |
| 53 | + |
| 54 | + # If no specific version type is specified, default to patch |
| 55 | + if [[ "$has_major" == "false" && "$has_minor" == "false" && "$has_patch" == "false" ]]; then |
| 56 | + has_patch=true |
| 57 | + fi |
| 58 | + fi |
| 59 | + |
| 60 | + echo "should_release=$has_release_label" >> $GITHUB_OUTPUT |
| 61 | + echo "has_major=$has_major" >> $GITHUB_OUTPUT |
| 62 | + echo "has_minor=$has_minor" >> $GITHUB_OUTPUT |
| 63 | + echo "has_patch=$has_patch" >> $GITHUB_OUTPUT |
| 64 | + echo "Should release: $has_release_label" |
| 65 | + echo "Has major: $has_major, minor: $has_minor, patch: $has_patch" |
| 66 | +
|
| 67 | + - name: Setup Node.js |
| 68 | + if: steps.check.outputs.should_release == 'true' |
| 69 | + uses: actions/setup-node@v4 |
| 70 | + with: |
| 71 | + node-version: 20.10 |
| 72 | + |
| 73 | + - name: Calculate new version with cumulative bumps |
| 74 | + if: steps.check.outputs.should_release == 'true' |
| 75 | + id: version |
| 76 | + run: | |
| 77 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 78 | + git config --global user.name "github-actions[bot]" |
| 79 | + |
| 80 | + # Get the latest tag from git |
| 81 | + latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 82 | + echo "Latest git tag: $latest_tag" |
| 83 | + |
| 84 | + # Remove 'v' prefix if present |
| 85 | + current_version=${latest_tag#v} |
| 86 | + echo "Current version: $current_version" |
| 87 | + |
| 88 | + # Parse current version |
| 89 | + IFS='.' read -r major minor patch <<< "$current_version" |
| 90 | + echo "Parsed version - Major: $major, Minor: $minor, Patch: $patch" |
| 91 | + |
| 92 | + # Apply cumulative version bumps |
| 93 | + if [[ "${{ steps.check.outputs.has_major }}" == "true" ]]; then |
| 94 | + major=$((major + 1)) |
| 95 | + minor=0 # Reset minor when major is bumped |
| 96 | + patch=0 # Reset patch when major is bumped |
| 97 | + echo "Applied major bump: $major.0.0" |
| 98 | + fi |
| 99 | + |
| 100 | + if [[ "${{ steps.check.outputs.has_minor }}" == "true" ]]; then |
| 101 | + minor=$((minor + 1)) |
| 102 | + if [[ "${{ steps.check.outputs.has_major }}" != "true" ]]; then |
| 103 | + patch=0 # Reset patch when minor is bumped (only if major wasn't bumped) |
| 104 | + fi |
| 105 | + echo "Applied minor bump: $major.$minor.$patch" |
| 106 | + fi |
| 107 | + |
| 108 | + if [[ "${{ steps.check.outputs.has_patch }}" == "true" ]]; then |
| 109 | + patch=$((patch + 1)) |
| 110 | + echo "Applied patch bump: $major.$minor.$patch" |
| 111 | + fi |
| 112 | + |
| 113 | + new_version="$major.$minor.$patch" |
| 114 | + echo "Final calculated version: $new_version" |
| 115 | + |
| 116 | + # Create package.json if it doesn't exist |
| 117 | + if [[ ! -f "package.json" ]]; then |
| 118 | + echo '{"version": "0.0.0"}' > package.json |
| 119 | + fi |
| 120 | + |
| 121 | + # Update package.json with new version |
| 122 | + npm version $new_version --no-git-tag-version --allow-same-version |
| 123 | + |
| 124 | + echo "NEW_VERSION=v$new_version" >> $GITHUB_ENV |
| 125 | + echo "New version will be: v$new_version" |
| 126 | +
|
| 127 | + - name: Create Release |
| 128 | + if: steps.check.outputs.should_release == 'true' |
| 129 | + uses: softprops/action-gh-release@v2 |
| 130 | + with: |
| 131 | + token: ${{ secrets.PAT }} # Use PAT to trigger other workflows |
| 132 | + tag_name: ${{ env.NEW_VERSION }} |
| 133 | + name: "Release ${{ env.NEW_VERSION }}" |
| 134 | + generate_release_notes: true |
| 135 | + make_latest: true |
| 136 | + body: | |
| 137 | + ## 🚀 Release ${{ env.NEW_VERSION }} |
| 138 | + |
| 139 | + **Version Bumps Applied:** |
| 140 | + - Major: ${{ steps.check.outputs.has_major }} |
| 141 | + - Minor: ${{ steps.check.outputs.has_minor }} |
| 142 | + - Patch: ${{ steps.check.outputs.has_patch }} |
| 143 | + |
| 144 | + **Triggered by:** PR #${{ github.event.pull_request.number }} - ${{ github.event.pull_request.title }} |
| 145 | + **Merged by:** @${{ github.event.pull_request.merged_by.login }} |
| 146 | + |
| 147 | + ### Changes in this PR |
| 148 | + ${{ github.event.pull_request.body }} |
| 149 | + |
| 150 | + --- |
| 151 | + *This release was automatically created by the Auto Release workflow* |
| 152 | +
|
0 commit comments