|
| 1 | +name: Release prep |
| 2 | + |
| 3 | +# Manual entry point: assemble changelog fragments, bump the version, build, |
| 4 | +# and open a `release vX.Y.Z` PR. Merging that PR runs the Release workflow |
| 5 | +# (publish + tag + GitHub Release). No NPM_TOKEN: npm auth in the Release |
| 6 | +# workflow is Trusted Publishing once configured, with an optional |
| 7 | +# NODE_AUTH_TOKEN bootstrap secret for the first publish; this workflow uses |
| 8 | +# only the built-in GITHUB_TOKEN. |
| 9 | +# |
| 10 | +# Trigger: Actions -> "Release prep" -> Run workflow. Pass an explicit target |
| 11 | +# version (e.g. 0.1.0-alpha.2) or leave the input empty for an auto bump |
| 12 | +# (`--patch`: X.Y.Z-pre.N -> N+1, stays in the prerelease line). |
| 13 | + |
| 14 | +on: |
| 15 | + workflow_dispatch: |
| 16 | + inputs: |
| 17 | + version: |
| 18 | + description: "Target version (e.g. 0.1.0-alpha.2). Leave empty for auto pre-release bump." |
| 19 | + required: false |
| 20 | + type: string |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: write |
| 24 | + pull-requests: write |
| 25 | + |
| 26 | +concurrency: |
| 27 | + group: release-prep |
| 28 | + cancel-in-progress: false |
| 29 | + |
| 30 | +jobs: |
| 31 | + prepare: |
| 32 | + runs-on: ubuntu-latest |
| 33 | + steps: |
| 34 | + - name: Checkout |
| 35 | + uses: actions/checkout@v4 |
| 36 | + with: |
| 37 | + fetch-depth: 0 |
| 38 | + |
| 39 | + - name: Setup pnpm |
| 40 | + uses: pnpm/action-setup@v4 |
| 41 | + with: |
| 42 | + version: 11.21.0 |
| 43 | + |
| 44 | + - name: Setup Node |
| 45 | + uses: actions/setup-node@v4 |
| 46 | + with: |
| 47 | + node-version: "24.19.0" |
| 48 | + registry-url: https://registry.npmjs.org |
| 49 | + cache: pnpm |
| 50 | + |
| 51 | + - name: Install dependencies |
| 52 | + run: pnpm install --frozen-lockfile |
| 53 | + |
| 54 | + # Validate the input format BEFORE writing it to GITHUB_OUTPUT: a |
| 55 | + # malformed (e.g. multiline) value must fail here instead of injecting |
| 56 | + # extra output lines that downstream steps would read as the version. |
| 57 | + - name: Reject already-released version |
| 58 | + id: want |
| 59 | + env: |
| 60 | + INPUT_VERSION: ${{ inputs.version }} |
| 61 | + run: | |
| 62 | + V="$INPUT_VERSION" |
| 63 | + if [ -z "$V" ]; then V="auto"; fi |
| 64 | + if [ "$V" != "auto" ]; then |
| 65 | + case "$V" in |
| 66 | + *$'\n'*|*$'\r'*) |
| 67 | + echo "::error::Invalid version \"$V\" (must be a single line)." |
| 68 | + exit 1 |
| 69 | + ;; |
| 70 | + esac |
| 71 | + if ! printf '%s' "$V" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$'; then |
| 72 | + echo "::error::Invalid version \"$V\". Expected X.Y.Z or X.Y.Z-pre.N (e.g. 0.1.0-alpha.2)." |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + if git rev-parse "v$V" >/dev/null 2>&1; then |
| 76 | + echo "::error::Tag v$V already exists." |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + fi |
| 80 | + echo "version=$V" >> "$GITHUB_OUTPUT" |
| 81 | +
|
| 82 | + - name: Assemble fragments + bump version |
| 83 | + run: | |
| 84 | + V="${{ steps.want.outputs.version }}" |
| 85 | + if [ "$V" = "auto" ]; then |
| 86 | + pnpm release:prepare -- --patch |
| 87 | + else |
| 88 | + pnpm release:prepare -- "$V" |
| 89 | + fi |
| 90 | +
|
| 91 | + - name: Resolve resulting version |
| 92 | + id: ver |
| 93 | + run: echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT" |
| 94 | + |
| 95 | + - name: Validate release version |
| 96 | + run: pnpm release:validate -- "v${{ steps.ver.outputs.version }}" |
| 97 | + |
| 98 | + - name: Build (release smoke) |
| 99 | + run: pnpm build |
| 100 | + |
| 101 | + - name: Extract changelog notes |
| 102 | + run: | |
| 103 | + awk -v v="${{ steps.ver.outputs.version }}" ' |
| 104 | + /^## \[/ { if (found) exit } |
| 105 | + index($0, "## [" v "]") == 1 { found = 1 } |
| 106 | + found { print } |
| 107 | + ' CHANGELOG.md > /tmp/notes.md |
| 108 | +
|
| 109 | + - name: Commit prepared release |
| 110 | + run: | |
| 111 | + git config user.name "github-actions[bot]" |
| 112 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 113 | + BRANCH="release/v${{ steps.ver.outputs.version }}" |
| 114 | + git checkout -B "$BRANCH" |
| 115 | + # Explicit paths only: the release PR must contain exactly the |
| 116 | + # version/changelog/fragment changes, never stray files. |
| 117 | + git add package.json CHANGELOG.md .changes/ |
| 118 | + git commit -m "chore(release): prepare v${{ steps.ver.outputs.version }}" |
| 119 | + git push --force-with-lease origin "$BRANCH" |
| 120 | +
|
| 121 | + - name: Ensure release label |
| 122 | + env: |
| 123 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + run: gh label create release --if-not-exists --color 0E8A16 || true |
| 125 | + |
| 126 | + # State-aware PR handling. `gh pr view` also matches closed PRs, and |
| 127 | + # `gh pr edit` silently keeps them closed — which would make a re-run |
| 128 | + # after rollback (close PR -> re-run prep) look successful while |
| 129 | + # leaving no open PR. Query state explicitly: |
| 130 | + # open PR -> update it; |
| 131 | + # closed PR -> reopen it, then update the body; |
| 132 | + # none -> create a new one. |
| 133 | + # A closed PR is never edited in place. |
| 134 | + - name: Open or update release PR |
| 135 | + env: |
| 136 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + VERSION: ${{ steps.ver.outputs.version }} |
| 138 | + run: | |
| 139 | + BRANCH="release/v${VERSION}" |
| 140 | + TITLE="release v${VERSION}" |
| 141 | + { |
| 142 | + echo "Generated by the **Release prep** workflow." |
| 143 | + echo |
| 144 | + echo "Merging this PR publishes dsh-llm-fallbacks@v${VERSION} to npm (--provenance; Trusted Publishing once configured, NODE_AUTH_TOKEN bootstrap), tags v${VERSION}, and creates the GitHub Release." |
| 145 | + echo |
| 146 | + echo "## Changelog" |
| 147 | + echo |
| 148 | + cat /tmp/notes.md |
| 149 | + } > /tmp/pr-body.md |
| 150 | +
|
| 151 | + OPEN_NUM="$(gh pr list --head "$BRANCH" --state open --json number -q '.[0].number' 2>/dev/null || true)" |
| 152 | + if [ -n "$OPEN_NUM" ]; then |
| 153 | + gh pr edit "$OPEN_NUM" --title "$TITLE" --body-file /tmp/pr-body.md |
| 154 | + echo "Updated existing open release PR #${OPEN_NUM} for $BRANCH" |
| 155 | + exit 0 |
| 156 | + fi |
| 157 | +
|
| 158 | + CLOSED_NUM="$(gh pr list --head "$BRANCH" --state closed --json number -q '.[0].number' 2>/dev/null || true)" |
| 159 | + if [ -n "$CLOSED_NUM" ]; then |
| 160 | + # Reopen first: `gh pr reopen` fails loudly on a merged PR instead |
| 161 | + # of silently leaving the release blocked. |
| 162 | + gh pr reopen "$CLOSED_NUM" |
| 163 | + gh pr edit "$CLOSED_NUM" --title "$TITLE" --body-file /tmp/pr-body.md |
| 164 | + echo "Reopened release PR #${CLOSED_NUM} for $BRANCH" |
| 165 | + else |
| 166 | + gh pr create --base main --head "$BRANCH" --title "$TITLE" \ |
| 167 | + --body-file /tmp/pr-body.md --label release |
| 168 | + echo "Created release PR for $BRANCH" |
| 169 | + fi |
0 commit comments