|
| 1 | +# Release Workflow |
| 2 | +# |
| 3 | +# Your flow: |
| 4 | +# develop → release (PR) → merge → publishes beta |
| 5 | +# release → main (PR) → merge → publishes stable (changeset required) |
| 6 | +# |
| 7 | +# Beta version: Reads changeset for bump type (patch/minor/major), defaults to patch |
| 8 | +# |
| 9 | +name: Release |
| 10 | + |
| 11 | +on: |
| 12 | + push: |
| 13 | + branches: |
| 14 | + - main |
| 15 | + - release |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + release: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + permissions: |
| 26 | + contents: write |
| 27 | + id-token: write |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout |
| 31 | + uses: actions/checkout@v4 |
| 32 | + |
| 33 | + - name: Setup Node.js |
| 34 | + uses: actions/setup-node@v4 |
| 35 | + with: |
| 36 | + node-version: "20" |
| 37 | + cache: "npm" |
| 38 | + registry-url: "https://registry.npmjs.org" |
| 39 | + |
| 40 | + - name: Install dependencies |
| 41 | + run: npm ci |
| 42 | + |
| 43 | + - name: Build package |
| 44 | + run: npm run build |
| 45 | + |
| 46 | + # ───────────────────────────────────────────────────────── |
| 47 | + # Check for changesets and determine bump type |
| 48 | + # ───────────────────────────────────────────────────────── |
| 49 | + - name: Check changesets and bump type |
| 50 | + id: changesets |
| 51 | + run: | |
| 52 | + CHANGESET_FILE=$(find .changeset -name "*.md" ! -name "README.md" 2>/dev/null | head -1) |
| 53 | +
|
| 54 | + if [ -z "$CHANGESET_FILE" ]; then |
| 55 | + echo "has_changesets=false" >> $GITHUB_OUTPUT |
| 56 | + echo "bump_type=patch" >> $GITHUB_OUTPUT |
| 57 | + echo "⚠️ No changesets found - defaulting to patch" |
| 58 | + else |
| 59 | + echo "has_changesets=true" >> $GITHUB_OUTPUT |
| 60 | +
|
| 61 | + # Read changeset to determine bump type (major > minor > patch) |
| 62 | + if grep -q "major" "$CHANGESET_FILE"; then |
| 63 | + echo "bump_type=major" >> $GITHUB_OUTPUT |
| 64 | + echo "📦 Changeset indicates: major" |
| 65 | + elif grep -q "minor" "$CHANGESET_FILE"; then |
| 66 | + echo "bump_type=minor" >> $GITHUB_OUTPUT |
| 67 | + echo "📦 Changeset indicates: minor" |
| 68 | + else |
| 69 | + echo "bump_type=patch" >> $GITHUB_OUTPUT |
| 70 | + echo "📦 Changeset indicates: patch" |
| 71 | + fi |
| 72 | + fi |
| 73 | +
|
| 74 | + # ───────────────────────────────────────────────────────── |
| 75 | + # RELEASE BRANCH → Publish Beta |
| 76 | + # ───────────────────────────────────────────────────────── |
| 77 | + - name: Get current version |
| 78 | + if: github.ref == 'refs/heads/release' |
| 79 | + id: current_version |
| 80 | + run: | |
| 81 | + VERSION=$(node -p "require('./package.json').version") |
| 82 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 83 | + echo "Current version: $VERSION" |
| 84 | +
|
| 85 | + - name: Determine beta version |
| 86 | + if: github.ref == 'refs/heads/release' |
| 87 | + id: beta_version |
| 88 | + run: | |
| 89 | + CURRENT="${{ steps.current_version.outputs.version }}" |
| 90 | + BUMP_TYPE="${{ steps.changesets.outputs.bump_type }}" |
| 91 | +
|
| 92 | + # Remove any existing prerelease suffix |
| 93 | + BASE=$(echo $CURRENT | sed 's/-beta.*//' | sed 's/-alpha.*//' | sed 's/-rc.*//') |
| 94 | + IFS='.' read -ra PARTS <<< "$BASE" |
| 95 | +
|
| 96 | + MAJOR=${PARTS[0]} |
| 97 | + MINOR=${PARTS[1]} |
| 98 | + PATCH=${PARTS[2]} |
| 99 | +
|
| 100 | + # Apply bump based on changeset type |
| 101 | + case $BUMP_TYPE in |
| 102 | + major) |
| 103 | + NEW_MAJOR=$((MAJOR + 1)) |
| 104 | + NEW_VERSION="${NEW_MAJOR}.0.0-beta.1" |
| 105 | + ;; |
| 106 | + minor) |
| 107 | + NEW_MINOR=$((MINOR + 1)) |
| 108 | + NEW_VERSION="${MAJOR}.${NEW_MINOR}.0-beta.1" |
| 109 | + ;; |
| 110 | + patch) |
| 111 | + # Check if already a beta of same base version |
| 112 | + if [[ "$CURRENT" == "${MAJOR}.${MINOR}.$((PATCH))-beta"* ]]; then |
| 113 | + # Increment beta number |
| 114 | + BETA_NUM=$(echo $CURRENT | sed 's/.*-beta\.//') |
| 115 | + NEW_BETA=$((BETA_NUM + 1)) |
| 116 | + NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}-beta.${NEW_BETA}" |
| 117 | + else |
| 118 | + NEW_PATCH=$((PATCH + 1)) |
| 119 | + NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}-beta.1" |
| 120 | + fi |
| 121 | + ;; |
| 122 | + esac |
| 123 | +
|
| 124 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 125 | + echo "📦 Beta version: $NEW_VERSION (bump type: $BUMP_TYPE)" |
| 126 | +
|
| 127 | + - name: Update version for beta |
| 128 | + if: github.ref == 'refs/heads/release' |
| 129 | + run: npm version ${{ steps.beta_version.outputs.new_version }} --no-git-tag-version |
| 130 | + |
| 131 | + - name: Publish beta to npm |
| 132 | + if: github.ref == 'refs/heads/release' |
| 133 | + run: npm publish --tag beta --provenance --access public |
| 134 | + env: |
| 135 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 136 | + |
| 137 | + - name: Commit beta version |
| 138 | + if: github.ref == 'refs/heads/release' |
| 139 | + run: | |
| 140 | + git config user.name "github-actions[bot]" |
| 141 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 142 | + git add package.json package-lock.json |
| 143 | + git commit -m "chore: release ${{ steps.beta_version.outputs.new_version }}" |
| 144 | + git push |
| 145 | + env: |
| 146 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 147 | + |
| 148 | + # ───────────────────────────────────────────────────────── |
| 149 | + # MAIN BRANCH → Publish Stable (changeset REQUIRED) |
| 150 | + # ───────────────────────────────────────────────────────── |
| 151 | + - name: Version packages (stable) |
| 152 | + if: github.ref == 'refs/heads/main' && steps.changesets.outputs.has_changesets == 'true' |
| 153 | + run: npx changeset version |
| 154 | + |
| 155 | + - name: Publish stable to npm |
| 156 | + if: github.ref == 'refs/heads/main' && steps.changesets.outputs.has_changesets == 'true' |
| 157 | + run: npm publish --provenance --access public |
| 158 | + env: |
| 159 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 160 | + |
| 161 | + - name: Commit version + changelog |
| 162 | + if: github.ref == 'refs/heads/main' && steps.changesets.outputs.has_changesets == 'true' |
| 163 | + run: | |
| 164 | + git config user.name "github-actions[bot]" |
| 165 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 166 | + git add -A |
| 167 | + git commit -m "chore: release stable" |
| 168 | + git push |
| 169 | + env: |
| 170 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 171 | + |
| 172 | + # ───────────────────────────────────────────────────────── |
| 173 | + # MAIN BRANCH + NO CHANGESETS → Skip |
| 174 | + # ───────────────────────────────────────────────────────── |
| 175 | + - name: Skip stable publish (no changesets) |
| 176 | + if: github.ref == 'refs/heads/main' && steps.changesets.outputs.has_changesets == 'false' |
| 177 | + run: | |
| 178 | + echo "────────────────────────────────────────────────" |
| 179 | + echo "ℹ️ No changesets found - skipping stable release" |
| 180 | + echo "" |
| 181 | + echo "To publish a stable version:" |
| 182 | + echo " 1. Run: npx changeset" |
| 183 | + echo " 2. Commit the .md file" |
| 184 | + echo " 3. Merge to main" |
| 185 | + echo "────────────────────────────────────────────────" |
0 commit comments