|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
| 6 | + branches: [main] |
| 7 | + |
| 8 | +jobs: |
| 9 | + release: |
| 10 | + # Only run if PR was merged (not just closed) and has a release label |
| 11 | + if: | |
| 12 | + github.event.pull_request.merged == true && |
| 13 | + (contains(github.event.pull_request.labels.*.name, 'release:major') || |
| 14 | + contains(github.event.pull_request.labels.*.name, 'release:minor') || |
| 15 | + contains(github.event.pull_request.labels.*.name, 'release:patch')) |
| 16 | +
|
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 # Fetch all history for tags |
| 26 | + |
| 27 | + - name: Get latest tag |
| 28 | + id: get_tag |
| 29 | + run: | |
| 30 | + # Get the latest semver tag (handles v prefix) |
| 31 | + LATEST_TAG=$(git tag --sort=-v:refname | grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' | head -1) |
| 32 | +
|
| 33 | + if [ -z "$LATEST_TAG" ]; then |
| 34 | + echo "No existing tags found, starting from v0.0.0" |
| 35 | + LATEST_TAG="v0.0.0" |
| 36 | + fi |
| 37 | +
|
| 38 | + echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 39 | + echo "Latest tag: $LATEST_TAG" |
| 40 | +
|
| 41 | + - name: Determine version bump type |
| 42 | + id: bump_type |
| 43 | + run: | |
| 44 | + if ${{ contains(github.event.pull_request.labels.*.name, 'release:major') }}; then |
| 45 | + echo "type=major" >> $GITHUB_OUTPUT |
| 46 | + elif ${{ contains(github.event.pull_request.labels.*.name, 'release:minor') }}; then |
| 47 | + echo "type=minor" >> $GITHUB_OUTPUT |
| 48 | + elif ${{ contains(github.event.pull_request.labels.*.name, 'release:patch') }}; then |
| 49 | + echo "type=patch" >> $GITHUB_OUTPUT |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Calculate new version |
| 53 | + id: new_version |
| 54 | + run: | |
| 55 | + LATEST_TAG="${{ steps.get_tag.outputs.latest_tag }}" |
| 56 | + BUMP_TYPE="${{ steps.bump_type.outputs.type }}" |
| 57 | +
|
| 58 | + # Remove 'v' prefix if present |
| 59 | + VERSION=${LATEST_TAG#v} |
| 60 | +
|
| 61 | + # Split version into parts |
| 62 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" |
| 63 | +
|
| 64 | + # Bump version based on type |
| 65 | + case $BUMP_TYPE in |
| 66 | + major) |
| 67 | + MAJOR=$((MAJOR + 1)) |
| 68 | + MINOR=0 |
| 69 | + PATCH=0 |
| 70 | + ;; |
| 71 | + minor) |
| 72 | + MINOR=$((MINOR + 1)) |
| 73 | + PATCH=0 |
| 74 | + ;; |
| 75 | + patch) |
| 76 | + PATCH=$((PATCH + 1)) |
| 77 | + ;; |
| 78 | + esac |
| 79 | +
|
| 80 | + NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}" |
| 81 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 82 | + echo "New version: $NEW_VERSION (bump type: $BUMP_TYPE)" |
| 83 | +
|
| 84 | + - name: Create and push tag |
| 85 | + run: | |
| 86 | + NEW_VERSION="${{ steps.new_version.outputs.new_version }}" |
| 87 | + git config user.name "github-actions[bot]" |
| 88 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 89 | + git tag -a "$NEW_VERSION" -m "Release $NEW_VERSION" |
| 90 | + git push origin "$NEW_VERSION" |
| 91 | +
|
| 92 | + - name: Create GitHub Release |
| 93 | + uses: actions/github-script@v7 |
| 94 | + with: |
| 95 | + script: | |
| 96 | + const newVersion = '${{ steps.new_version.outputs.new_version }}'; |
| 97 | + const previousTag = '${{ steps.get_tag.outputs.latest_tag }}'; |
| 98 | +
|
| 99 | + await github.rest.repos.createRelease({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + tag_name: newVersion, |
| 103 | + name: `Release ${newVersion}`, |
| 104 | + generate_release_notes: true, |
| 105 | + draft: false, |
| 106 | + prerelease: false |
| 107 | + }); |
| 108 | +
|
| 109 | + console.log(`Created release ${newVersion}`); |
0 commit comments