|
| 1 | +name: Release Workflow |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: "Exact version to release (e.g. 0.3.0-rc.0). Overrides release-type/preid; npm dist-tag is derived from it (0.3.0-rc.0 -> rc, 0.3.0 -> latest)" |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + default: "" |
| 11 | + release-type: |
| 12 | + description: "Version bump when 'version' is empty (patch/minor/major publish to npm 'latest'; prerelease uses 'preid')" |
| 13 | + required: false |
| 14 | + type: choice |
| 15 | + options: |
| 16 | + - patch |
| 17 | + - minor |
| 18 | + - major |
| 19 | + - prerelease |
| 20 | + default: patch |
| 21 | + preid: |
| 22 | + description: "Prerelease identifier (used only when release-type=prerelease and 'version' is empty; also becomes the npm dist-tag, e.g. next | canary)" |
| 23 | + required: false |
| 24 | + type: string |
| 25 | + default: next |
| 26 | + publish-only: |
| 27 | + description: "Publish the version already committed in packages/tanstack-router/package.json (with its CHANGELOG entry). Skips version bump and changelog; tags, publishes and creates the GitHub release. Ignores version/release-type/preid." |
| 28 | + required: false |
| 29 | + type: boolean |
| 30 | + default: false |
| 31 | + dry-run: |
| 32 | + description: "Run release steps without making changes (no git push, no publish)" |
| 33 | + required: false |
| 34 | + type: boolean |
| 35 | + default: false |
| 36 | + |
| 37 | +concurrency: |
| 38 | + # Avoid overlapping publishes on the same ref/branch |
| 39 | + group: nx-release-${{ github.ref }} |
| 40 | + cancel-in-progress: false |
| 41 | + |
| 42 | +permissions: |
| 43 | + contents: write # needed to push version commits and tags |
| 44 | + id-token: write # required for npm provenance / trusted publishing (OIDC) |
| 45 | + |
| 46 | +env: |
| 47 | + RELEASE_PROJECT: tanstack-router |
| 48 | + PACKAGE_DIR: packages/tanstack-router |
| 49 | + |
| 50 | +jobs: |
| 51 | + release: |
| 52 | + name: Version and Publish (gated by environment) |
| 53 | + runs-on: ubuntu-latest |
| 54 | + environment: |
| 55 | + name: ${{ inputs.dry-run && 'npm-publish-dry-run' || 'npm-publish' }} |
| 56 | + |
| 57 | + steps: |
| 58 | + - name: Harden the runner (Audit all outbound calls) |
| 59 | + uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 |
| 60 | + with: |
| 61 | + egress-policy: audit |
| 62 | + |
| 63 | + - name: Checkout repository (full history for tagging) |
| 64 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 65 | + with: |
| 66 | + fetch-depth: 0 |
| 67 | + |
| 68 | + - name: Setup Node.js |
| 69 | + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 |
| 70 | + with: |
| 71 | + node-version: '24' |
| 72 | + registry-url: 'https://registry.npmjs.org' |
| 73 | + cache: 'npm' |
| 74 | + |
| 75 | + - name: Update npm (required for OIDC trusted publishing) |
| 76 | + run: | |
| 77 | + npm install -g npm@^11.5.1 |
| 78 | + npm --version |
| 79 | +
|
| 80 | + - name: Install dependencies |
| 81 | + run: npm ci |
| 82 | + |
| 83 | + - name: Configure git user for automated commits |
| 84 | + run: | |
| 85 | + git config user.name "github-actions[bot]" |
| 86 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 87 | +
|
| 88 | + # The checkout above is pinned to the SHA resolved at dispatch time; re-sync to |
| 89 | + # the branch tip so the release commit/tag push is a fast-forward. |
| 90 | + - name: Sync to latest branch tip |
| 91 | + if: ${{ !inputs.dry-run }} |
| 92 | + run: | |
| 93 | + git fetch origin "$GITHUB_REF_NAME" |
| 94 | + git reset --hard "origin/$GITHUB_REF_NAME" |
| 95 | +
|
| 96 | + - name: Resolve release context |
| 97 | + id: ctx |
| 98 | + shell: bash |
| 99 | + env: |
| 100 | + INPUT_VERSION: ${{ inputs.version }} |
| 101 | + INPUT_RELEASE_TYPE: ${{ inputs['release-type'] }} |
| 102 | + INPUT_PREID: ${{ inputs.preid }} |
| 103 | + INPUT_PUBLISH_ONLY: ${{ inputs['publish-only'] }} |
| 104 | + INPUT_DRY_RUN: ${{ inputs['dry-run'] }} |
| 105 | + run: | |
| 106 | + set -euo pipefail |
| 107 | +
|
| 108 | + dist_tag_for() { |
| 109 | + # npm dist-tag is the alphabetic prerelease label (0.3.0-rc.0 -> rc); purely numeric labels fall back to next. |
| 110 | + local v="$1" |
| 111 | + if [[ "$v" == *-* ]]; then |
| 112 | + local prerelease="${v#*-}" |
| 113 | + local tag="${prerelease%%.*}" |
| 114 | + if [[ "$tag" =~ ^[0-9]+$ ]]; then tag="next"; fi |
| 115 | + echo "$tag" |
| 116 | + else |
| 117 | + echo "latest" |
| 118 | + fi |
| 119 | + } |
| 120 | +
|
| 121 | + specifier="" |
| 122 | + preid="" |
| 123 | + version="" |
| 124 | + if [[ "$INPUT_PUBLISH_ONLY" == "true" ]]; then |
| 125 | + version=$(node -p "require('./${PACKAGE_DIR}/package.json').version") |
| 126 | + dist_tag=$(dist_tag_for "$version") |
| 127 | + elif [[ -n "$INPUT_VERSION" ]]; then |
| 128 | + specifier="$INPUT_VERSION" |
| 129 | + dist_tag=$(dist_tag_for "$INPUT_VERSION") |
| 130 | + else |
| 131 | + specifier="$INPUT_RELEASE_TYPE" |
| 132 | + # npm dist-tag follows release type: prerelease -> preid, stable -> latest |
| 133 | + if [[ "$INPUT_RELEASE_TYPE" == "prerelease" ]]; then |
| 134 | + preid="$INPUT_PREID" |
| 135 | + dist_tag="$INPUT_PREID" |
| 136 | + else |
| 137 | + dist_tag="latest" |
| 138 | + fi |
| 139 | + fi |
| 140 | +
|
| 141 | + { |
| 142 | + echo "publish_only=${INPUT_PUBLISH_ONLY}" |
| 143 | + echo "specifier=${specifier}" |
| 144 | + echo "preid=${preid}" |
| 145 | + echo "version=${version}" |
| 146 | + echo "dist_tag=${dist_tag}" |
| 147 | + echo "dry_run=${INPUT_DRY_RUN}" |
| 148 | + } >> "$GITHUB_OUTPUT" |
| 149 | +
|
| 150 | + # PUBLISH-ONLY: the version and CHANGELOG entry are already committed. Refuse a |
| 151 | + # version npm already has, then tag HEAD locally (nx.json releaseTagPattern) so the |
| 152 | + # GitHub release and future changelogs have their anchor. |
| 153 | + - name: Check committed version (publish-only) |
| 154 | + if: ${{ inputs['publish-only'] }} |
| 155 | + shell: bash |
| 156 | + env: |
| 157 | + VERSION: ${{ steps.ctx.outputs.version }} |
| 158 | + run: | |
| 159 | + set -euo pipefail |
| 160 | + pkg=$(node -p "require('./${PACKAGE_DIR}/package.json').name") |
| 161 | + if npm view "${pkg}@${VERSION}" version >/dev/null 2>&1; then |
| 162 | + echo "::error::${pkg}@${VERSION} is already on npm. Bump the version (or dispatch without publish-only)." |
| 163 | + exit 1 |
| 164 | + fi |
| 165 | + if ! grep -q "^## ${VERSION} (" "${PACKAGE_DIR}/CHANGELOG.md"; then |
| 166 | + echo "::warning::No '## ${VERSION} (' entry in ${PACKAGE_DIR}/CHANGELOG.md; the GitHub release will use generated notes." |
| 167 | + fi |
| 168 | + tag="${VERSION}-${RELEASE_PROJECT}" |
| 169 | + if git rev-parse -q --verify "refs/tags/${tag}" >/dev/null; then |
| 170 | + if [[ "$(git rev-list -n1 "$tag")" != "$(git rev-parse HEAD)" ]]; then |
| 171 | + echo "::error::Tag ${tag} already exists on another commit." |
| 172 | + exit 1 |
| 173 | + fi |
| 174 | + fi |
| 175 | +
|
| 176 | + # VERSION: bumps the version, generates the changelog, commits + tags locally in one shot. |
| 177 | + # --skip-publish keeps publishing as a separate step below so the OIDC token-clearing logic still runs. |
| 178 | + - name: nx release version + changelog |
| 179 | + if: ${{ !inputs['publish-only'] }} |
| 180 | + shell: bash |
| 181 | + env: |
| 182 | + SPECIFIER: ${{ steps.ctx.outputs.specifier }} |
| 183 | + PREID: ${{ steps.ctx.outputs.preid }} |
| 184 | + DRY_RUN: ${{ inputs['dry-run'] }} |
| 185 | + run: | |
| 186 | + set -euo pipefail |
| 187 | +
|
| 188 | + preid_arg=() |
| 189 | + if [[ -n "$PREID" ]]; then |
| 190 | + preid_arg=(--preid "$PREID") |
| 191 | + fi |
| 192 | + dry_run_arg=() |
| 193 | + if [[ "$DRY_RUN" == "true" ]]; then |
| 194 | + dry_run_arg=(--dry-run) |
| 195 | + fi |
| 196 | +
|
| 197 | + npx nx release "$SPECIFIER" \ |
| 198 | + "${preid_arg[@]}" \ |
| 199 | + --projects "$RELEASE_PROJECT" \ |
| 200 | + --skip-publish \ |
| 201 | + --verbose \ |
| 202 | + "${dry_run_arg[@]}" |
| 203 | +
|
| 204 | + - name: Tag committed version (publish-only) |
| 205 | + if: ${{ inputs['publish-only'] }} |
| 206 | + shell: bash |
| 207 | + env: |
| 208 | + VERSION: ${{ steps.ctx.outputs.version }} |
| 209 | + run: | |
| 210 | + set -euo pipefail |
| 211 | + tag="${VERSION}-${RELEASE_PROJECT}" |
| 212 | + if ! git rev-parse -q --verify "refs/tags/${tag}" >/dev/null; then |
| 213 | + git tag -a "$tag" -m "$tag" |
| 214 | + fi |
| 215 | +
|
| 216 | + # BUILD + TEST: the build copies the (bumped) package.json into dist/, which is what gets published. |
| 217 | + - name: Build |
| 218 | + run: npx nx build "$RELEASE_PROJECT" --verbose |
| 219 | + |
| 220 | + - name: Test |
| 221 | + run: npx nx test "$RELEASE_PROJECT" |
| 222 | + |
| 223 | + - name: Resolve released version |
| 224 | + id: released |
| 225 | + shell: bash |
| 226 | + run: | |
| 227 | + set -euo pipefail |
| 228 | + echo "version=$(node -p "require('./dist/${PACKAGE_DIR}/package.json').version")" >> "$GITHUB_OUTPUT" |
| 229 | +
|
| 230 | + # Nothing leaves the runner until build and tests pass. --atomic: if the branch push |
| 231 | + # is rejected, the release tag is rejected with it, so a failed run leaves no |
| 232 | + # orphaned tag behind and a plain re-dispatch is enough to retry. |
| 233 | + - name: Push release commit and tag |
| 234 | + if: ${{ !inputs['publish-only'] && !inputs.dry-run }} |
| 235 | + run: git push --atomic --follow-tags origin HEAD |
| 236 | + |
| 237 | + - name: Push release tag (publish-only) |
| 238 | + if: ${{ inputs['publish-only'] && !inputs.dry-run }} |
| 239 | + shell: bash |
| 240 | + env: |
| 241 | + VERSION: ${{ steps.ctx.outputs.version }} |
| 242 | + run: git push origin "refs/tags/${VERSION}-${RELEASE_PROJECT}" |
| 243 | + |
| 244 | + # PUBLISH: OIDC trusted publishing (default). Avoid any lingering token auth. |
| 245 | + - name: nx release publish (OIDC) |
| 246 | + if: ${{ vars.USE_NPM_TOKEN != 'true' }} |
| 247 | + shell: bash |
| 248 | + env: |
| 249 | + NPM_CONFIG_PROVENANCE: true |
| 250 | + NODE_AUTH_TOKEN: "" |
| 251 | + DIST_TAG: ${{ steps.ctx.outputs.dist_tag }} |
| 252 | + DRY_RUN: ${{ inputs['dry-run'] }} |
| 253 | + run: | |
| 254 | + set -euo pipefail |
| 255 | + unset NODE_AUTH_TOKEN |
| 256 | + rm -f ~/.npmrc || true |
| 257 | + if [[ -n "${NPM_CONFIG_USERCONFIG:-}" ]]; then |
| 258 | + rm -f "$NPM_CONFIG_USERCONFIG" || true |
| 259 | + fi |
| 260 | +
|
| 261 | + dry_run_arg=() |
| 262 | + if [[ "$DRY_RUN" == "true" ]]; then |
| 263 | + dry_run_arg=(--dry-run) |
| 264 | + fi |
| 265 | +
|
| 266 | + npx nx release publish \ |
| 267 | + --projects "$RELEASE_PROJECT" \ |
| 268 | + --tag "$DIST_TAG" \ |
| 269 | + --access public \ |
| 270 | + --verbose \ |
| 271 | + "${dry_run_arg[@]}" |
| 272 | +
|
| 273 | + # PUBLISH: token fallback (only when explicitly enabled via repo/environment variable USE_NPM_TOKEN=true). |
| 274 | + - name: nx release publish (token) |
| 275 | + if: ${{ vars.USE_NPM_TOKEN == 'true' }} |
| 276 | + shell: bash |
| 277 | + env: |
| 278 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} |
| 279 | + NPM_CONFIG_PROVENANCE: true |
| 280 | + DIST_TAG: ${{ steps.ctx.outputs.dist_tag }} |
| 281 | + DRY_RUN: ${{ inputs['dry-run'] }} |
| 282 | + run: | |
| 283 | + set -euo pipefail |
| 284 | + dry_run_arg=() |
| 285 | + if [[ "$DRY_RUN" == "true" ]]; then |
| 286 | + dry_run_arg=(--dry-run) |
| 287 | + fi |
| 288 | + npx nx release publish --projects "$RELEASE_PROJECT" --tag "$DIST_TAG" --access public --verbose "${dry_run_arg[@]}" |
| 289 | +
|
| 290 | + # Nx only writes CHANGELOG.md, so the GitHub release is posted here, after the tag |
| 291 | + # push (so it is not auto-created from the wrong commit) and after npm publish (so |
| 292 | + # a GitHub API failure cannot block publishing). |
| 293 | + - name: Create GitHub release |
| 294 | + if: ${{ !inputs.dry-run }} |
| 295 | + shell: bash |
| 296 | + env: |
| 297 | + GH_TOKEN: ${{ github.token }} |
| 298 | + VERSION: ${{ steps.released.outputs.version }} |
| 299 | + run: | |
| 300 | + set -euo pipefail |
| 301 | +
|
| 302 | + tag="${VERSION}-${RELEASE_PROJECT}" |
| 303 | + pkg=$(node -p "require('./${PACKAGE_DIR}/package.json').name") |
| 304 | +
|
| 305 | + if gh release view "$tag" --json id --jq .id >/dev/null 2>&1; then |
| 306 | + echo "Release for '$tag' already exists; skipping." |
| 307 | + exit 0 |
| 308 | + fi |
| 309 | +
|
| 310 | + notes_file="$(mktemp)" |
| 311 | + awk -v h="## ${VERSION} (" 'index($0, h) == 1 {found=1; next} /^## / {if (found) exit} found' "${PACKAGE_DIR}/CHANGELOG.md" > "$notes_file" |
| 312 | +
|
| 313 | + flags=(--verify-tag --title "${pkg}@${VERSION}") |
| 314 | + if [[ "$VERSION" == *-* ]]; then |
| 315 | + flags+=(--prerelease) |
| 316 | + else |
| 317 | + flags+=(--latest) |
| 318 | + fi |
| 319 | + if [[ -s "$notes_file" ]]; then |
| 320 | + flags+=(--notes-file "$notes_file") |
| 321 | + else |
| 322 | + flags+=(--generate-notes) |
| 323 | + fi |
| 324 | +
|
| 325 | + gh release create "$tag" "${flags[@]}" |
| 326 | +
|
| 327 | + - name: Summary |
| 328 | + if: always() |
| 329 | + run: | |
| 330 | + echo "Nx Release completed." |
| 331 | + echo "- project: ${RELEASE_PROJECT}" |
| 332 | + echo "- publish-only: ${{ steps.ctx.outputs.publish_only }}" |
| 333 | + echo "- specifier: '${{ steps.ctx.outputs.specifier }}'" |
| 334 | + echo "- preid: '${{ steps.ctx.outputs.preid }}'" |
| 335 | + echo "- version: ${{ steps.released.outputs.version }}" |
| 336 | + echo "- dist-tag: ${{ steps.ctx.outputs.dist_tag }}" |
| 337 | + echo "- dry-run: ${{ steps.ctx.outputs.dry_run }}" |
| 338 | + echo "- use-token: ${{ vars.USE_NPM_TOKEN == 'true' }}" |
0 commit comments