|
1 | 1 | name: Release |
2 | 2 | on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + releaseType: |
| 6 | + description: 'Release type (patch, minor, major)' |
| 7 | + required: false |
| 8 | + default: 'auto' |
| 9 | + type: choice |
| 10 | + options: |
| 11 | + - auto |
| 12 | + - patch |
| 13 | + - minor |
| 14 | + - major |
| 15 | + dryRun: |
| 16 | + description: 'Dry run (no actual release)' |
| 17 | + required: false |
| 18 | + default: false |
| 19 | + type: boolean |
| 20 | + preRelease: |
| 21 | + description: 'Pre-release identifier (beta, alpha, rc, etc.)' |
| 22 | + required: false |
| 23 | + default: '' |
| 24 | + type: string |
| 25 | + repository_dispatch: |
| 26 | + types: [release-trigger] |
3 | 27 | push: |
4 | 28 | branches: |
5 | | - - master |
| 29 | + - release |
6 | 30 | jobs: |
7 | 31 | release: |
8 | 32 | name: Release |
9 | 33 | runs-on: ubuntu-latest |
10 | 34 | strategy: |
11 | 35 | matrix: |
12 | 36 | node-version: [20] |
| 37 | + outputs: |
| 38 | + new_version: ${{ steps.extract-version.outputs.version }} |
| 39 | + released: ${{ steps.release.outputs.released }} |
13 | 40 | steps: |
14 | 41 | - name: Checkout |
15 | 42 | uses: actions/checkout@v4 |
16 | 43 | with: |
17 | 44 | fetch-depth: 0 |
| 45 | + token: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} |
| 46 | + |
18 | 47 | - name: Setup Bun |
19 | 48 | uses: oven-sh/setup-bun@v1 |
20 | 49 | with: |
21 | 50 | bun-version: latest |
| 51 | + |
22 | 52 | - name: Use Node.js ${{ matrix.node-version }} |
23 | 53 | uses: actions/setup-node@v4 |
24 | 54 | with: |
25 | 55 | node-version: ${{ matrix.node-version }} |
| 56 | + cache: 'npm' |
| 57 | + |
| 58 | + # Additional caching for faster builds |
| 59 | + - name: Cache dependencies |
| 60 | + uses: actions/cache@v3 |
| 61 | + with: |
| 62 | + path: | |
| 63 | + **/node_modules |
| 64 | + ~/.npm |
| 65 | + key: ${{ runner.os }}-node-${{ hashFiles('**/pnpm-lock.yaml', '**/bun.lockb') }} |
| 66 | + restore-keys: | |
| 67 | + ${{ runner.os }}-node- |
| 68 | +
|
26 | 69 | - name: Install dependencies |
27 | 70 | run: | |
28 | 71 | bun install |
| 72 | +
|
| 73 | + # Validate version before release (tag protection) |
| 74 | + - name: Validate release |
| 75 | + id: validate |
| 76 | + run: | |
| 77 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" || "${{ github.event_name }}" == "repository_dispatch" ]]; then |
| 78 | + echo "✅ Manual release triggered, validation passed" |
| 79 | + echo "valid=true" >> $GITHUB_OUTPUT |
| 80 | + elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/release" ]]; then |
| 81 | + # Check if there are any new commits since last release |
| 82 | + LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "none") |
| 83 | + if [[ "$LAST_TAG" != "none" ]]; then |
| 84 | + CHANGES=$(git log $LAST_TAG..HEAD --oneline) |
| 85 | + if [[ -z "$CHANGES" ]]; then |
| 86 | + echo "❌ No changes since last release" |
| 87 | + echo "valid=false" >> $GITHUB_OUTPUT |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | + fi |
| 91 | + echo "✅ Changes detected, validation passed" |
| 92 | + echo "valid=true" >> $GITHUB_OUTPUT |
| 93 | + else |
| 94 | + echo "❌ Unknown trigger source" |
| 95 | + echo "valid=false" >> $GITHUB_OUTPUT |
| 96 | + fi |
| 97 | +
|
29 | 98 | - name: Build |
| 99 | + if: steps.validate.outputs.valid == 'true' |
30 | 100 | run: | |
31 | 101 | bun run build --if-present |
| 102 | +
|
32 | 103 | - name: Run tests |
| 104 | + if: steps.validate.outputs.valid == 'true' |
33 | 105 | run: | |
34 | 106 | bun run test |
| 107 | +
|
35 | 108 | - name: Release |
| 109 | + id: release |
| 110 | + if: steps.validate.outputs.valid == 'true' |
36 | 111 | env: |
37 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 112 | + GITHUB_TOKEN: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} |
38 | 113 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
39 | | - run: npx semantic-release |
| 114 | + RELEASE_TYPE: ${{ github.event.inputs.releaseType || 'auto' }} |
| 115 | + DRY_RUN: ${{ github.event.inputs.dryRun || 'false' }} |
| 116 | + PRE_RELEASE: ${{ github.event.inputs.preRelease || '' }} |
| 117 | + run: | |
| 118 | + EXTRA_ARGS="" |
| 119 | + RELEASED="false" |
| 120 | + |
| 121 | + if [ "$DRY_RUN" == "true" ]; then |
| 122 | + echo "Running in dry-run mode" |
| 123 | + EXTRA_ARGS="--dry-run" |
| 124 | + fi |
| 125 | + |
| 126 | + if [ -n "$PRE_RELEASE" ]; then |
| 127 | + echo "Running pre-release: $PRE_RELEASE" |
| 128 | + npx semantic-release --prerelease $PRE_RELEASE $EXTRA_ARGS |
| 129 | + elif [ "$RELEASE_TYPE" == "auto" ]; then |
| 130 | + echo "Running auto release" |
| 131 | + npx semantic-release $EXTRA_ARGS |
| 132 | + else |
| 133 | + echo "Running release with type: $RELEASE_TYPE" |
| 134 | + npx semantic-release --release-as $RELEASE_TYPE $EXTRA_ARGS |
| 135 | + fi |
| 136 | + |
| 137 | + # Only set released=true if this was not a dry run and the command succeeded |
| 138 | + if [ "$DRY_RUN" != "true" ] && [ $? -eq 0 ]; then |
| 139 | + echo "released=true" >> $GITHUB_OUTPUT |
| 140 | + else |
| 141 | + echo "released=false" >> $GITHUB_OUTPUT |
| 142 | + fi |
| 143 | +
|
| 144 | + # Extract version for notifications |
| 145 | + - name: Extract version |
| 146 | + id: extract-version |
| 147 | + if: steps.release.outputs.released == 'true' |
| 148 | + run: | |
| 149 | + VERSION=$(grep '"version":' package.json | cut -d'"' -f4) |
| 150 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 151 | +
|
| 152 | + # Find related PR to comment on |
| 153 | + - name: Find PR |
| 154 | + id: find-pr |
| 155 | + if: steps.release.outputs.released == 'true' |
| 156 | + uses: jwalton/gh-find-current-pr@v1 |
| 157 | + with: |
| 158 | + state: closed |
| 159 | + |
| 160 | + # Comment on the PR that triggered this release |
| 161 | + - name: Comment on PR |
| 162 | + if: steps.release.outputs.released == 'true' && steps.find-pr.outputs.pr |
| 163 | + uses: actions/github-script@v6 |
| 164 | + with: |
| 165 | + script: | |
| 166 | + github.rest.issues.createComment({ |
| 167 | + issue_number: ${{ steps.find-pr.outputs.pr }}, |
| 168 | + owner: context.repo.owner, |
| 169 | + repo: context.repo.repo, |
| 170 | + body: '🚀 Release has been published: v${{ steps.extract-version.outputs.version }}' |
| 171 | + }) |
| 172 | + |
| 173 | + # Notification job that runs after release |
| 174 | + notify: |
| 175 | + name: Send notifications |
| 176 | + needs: release |
| 177 | + if: needs.release.outputs.released == 'true' |
| 178 | + runs-on: ubuntu-latest |
| 179 | + steps: |
| 180 | + # Optional: Slack notifications |
| 181 | + - name: Notify Slack |
| 182 | + uses: rtCamp/action-slack-notify@v2 |
| 183 | + if: env.SLACK_WEBHOOK != '' |
| 184 | + env: |
| 185 | + SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} |
| 186 | + SLACK_TITLE: "🚀 New Release" |
| 187 | + SLACK_MESSAGE: "Version v${{ needs.release.outputs.new_version }} of QuickAdd has been released!" |
| 188 | + SLACK_COLOR: "good" |
| 189 | + SLACK_ICON: "https://github.com/chhoumann.png" |
| 190 | + MSG_MINIMAL: true |
| 191 | + |
| 192 | + # Optional: Discord notifications |
| 193 | + - name: Notify Discord |
| 194 | + if: env.DISCORD_WEBHOOK != '' |
| 195 | + uses: Ilshidur/action-discord@master |
| 196 | + env: |
| 197 | + DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} |
| 198 | + with: |
| 199 | + args: '🚀 **New Release**: Version v${{ needs.release.outputs.new_version }} of QuickAdd has been released!' |
0 commit comments