diff --git a/.claude/skills/developing-insta-cli/SKILL.md b/.claude/skills/developing-insta-cli/SKILL.md index 3bbb4d54..188fd6e8 100644 --- a/.claude/skills/developing-insta-cli/SKILL.md +++ b/.claude/skills/developing-insta-cli/SKILL.md @@ -98,6 +98,11 @@ skipped because its argv is split before commander ever sees it — there, pass 1. **Bump**: PR changing `package.json` version (main is protected — never commit the bump directly). Merge it via the flow above. 2. **Tag**: `git checkout main && git pull && git tag vX.Y.Z && git push origin vX.Y.Z`. + Two guards apply. The workflow's first job, `require-main`, fails the whole release (nothing is + built, no GitHub Release, no npm publish) unless the tagged commit is on `main` (GitHub compare + `...main` is `ahead` or `identical`); a `workflow_dispatch` run is checked the same way. + And a repository ruleset on `refs/tags/v*` restricts creating, moving, or deleting release tags + to repo admins, so only an admin can cut a release. 3. **Binaries (automatic)**: the `release` workflow builds 5 platform binaries + SHA256SUMS and publishes a GitHub Release. `install.sh`, `agents.sh`, and `insta upgrade` serve users from it immediately. 4. **npm (automatic on tag — OIDC trusted publishing)**: the `publish-npm` job authenticates via OpenID Connect (npmjs.com → `insta` → Trusted Publisher = this repo's `release.yml`); no token @@ -117,6 +122,8 @@ skipped because its argv is split before commander ever sees it — there, pass | John-bot ignored the request | Batched links — resend ONE link per message | | `npm error code EOTP` | Publish needs `--otp=` | | `npm publish` ENOENT package.json | Ran outside the repo root | +| `release` run fails at `require-main` ("not on main") | The tag points at a commit that isn't on `main` (e.g. tagged a feature branch, or before the squash-merge). Delete the tag, tag the merged `main` commit, push again | +| `git push origin vX.Y.Z` rejected by a ruleset | Only repo admins may create/update/delete `v*` tags — ask an admin to cut the release | | `npx insta@latest` behind the GH release | `publish-npm` job failed (OIDC trust/config?) — see step 4 | | `'C:\Program' is not recognized` from a spawned tool (win CI only) | Callers of `resolveSpawnable` must pass its `windowsVerbatimArguments` option to `spawn`; cmd.exe needs the resolver's outer command quotes and inner argument quotes intact. Spawn real `.exe` files directly. | | CLI hits the wrong server in tests | Persisted `~/.insta/config.json` apiUrl; set `INSTA_API_URL` (≥0.0.7) or move the config aside | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 80f0bc57..adab39ff 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,7 +19,35 @@ permissions: contents: write # create releases + upload assets jobs: + # Release gate: only a commit already on main may be released. A `v*` tag (repo admins only, by + # the tag ruleset) or a dispatch from a branch can point at an unreviewed commit; without this + # check that commit would ship to the GitHub Release (served by install.sh / `insta upgrade`) and + # to npm with a valid provenance attestation. Every other job `needs:` this one, so nothing is + # built or published from an off-main commit. GITHUB_SHA is the tagged commit on a tag push and + # the dispatched ref's head on workflow_dispatch. Same check as instacloud-platform deploy-prod.yml. + # + # Limit: a tag push runs the release.yml *of the tagged commit*, so a tag on an off-main commit + # whose copy of this file lacks the gate is not stopped here. That path is closed by the tag + # ruleset (only admins create `v*` tags), not by this job; moving npm publish to a main-pinned + # `workflow_run` workflow would close it in CI as well. + require-main: + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Require the commit to be on main + env: + GH_TOKEN: ${{ github.token }} + SHA: ${{ github.sha }} + run: | + status="$(gh api "repos/$GITHUB_REPOSITORY/compare/$SHA...main" --jq .status)" + case "$status" in + ahead|identical) echo "$SHA is on main ($status)" ;; + *) echo "::error::$SHA is not on main (compare status: $status). Merge it to main first, then tag the main commit."; exit 1 ;; + esac + build: + needs: require-main runs-on: ubuntu-latest # Bun cross-compiles every target from one runner strategy: fail-fast: false @@ -57,7 +85,7 @@ jobs: if-no-files-found: error release: - needs: build + needs: [require-main, build] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -116,10 +144,11 @@ jobs: # npm half of the release: publishes `insta` to the registry via OIDC trusted publishing — no # token secret, no NPM_TOKEN, and nothing to rotate (the step comment below carries the detail - # and the one setting it depends on). Gated on the binaries building, tag pushes only. + # and the one setting it depends on). Gated on the on-main check and the binaries building, tag + # pushes only. # --provenance attests the build (public repo + OIDC). publish-npm: - needs: release + needs: [require-main, release] if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-latest permissions: diff --git a/test/release-workflow.test.ts b/test/release-workflow.test.ts new file mode 100644 index 00000000..36fd8849 --- /dev/null +++ b/test/release-workflow.test.ts @@ -0,0 +1,43 @@ +import { spawnSync } from 'node:child_process' +import { readFileSync } from 'node:fs' +import { describe, expect, it } from 'vitest' +import { parse } from 'yaml' + +// Regression guard for the release gate: a `v*` tag (or a dispatch) pointing at a commit that is +// not on main must not build or publish anything — not the GitHub Release, not npm. +const workflow = parse(readFileSync(new URL('../.github/workflows/release.yml', import.meta.url), 'utf8')) +const jobs: Record = workflow.jobs +const needsOf = (job: string) => [jobs[job].needs ?? []].flat() + +const gateStep = jobs['require-main'].steps.find((s) => s.name === 'Require the commit to be on main') + +describe('release workflow main-only gate', () => { + it('has the gate job with no dependencies of its own', () => { + expect(gateStep?.run).toContain('compare/$SHA...main') + expect(needsOf('require-main')).toEqual([]) + }) + + it('makes every other job depend on the gate directly', () => { + for (const name of Object.keys(jobs).filter((j) => j !== 'require-main')) { + expect(needsOf(name), name).toContain('require-main') + } + }) + + it.skipIf(process.platform === 'win32').each([ + { status: 'ahead', ok: true }, + { status: 'identical', ok: true }, + { status: 'behind', ok: false }, + { status: 'diverged', ok: false }, + { status: '', ok: false }, + ])('compare status $status -> passes=$ok', ({ status, ok }) => { + // Stub `gh` with a shell function that prints the compare status the API would return. + const script = `set -e\ngh() { printf '%s\\n' "$STATUS"; }\n${gateStep!.run}` + const r = spawnSync('bash', ['-c', script], { + env: { ...process.env, STATUS: status, SHA: 'abc123', GITHUB_REPOSITORY: 'o/r' }, + encoding: 'utf8', + }) + // Exact exit code and the gate's own message, so a crash in the script can't pass as a refusal. + expect(r.status, r.stdout + r.stderr).toBe(ok ? 0 : 1) + if (!ok) expect(r.stdout).toContain('is not on main') + }) +})