|
| 1 | +name: Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - "src/**" |
| 8 | + - "pnpm-lock.yaml" |
| 9 | + - ".github/workflows/publish.yml" |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + bump: |
| 13 | + description: "Version bump type" |
| 14 | + required: true |
| 15 | + type: choice |
| 16 | + options: |
| 17 | + - patch |
| 18 | + - minor |
| 19 | + - major |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: publish-${{ github.event_name }} |
| 23 | + cancel-in-progress: true |
| 24 | + |
| 25 | +permissions: |
| 26 | + contents: write |
| 27 | + id-token: write |
| 28 | + |
| 29 | +jobs: |
| 30 | + quality: |
| 31 | + name: Quality Gates |
| 32 | + runs-on: ubuntu-latest |
| 33 | + steps: |
| 34 | + - uses: actions/checkout@v4 |
| 35 | + |
| 36 | + - uses: pnpm/action-setup@v4 |
| 37 | + |
| 38 | + - uses: actions/setup-node@v4 |
| 39 | + with: |
| 40 | + node-version: 22 |
| 41 | + cache: pnpm |
| 42 | + |
| 43 | + - run: pnpm install --frozen-lockfile |
| 44 | + |
| 45 | + - name: Lint |
| 46 | + run: pnpm lint |
| 47 | + |
| 48 | + - name: Typecheck |
| 49 | + run: pnpm typecheck |
| 50 | + |
| 51 | + - name: Build |
| 52 | + run: pnpm build |
| 53 | + |
| 54 | + - name: Test |
| 55 | + run: pnpm test |
| 56 | + |
| 57 | + canary: |
| 58 | + name: Publish Canary |
| 59 | + needs: quality |
| 60 | + if: github.event_name == 'push' |
| 61 | + runs-on: ubuntu-latest |
| 62 | + permissions: |
| 63 | + id-token: write |
| 64 | + contents: read |
| 65 | + steps: |
| 66 | + - uses: actions/checkout@v4 |
| 67 | + |
| 68 | + - uses: pnpm/action-setup@v4 |
| 69 | + |
| 70 | + - uses: actions/setup-node@v4 |
| 71 | + with: |
| 72 | + node-version: 22 |
| 73 | + cache: pnpm |
| 74 | + registry-url: https://registry.npmjs.org |
| 75 | + |
| 76 | + - run: pnpm install --frozen-lockfile |
| 77 | + |
| 78 | + - name: Build |
| 79 | + run: pnpm build |
| 80 | + |
| 81 | + - name: Upgrade npm for OIDC support |
| 82 | + run: npm install -g npm@latest |
| 83 | + |
| 84 | + - name: Publish canary |
| 85 | + run: | |
| 86 | + sed -i '/_authToken/d' "$NPM_CONFIG_USERCONFIG" |
| 87 | + unset NODE_AUTH_TOKEN |
| 88 | + BASE_VERSION=$(node -p "require('./package.json').version") |
| 89 | + SHORT_SHA=$(echo "$GITHUB_SHA" | cut -c1-7) |
| 90 | + CANARY_VERSION="${BASE_VERSION}-canary.${SHORT_SHA}" |
| 91 | + npm version "$CANARY_VERSION" --no-git-tag-version |
| 92 | + TARBALL=$(pnpm pack --pack-destination /tmp | tail -1) |
| 93 | + npm publish "$TARBALL" --tag canary --provenance --access public |
| 94 | +
|
| 95 | + release: |
| 96 | + name: Publish Release |
| 97 | + needs: quality |
| 98 | + if: github.event_name == 'workflow_dispatch' |
| 99 | + runs-on: ubuntu-latest |
| 100 | + permissions: |
| 101 | + contents: write |
| 102 | + id-token: write |
| 103 | + steps: |
| 104 | + - uses: actions/checkout@v4 |
| 105 | + with: |
| 106 | + fetch-depth: 0 |
| 107 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 108 | + |
| 109 | + - uses: pnpm/action-setup@v4 |
| 110 | + |
| 111 | + - uses: actions/setup-node@v4 |
| 112 | + with: |
| 113 | + node-version: 22 |
| 114 | + cache: pnpm |
| 115 | + registry-url: https://registry.npmjs.org |
| 116 | + |
| 117 | + - run: pnpm install --frozen-lockfile |
| 118 | + |
| 119 | + - name: Build |
| 120 | + run: pnpm build |
| 121 | + |
| 122 | + - name: Bump version |
| 123 | + id: version |
| 124 | + run: | |
| 125 | + npm version ${{ inputs.bump }} --no-git-tag-version |
| 126 | + VERSION=$(node -p "require('./package.json').version") |
| 127 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 128 | +
|
| 129 | + - name: Generate changelog |
| 130 | + id: changelog |
| 131 | + run: | |
| 132 | + LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 133 | + if [ -z "$LAST_TAG" ]; then |
| 134 | + RANGE="HEAD" |
| 135 | + else |
| 136 | + RANGE="${LAST_TAG}..HEAD" |
| 137 | + fi |
| 138 | +
|
| 139 | + { |
| 140 | + echo "body<<CHANGELOG_EOF" |
| 141 | +
|
| 142 | + FEATS=$(git log "$RANGE" --pretty=format:"%s" --grep="^feat" 2>/dev/null || true) |
| 143 | + if [ -n "$FEATS" ]; then |
| 144 | + echo "### Features" |
| 145 | + echo "$FEATS" | sed 's/^/- /' |
| 146 | + echo "" |
| 147 | + fi |
| 148 | +
|
| 149 | + FIXES=$(git log "$RANGE" --pretty=format:"%s" --grep="^fix" 2>/dev/null || true) |
| 150 | + if [ -n "$FIXES" ]; then |
| 151 | + echo "### Bug Fixes" |
| 152 | + echo "$FIXES" | sed 's/^/- /' |
| 153 | + echo "" |
| 154 | + fi |
| 155 | +
|
| 156 | + OTHERS=$(git log "$RANGE" --pretty=format:"%s" --invert-grep --grep="^feat" --grep="^fix" 2>/dev/null || true) |
| 157 | + if [ -n "$OTHERS" ]; then |
| 158 | + echo "### Other Changes" |
| 159 | + echo "$OTHERS" | sed 's/^/- /' |
| 160 | + echo "" |
| 161 | + fi |
| 162 | +
|
| 163 | + echo "CHANGELOG_EOF" |
| 164 | + } >> "$GITHUB_OUTPUT" |
| 165 | +
|
| 166 | + - name: Commit and tag |
| 167 | + run: | |
| 168 | + git config user.name "github-actions[bot]" |
| 169 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 170 | + git add package.json |
| 171 | + git commit -m "chore(release): v${{ steps.version.outputs.version }}" |
| 172 | + git tag -a "v${{ steps.version.outputs.version }}" -m "v${{ steps.version.outputs.version }}" |
| 173 | + git push origin main --follow-tags |
| 174 | +
|
| 175 | + - name: Upgrade npm for OIDC support |
| 176 | + run: npm install -g npm@latest |
| 177 | + |
| 178 | + - name: Publish to npm |
| 179 | + run: | |
| 180 | + sed -i '/_authToken/d' "$NPM_CONFIG_USERCONFIG" |
| 181 | + unset NODE_AUTH_TOKEN |
| 182 | + TARBALL=$(pnpm pack --pack-destination /tmp | tail -1) |
| 183 | + npm publish "$TARBALL" --tag latest --provenance --access public |
| 184 | +
|
| 185 | + - name: Create GitHub Release |
| 186 | + env: |
| 187 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 188 | + run: | |
| 189 | + cat <<'NOTES_EOF' > /tmp/release-notes.md |
| 190 | + ${{ steps.changelog.outputs.body }} |
| 191 | + NOTES_EOF |
| 192 | + gh release create "v${{ steps.version.outputs.version }}" \ |
| 193 | + --title "v${{ steps.version.outputs.version }}" \ |
| 194 | + --notes-file /tmp/release-notes.md |
0 commit comments