Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Publish OK's npm package from a job that runs no package code (#5571)

Nick Gomezab8a96beeef9
diff --git a/.github/scripts/release-cascade-shape.test.mjs b/.github/scripts/release-cascade-shape.test.mjs
index de0bb29a0..17f503a6e 100644
--- a/.github/scripts/release-cascade-shape.test.mjs
+++ b/.github/scripts/release-cascade-shape.test.mjs
@@ -70,7 +70,7 @@ describe('release jobs install the pnpm version declared by the checked-out tag'
'desktop-release.yml#build-macos',
'desktop-release.yml#build-windows',
'desktop-release.yml#build-linux',
- 'release.yml#release',
+ 'release.yml#build',
]),
);
});
@@ -1333,7 +1333,7 @@ describe('every job that reads changesets installs the Changesets reader first',
'bug-lane.yml#read-bumps',
'point-release.yml#read-bumps',
'promote-stable.yml#read-bumps',
- 'release.yml#release',
+ 'release.yml#build',
'select-beta-to-promote.yml#read-bumps',
]),
);
@@ -1957,6 +1957,705 @@ describe('the release App credential never shares a job with installed packages'
expect(commands(step)).toMatch(/gh run list --workflow=promote-stable\.yml/);
});
});
+
+ describe('release.yml keeps the OIDC publish permission and the write token out of every job that runs package code', () => {
+ const releaseWorkflow = parse(read('release.yml'));
+ const TARBALL_PUBLISH = 'npm publish "$TARBALL" --access public --tag "$TAG" --provenance --registry https://registry.npmjs.org/';
+ const PUBLISHER_COMMANDS = new Set([
+ 'npm install -g [email protected]',
+ 'pacote="$(npm root -g)/npm/node_modules/pacote"',
+ 'echo "npm $(npm --version)"',
+ 'if [[ "$(npm view "${PACKAGE_NAME}@${VERSION}" version 2>/dev/null || true)" == "$VERSION" ]]; then',
+ TARBALL_PUBLISH,
+ ]);
+ const PUBLISHER_ACTIONS = ['actions/setup-node@', 'actions/download-artifact@'];
+ const PACK_IF = "github.event.action == 'publish-stable' || steps.compute-beta.outputs.run_beta == 'true'";
+ const RUN_IF = "github.event.action == 'publish-stable' || needs.build.outputs.run_beta == 'true'";
+ const BETA_IF = "needs.build.outputs.run_beta == 'true'";
+
+ const permissionsOf = (workflow, job) => job.permissions ?? workflow.permissions ?? 'write-all';
+ const grants = (permissions) =>
+ typeof permissions === 'string'
+ ? permissions === 'read-all'
+ ? []
+ : [permissions]
+ : Object.entries(permissions)
+ .filter(([, level]) => level === 'write')
+ .map(([scope]) => `${scope}: write`);
+ const mintsOidc = (workflow, job) =>
+ grants(permissionsOf(workflow, job)).some((grant) => grant === 'id-token: write' || grant === 'write-all');
+ const credentialsOf = (workflow, job) => [
+ ...grants(permissionsOf(workflow, job)),
+ ...(JSON.stringify(job).match(/secrets\.\w+/g) ?? []),
+ ...(mintsAppToken(job) ? ['App token'] : []),
+ ];
+ const publisherView = (step) =>
+ step.run === undefined
+ ? step
+ : { ...step, run: step.run.split('\n').filter((line) => !PUBLISHER_COMMANDS.has(line.trim())).join('\n') };
+ const releasePackageRoutes = (workflow, job) => {
+ const publisher = mintsOidc(workflow, job);
+ return packageRoutes(
+ steps(job)
+ .filter((step) => !(publisher && PUBLISHER_ACTIONS.some((prefix) => step.uses?.startsWith(prefix))))
+ .map((step) => (publisher ? publisherView(step) : step)),
+ );
+ };
+ const READS_RELEASES = /(^|[;&|({`\s])gh\s+release\s+(list|view)\b/m;
+ const seesDraftReleases = (workflow, job) =>
+ grants(permissionsOf(workflow, job)).some((grant) => grant === 'contents: write' || grant === 'write-all');
+ const releaseViolations = (workflow) => {
+ const jobs = Object.entries(workflow.jobs);
+ const violations = [];
+ for (const [id, job] of jobs) {
+ const routes = releasePackageRoutes(workflow, job);
+ const credentials = credentialsOf(workflow, job);
+ if (routes.length > 0 && credentials.length > 0) {
+ violations.push(`${id} runs package code (${routes.join(', ')}) and holds ${credentials.join(', ')}`);
+ }
+ if (mintsOidc(workflow, job)) {
+ const others = credentials.filter((credential) => credential !== 'id-token: write');
+ if (others.length > 0) violations.push(`${id} can mint an OIDC token and also holds ${others.join(', ')}`);
+ const repository = steps(job)
+ .filter((step) => step.uses?.startsWith('actions/checkout@') || step.uses?.startsWith('./'))
+ .map((step) => step.uses);
+ if (repository.length > 0) {
+ violations.push(`${id} can mint an OIDC token and runs repository content (${repository.join(', ')})`);
+ }
+ }
+ for (const step of steps(job).filter((candidate) => candidate.uses?.startsWith('actions/checkout@'))) {
+ if (step.with?.['persist-credentials'] !== false || step.with?.token !== undefined) {
+ violations.push(`${id} has a checkout that persists a credential`);
+ }
+ }
+ }
+ for (const [id, job] of jobs) {
+ if (steps(job).some((step) => READS_RELEASES.test(commands(step))) && !seesDraftReleases(workflow, job)) {
+ violations.push(`${id} reads Releases with gh but cannot see draft Releases`);
+ }
+ if (releasePackageRoutes(workflow, job).length === 0) continue;
+ for (const need of [job.needs ?? []].flat()) {
+ const feeder = workflow.jobs[need];
+ const actions = steps(feeder).filter((step) => step.uses).map((step) => step.uses);
+ const credentials = credentialsOf(workflow, feeder);
+ if (credentials.length > 0 && actions.length > 0) {
+ violations.push(`${need} holds ${credentials.join(', ')} and feeds package code in ${id}, but runs ${actions.join(', ')}`);
+ }
+ }
+ }
+ const publishers = jobs
+ .filter(([, job]) => steps(job).some((step) => commands(step).split('\n').some((line) => line.trim() === TARBALL_PUBLISH)))
+ .map(([id]) => id);
+ if (publishers.length !== 1 || !mintsOidc(workflow, workflow.jobs[publishers[0]])) {
+ violations.push(`expected one OIDC job to publish the packed tarball, found ${publishers.join(', ') || 'none'}`);
+ }
+ return violations;
+ };
+ const runBash = (script, env) => {
+ try {
+ return { status: 0, stdout: execFileSync('bash', ['-c', script], { encoding: 'utf8', env, stdio: ['ignore', 'pipe', 'pipe'] }), stderr: '' };
+ } catch (error) {
+ if (typeof error.status !== 'number') throw error;
+ return { status: error.status, stdout: error.stdout, stderr: error.stderr };
+ }
+ };
+ const mutated = (change) => {
+ const workflow = structuredClone(releaseWorkflow);
+ change(workflow);
+ return workflow;
+ };
+ const stepNamed = (job, name) => {
+ const step = steps(job ?? {}).find((candidate) => candidate.name === name);
+ if (!step) throw new Error(`release.yml has no step named ${name}`);
+ return step;
+ };
+ const PUBLISH_STEP = 'Publish to npm via Trusted Publishing';
+ const VALIDATE_STEP = 'Refuse build outputs that are not well-formed versions';
+ const replacePublishLine = (workflow, line) => {
+ const step = stepNamed(workflow.jobs.publish, PUBLISH_STEP);
+ step.run = step.run.replace(TARBALL_PUBLISH, line);
+ };
+
+ test('no job that runs package code holds a credential, and only the OIDC job publishes', () => {
+ expect(releaseViolations(releaseWorkflow)).toEqual([]);
+ expect(Object.keys(releaseWorkflow.jobs)).toEqual(['read-releases', 'build', 'release', 'publish']);
+ const { 'read-releases': readReleases, build, release, publish } = releaseWorkflow.jobs;
+ expect(releasePackageRoutes(releaseWorkflow, readReleases)).toEqual([]);
+ expect(readReleases.permissions).toEqual({ contents: 'write' });
+ expect(releasePackageRoutes(releaseWorkflow, build).length).toBeGreaterThan(0);
+ expect(credentialsOf(releaseWorkflow, build)).toEqual([]);
+ expect(build.permissions).toEqual({ contents: 'read' });
+ expect(releasePackageRoutes(releaseWorkflow, release)).toEqual([]);
+ expect(release.permissions).toEqual({ contents: 'write' });
+ expect(releasePackageRoutes(releaseWorkflow, publish)).toEqual([]);
+ expect(publish.permissions).toEqual({ 'id-token': 'write' });
+ expect(releaseWorkflow.permissions).toEqual({ contents: 'read' });
+ });
+
+ test.each([
+ ['an install in the publish job', (w) => w.jobs.publish.steps.push({ name: 'Install', run: 'pnpm install --frozen-lockfile' }), 'publish runs package code'],
+ ['changeset publish in the publish job', (w) => replacePublishLine(w, 'pnpm exec changeset publish --tag "$TAG"'), 'publish runs package code'],
+ ['a pnpm setup action in the publish job', (w) => w.jobs.publish.steps.unshift({ uses: tagCompatiblePnpmSetup }), 'publish runs package code'],
+ ['publishing a workspace folder', (w) => replacePublishLine(w, 'npm publish packages/cli --access public --tag "$TAG"'), 'publish runs package code'],
+ ['a checkout in the publish job', (w) => w.jobs.publish.steps.unshift({ uses: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1', with: { 'persist-credentials': false } }), 'publish can mint an OIDC token and runs repository content'],
+ ['a local action in the publish job', (w) => w.jobs.publish.steps.push({ uses: './.github/composite-actions/share-contract-reader-gate' }), 'publish can mint an OIDC token and runs repository content'],
+ ['id-token on the build job', (w) => { w.jobs.build.permissions = { contents: 'read', 'id-token': 'write' }; }, 'build runs package code'],
+ ['a write token on the build job', (w) => { w.jobs.build.permissions = { contents: 'write' }; }, 'build runs package code'],
+ ['a secret in the build job', (w) => { stepNamed(w.jobs.build, 'Install').env = { GH_TOKEN: '${{ secrets.GITHUB_TOKEN }}' }; }, 'build runs package code'],
+ ['a build job that inherits write permissions', (w) => { delete w.jobs.build.permissions; w.permissions = { contents: 'write', 'id-token': 'write' }; }, 'build runs package code'],
+ ['id-token on the release job', (w) => { w.jobs.release.permissions = { contents: 'write', 'id-token': 'write' }; }, 'release can mint an OIDC token and also holds contents: write'],
+ ['a checkout that persists its token', (w) => { delete w.jobs.release.steps[0].with['persist-credentials']; }, 'release has a checkout that persists a credential'],
+ ['a publish job that publishes nothing', (w) => replacePublishLine(w, 'true'), 'expected one OIDC job to publish the packed tarball, found none'],
+ ['a gh release read in the build job', (w) => w.jobs.build.steps.push({ name: 'Peek', run: 'gh release list --repo "$GITHUB_REPOSITORY"' }), 'build reads Releases with gh but cannot see draft Releases'],
+ ['read-releases narrowed so drafts are invisible', (w) => { w.jobs['read-releases'].permissions = { contents: 'read' }; }, 'read-releases reads Releases with gh but cannot see draft Releases'],
+ ['a checkout in read-releases', (w) => w.jobs['read-releases'].steps.unshift({ uses: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1', with: { 'persist-credentials': false } }), 'read-releases holds contents: write and feeds package code in build, but runs actions/checkout@'],
+ ['an install in read-releases', (w) => w.jobs['read-releases'].steps.push({ name: 'Install', run: 'pnpm install --frozen-lockfile' }), 'read-releases runs package code'],
+ ])('the shape check bites on %s', (_, change, expected) => {
+ const introduced = expect.arrayContaining([expect.stringContaining(expected)]);
+ expect(releaseViolations(releaseWorkflow)).not.toEqual(introduced);
+ expect(releaseViolations(mutated(change))).toEqual(introduced);
+ });
+
+ test('the build job packs the overridden cli after its prepublishOnly, and the publish job reads that artifact', () => {
+ const { build, publish } = releaseWorkflow.jobs;
+ const index = (name) => steps(build).indexOf(stepNamed(build, name));
+ const prepublish = stepNamed(build, "Run the cli's prepublishOnly against the overridden versions");
+ const pack = stepNamed(build, 'Pack the cli tarball');
+ const upload = stepNamed(build, 'Upload the packed tarball for the publish job');
+ expect(prepublish.run).toBe('pnpm run prepublishOnly');
+ expect(commands(pack)).toContain('pnpm pack --pack-destination "$RUNNER_TEMP/npm-package"');
+ for (const step of [prepublish, pack]) expect(step['working-directory']).toBe('packages/cli');
+ for (const step of [prepublish, pack, upload]) expect(step.if).toBe(PACK_IF);
+ expect(upload.uses).toMatch(/^actions\/upload-artifact@[0-9a-f]{40}$/);
+ expect(upload.with).toMatchObject({ name: 'npm-package', path: '${{ runner.temp }}/npm-package/*.tgz', 'if-no-files-found': 'error' });
+ expect(index('Override fixed-group versions to X.Y.Z-beta.N')).toBeLessThan(index(prepublish.name));
+ expect(index('Validate stable_version + override package.json (publish-stable)')).toBeLessThan(index(prepublish.name));
+ expect(index(prepublish.name)).toBeLessThan(index(pack.name));
+ expect(index(pack.name)).toBeLessThan(index(upload.name));
+ const download = steps(publish).find((step) => step.uses?.startsWith('actions/download-artifact@'));
+ expect(download.with).toEqual({ name: 'npm-package', path: '${{ runner.temp }}/npm-package' });
+ expect(stepNamed(publish, PUBLISH_STEP).env.PACKAGE_DIR).toBe('${{ runner.temp }}/npm-package');
+ expect(publish.needs).toEqual(['build', 'release']);
+ expect(releaseWorkflow.jobs.release.needs).toBe('build');
+ for (const job of ['release', 'publish']) expect(releaseWorkflow.jobs[job].if).toBe(RUN_IF);
+ });
+
+ test('the credential jobs take from the build job only what needs package code to compute', () => {
+ const reads = (id) => {
+ const job = releaseWorkflow.jobs[id];
+ return expressions(job)
+ .filter(({ expr }) => /\bneeds\b/.test(expr))
+ .map(({ path, expr }) =>
+ `${(path[0] === 'steps' ? ['steps', job.steps[path[1]].name, ...path.slice(2)] : path).join(' > ')}: ${expr}`,
+ );
+ };
+ const RESOLVE = 'Resolve -beta.N counter';
+ expect(reads('release')).toEqual([
+ `if: ${RUN_IF}`,
+ `steps > ${VALIDATE_STEP} > if: ${BETA_IF}`,
+ `steps > ${VALIDATE_STEP} > env > BASE_VERSION: needs.build.outputs.base_version`,
+ `steps > ${VALIDATE_STEP} > env > BUILD_VERSION: needs.build.outputs.version`,
+ `steps > Guard - beta base must lead the latest stable > if: ${BETA_IF}`,
+ 'steps > Guard - beta base must lead the latest stable > env > BASE_VERSION: needs.build.outputs.base_version',
+ `steps > ${RESOLVE} > if: ${BETA_IF}`,
+ `steps > ${RESOLVE} > env > BASE_VERSION: needs.build.outputs.base_version`,
+ `steps > Refuse a beta the build job resolved differently > if: ${BETA_IF}`,
+ 'steps > Refuse a beta the build job resolved differently > env > BUILD_VERSION: needs.build.outputs.version',
+ `steps > Attest production reader before release > if: ${RUN_IF}`,
+ `steps > Tag + create prerelease GitHub Release > if: ${BETA_IF}`,
+ 'steps > Tag + create prerelease GitHub Release > env > NOTES_B64: needs.build.outputs.notes_b64',
+ `steps > Trigger desktop-release.yml to build + upload the desktop installers > if: ${BETA_IF}`,
+ ]);
+ expect(reads('publish')).toEqual([
+ `if: ${RUN_IF}`,
+ `steps > ${PUBLISH_STEP} > env > BETA_VERSION: needs.release.outputs.version`,
+ ]);
+ const { build, release } = releaseWorkflow.jobs;
+ const algorithm = (step) => commands(step).slice(commands(step).indexOf('MAX_N=-1'));
+ expect(algorithm(stepNamed(release, RESOLVE)).length).toBeGreaterThan(0);
+ expect(algorithm(stepNamed(release, RESOLVE))).toBe(algorithm(stepNamed(build, RESOLVE)));
+ expect(commands(stepNamed(release, RESOLVE))).not.toContain('::error::');
+ expect(stepNamed(release, 'Checkout').with.ref).toBe('${{ github.sha }}');
+ for (const name of ['Tag + create prerelease GitHub Release', 'Trigger desktop-release.yml to build + upload the desktop installers']) {
+ expect(stepNamed(release, name).env.TAG).toBe('${{ steps.resolve-beta.outputs.tag }}');
+ }
+ expect(release.outputs).toEqual({ version: '${{ steps.resolve-beta.outputs.version }}' });
+ });
+
+ test("the release job checks the build job's versions before any step prints them", () => {
+ const { release } = releaseWorkflow.jobs;
+ const validate = stepNamed(release, VALIDATE_STEP);
+ expect(validate.if).toBe(BETA_IF);
+ const readers = steps(release).filter(
+ (step) => step !== validate && /needs\.build\.outputs\.(base_version|version)\b/.test(JSON.stringify(step.env ?? {})),
+ );
+ expect(readers.map((step) => step.name)).toEqual([
+ 'Guard - beta base must lead the latest stable',
+ 'Resolve -beta.N counter',
+ 'Refuse a beta the build job resolved differently',
+ ]);
+ for (const step of readers) expect(steps(release).indexOf(validate), step.name).toBeLessThan(steps(release).indexOf(step));
+ const run = (base, version) => runBash(validate.run, { PATH: process.env.PATH, BASE_VERSION: base, BUILD_VERSION: version });
+ expect(run('0.82.0', '0.82.0-beta.9').status).toBe(0);
+ for (const [base, version] of [
+ ['0.82.0\n::warning::injected', '0.82.0-beta.9'],
+ ['0.82.0', '0.82.0-beta.9\n::warning::injected'],
+ ['', '0.82.0-beta.9'],
+ ['0.82.0', '0.82.0'],
+ ]) {
+ const refused = run(base, version);
+ expect(refused.status, JSON.stringify([base, version])).toBe(1);
+ expect(refused.stdout).not.toContain('injected');
+ expect(refused.stdout).toMatch(/^::error::The build job's (base_version|version) is not/m);
+ }
+ });
+
+ describe('the release job pushes its tag with the job token through a one-command credential helper', () => {
+ const TAG_STEP = 'Tag + create prerelease GitHub Release';
+ const TAG = 'v0.82.0-beta.9';
+ const TOKEN = 'placeholder-job-token';
+ const root = mkdtempSync(join(tmpdir(), 'release-tag-push-'));
+ afterAll(() => rmSync(root, { recursive: true, force: true }));
+ const bin = join(root, 'bin');
+ mkdirSync(bin);
+ const recorder = (exitWhen) =>
+ [
+ '#!/usr/bin/env node',
+ "const { appendFileSync } = require('node:fs');",
+ 'const args = process.argv.slice(2);',
+ "appendFileSync(process.env.CALL_LOG, `${JSON.stringify([require('node:path').basename(process.argv[1]), ...args])}\\n`);",
+ `process.exit(${exitWhen} ? 1 : 0);`,
+ '',
+ ].join('\n');
+ writeFileSync(join(bin, 'git'), recorder("args[0] === 'rev-parse'"), { mode: 0o755 });
+ writeFileSync(join(bin, 'gh'), recorder("args[0] === 'release' && args[1] === 'view'"), { mode: 0o755 });
+ let runs = 0;
+ const tagStepRun = (workflow) => {
+ runs += 1;
+ const step = stepNamed(workflow.jobs.release, TAG_STEP);
+ const log = join(root, `calls-${runs}.log`);
+ const env = { PATH: `${bin}:${process.env.PATH}`, CALL_LOG: log, TMPDIR: root };
+ const known = { GH_TOKEN: TOKEN, TAG, NOTES_B64: Buffer.from('Notes.\n').toString('base64') };
+ for (const name of Object.keys(step.env ?? {})) env[name] = known[name];
+ const result = runBash(step.run, env);
+ const calls = readFileSync(log, 'utf8').trim().split('\n').map((line) => JSON.parse(line));
+ return { result, calls, stepEnv: step.env ?? {} };
+ };
+ const credentialFill = (configured, env) =>
+ execFileSync('git', [...configured, 'credential', 'fill'], {
+ input: 'protocol=https\nhost=github.com\n\n',
+ encoding: 'utf8',
+ env: { PATH: process.env.PATH, HOME: root, GIT_CONFIG_NOSYSTEM: '1', GIT_TERMINAL_PROMPT: '0', ...env },
+ });
+
+ test('the push resets every configured helper and installs the App-style helper, and nothing else carries a credential', async () => {
+ const { APP_CREDENTIAL_HELPER } = await import('./point-release-plan.mjs');
+ const { result, calls } = tagStepRun(releaseWorkflow);
+ expect(result.status, result.stderr).toBe(0);
+ const pushes = calls.filter((call) => call[0] === 'git' && call.includes('push'));
+ expect(pushes).toEqual([
+ ['git', '-c', 'credential.helper=', '-c', `credential.helper=${APP_CREDENTIAL_HELPER}`, 'push', 'origin', TAG],
+ ]);
+ });
+
+ test('the helper answers with the GH_TOKEN the step is given, and a helper configured beforehand is not consulted', () => {
+ const { calls, stepEnv } = tagStepRun(releaseWorkflow);
+ expect(stepEnv.GH_TOKEN).toBe('${{ secrets.GITHUB_TOKEN }}');
+ const push = calls.find((call) => call[0] === 'git' && call.includes('push'));
+ const configured = push.slice(1, push.indexOf('push'));
+ const env = Object.fromEntries(Object.keys(stepEnv).filter((name) => name === 'GH_TOKEN').map((name) => [name, TOKEN]));
+ expect(credentialFill(configured, env)).toContain(`username=x-access-token\npassword=${TOKEN}\n`);
+ const preconfigured = ['-c', 'credential.helper=!f() { echo username=someone-else; echo password=persisted-credential; }; f'];
+ const filled = credentialFill([...preconfigured, ...configured], env);
+ expect(filled).not.toContain('persisted-credential');
+ expect(filled).toContain(`password=${TOKEN}\n`);
+ });
+ });
+
+ test('the release job refuses a beta the build job resolved differently', () => {
+ const refusal = stepNamed(releaseWorkflow.jobs.release, 'Refuse a beta the build job resolved differently');
+ expect(refusal.env.RESOLVED).toBe('${{ steps.resolve-beta.outputs.version }}');
+ const run = (resolved, built) =>
+ runBash(refusal.run, { PATH: process.env.PATH, RESOLVED: resolved, BUILD_VERSION: built });
+ expect(run('0.82.0-beta.9', '0.82.0-beta.9').status).toBe(0);
+ const refused = run('0.82.0-beta.10', '0.82.0-beta.9');
+ expect(refused.status).toBe(1);
+ expect(refused.stdout).toContain('::error::The build job packed 0.82.0-beta.9, but this job resolved 0.82.0-beta.10');
+ expect(refused.stdout).toContain('Re-run all jobs');
+ });
+
+ describe('the publish step publishes only the expected package from the downloaded tarball', () => {
+ const publishStep = () => stepNamed(releaseWorkflow.jobs.publish, PUBLISH_STEP);
+ const root = mkdtempSync(join(tmpdir(), 'release-publish-step-'));
+ afterAll(() => rmSync(root, { recursive: true, force: true }));
+ const bin = join(root, 'bin');
+ mkdirSync(bin);
+ const nodeGlobalRoot = join(dirname(process.execPath), '..', 'lib', 'node_modules');
+ writeFileSync(
+ join(bin, 'npm'),
+ '#!/bin/sh\nprintf \'%s\\n\' "$*" >> "$NPM_LOG"\nif [ "$1" = root ]; then printf \'%s\\n\' "$NPM_GLOBAL_ROOT"; exit 0; fi\nif [ "$1" = view ]; then [ -n "$NPM_VIEW" ] || exit 1; printf \'%s\\n\' "$NPM_VIEW"; fi\nexit 0\n',
+ { mode: 0o755 },
+ );
+ const { gzipSync } = createRequire(import.meta.url)('node:zlib');
+ const ZERO_BLOCK = Buffer.alloc(512);
+ const tarEntry = (name, content) => {
+ const body = Buffer.from(typeof content === 'string' ? content : JSON.stringify(content));
+ const header = Buffer.alloc(512);
+ const put = (text, offset, length) => header.write(text, offset, length, 'ascii');
+ put(name, 0, 100);
+ put('0000644\0', 100, 8);
+ put('0000000\0', 108, 8);
+ put('0000000\0', 116, 8);
+ put(`${body.length.toString(8).padStart(11, '0')}\0`, 124, 12);
+ put('00000000000\0', 136, 12);
+ put(' ', 148, 8);
+ put('0', 156, 1);
+ put('ustar\0', 257, 6);
+ put('00', 263, 2);
+ put(`${header.reduce((total, byte) => total + byte, 0).toString(8).padStart(6, '0')}\0 `, 148, 8);
+ return Buffer.concat([header, body, Buffer.alloc((512 - (body.length % 512)) % 512)]);
+ };
+ const tgz = (...blocks) => gzipSync(Buffer.concat([...blocks, ZERO_BLOCK, ZERO_BLOCK]));
+ let cases = 0;
+ const publishWith = ({ tarballs, action = '', beta = '0.82.0-beta.9', stable = '', view = '' }) => {
+ cases += 1;
+ const dir = join(root, `case-${cases}`);
+ const packageDir = join(dir, 'npm-package');
+ mkdirSync(packageDir, { recursive: true });
+ for (const [index, tarball] of tarballs.entries()) {
+ if (Buffer.isBuffer(tarball)) {
+ writeFileSync(join(packageDir, `package-${index}.tgz`), tarball);
+ continue;
+ }
+ const source = join(dir, `source-${index}`);
+ const { files, members } = tarball.files ? tarball : { files: { 'package/package.json': tarball }, members: ['package'] };
+ for (const [path, content] of Object.entries(files)) {
+ mkdirSync(dirname(join(source, path)), { recursive: true });
+ writeFileSync(join(source, path), typeof content === 'string' ? content : JSON.stringify(content));
+ }
+ execFileSync('tar', ['-czf', join(packageDir, `package-${index}.tgz`), '-C', source, ...members]);
+ }
+ const log = join(dir, 'npm.log');
+ const step = publishStep();
+ const result = runBash(step.run, {
+ PATH: `${bin}:${process.env.PATH}`,
+ ACTION: action,
+ BETA_VERSION: beta,
+ STABLE_VERSION: stable,
+ PACKAGE_NAME: step.env.PACKAGE_NAME,
+ PACKAGE_DIR: packageDir,
+ NPM_LOG: log,
+ NPM_VIEW: view,
+ NPM_GLOBAL_ROOT: nodeGlobalRoot,
+ HTTPS_PROXY: 'http://127.0.0.1:9',
+ HTTP_PROXY: 'http://127.0.0.1:9',
+ https_proxy: 'http://127.0.0.1:9',
+ http_proxy: 'http://127.0.0.1:9',
+ NO_PROXY: '',
+ });
+ const calls = existsSync(log) ? readFileSync(log, 'utf8').trim().split('\n') : [];
+ return { ...result, calls, publishes: calls.filter((call) => call.startsWith('publish ')), packageDir };
+ };
+ const cli = (version, extra = {}) => ({ name: '@inkeep/open-knowledge', version, publishConfig: { access: 'public' }, ...extra });
+
+ test('the stub npm hands the step a real pacote, the one bundled with the npm that runs these tests', () => {
+ expect(existsSync(join(nodeGlobalRoot, 'npm', 'node_modules', 'pacote', 'package.json'))).toBe(true);
+ expect(commands(publishStep())).toContain('pacote="$(npm root -g)/npm/node_modules/pacote"');
+ expect(commands(publishStep())).not.toMatch(/\btar\s/);
+ });
+
+ test('the step passes provenance and the registry as CLI flags, which publishConfig cannot override, and names the package it checks', () => {
+ expect(commands(publishStep()).split('\n').map((line) => line.trim()).filter((line) => /\bnpm\s+publish\b/.test(line))).toEqual([TARBALL_PUBLISH]);
+ expect(publishStep().env).not.toHaveProperty('NPM_CONFIG_PROVENANCE');
+ expect(publishStep().env.PACKAGE_NAME).toBe('@inkeep/open-knowledge');
+ });
+
+ test('a well-formed tarball built byte by byte publishes, so the zero-block row is refused for its hidden manifest alone', () => {
+ const result = publishWith({ tarballs: [tgz(tarEntry('package/package.json', cli('0.82.0-beta.9')), tarEntry('package/index.js', 'module.exports = 1;\n'))] });
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.publishes).toHaveLength(1);
+ });
+
+ test('a beta publishes the downloaded tarball under the beta tag', () => {
+ const result = publishWith({ tarballs: [cli('0.82.0-beta.9')] });
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.publishes).toEqual([
+ `publish ${join(result.packageDir, 'package-0.tgz')} --access public --tag beta --provenance --registry https://registry.npmjs.org/`,
+ ]);
+ });
+
+ test('a stable publishes the dispatched version under the latest tag', () => {
+ const result = publishWith({ tarballs: [cli('0.82.0')], action: 'publish-stable', beta: '', stable: '0.82.0' });
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.publishes).toEqual([
+ `publish ${join(result.packageDir, 'package-0.tgz')} --access public --tag latest --provenance --registry https://registry.npmjs.org/`,
+ ]);
+ });
+
+ test.each([
+ ['a v-prefixed version', cli('v0.82.0-beta.9')],
+ ['build metadata on the version', cli('0.82.0-beta.9+build.1')],
+ ['a padded name', { ...cli('0.82.0-beta.9'), name: ' @inkeep/open-knowledge ' }],
+ ])('the step checks %s as the cleaned manifest npm publishes, not the raw bytes', (_, manifest) => {
+ const result = publishWith({ tarballs: [manifest] });
+ expect(result.status, result.stdout).toBe(0);
+ expect(result.publishes).toHaveLength(1);
+ });
+
+ test('a version already on npm is skipped, as changeset publish skipped it', () => {
+ const result = publishWith({ tarballs: [cli('0.82.0-beta.9')], view: '0.82.0-beta.9' });
+ expect(result.status).toBe(0);
+ expect(result.stdout).toContain('::notice::@inkeep/[email protected] is already on npm');
+ expect(result.publishes).toEqual([]);
+ });
+
+ test.each([
+ ['a different version', { tarballs: [cli('0.82.0-beta.8')] }, '"version":"0.82.0-beta.8"'],
+ ['a different package', { tarballs: [{ ...cli('0.82.0-beta.9'), name: '@inkeep/open-knowledge-core' }] }, '"name":"@inkeep/open-knowledge-core"'],
+ ['a publishConfig that redirects the registry', { tarballs: [cli('0.82.0-beta.9', { publishConfig: { access: 'public', '@inkeep:registry': 'https://registry.example.test/' } })] }, 'whose publishConfig may set access and nothing else'],
+ ['a publishConfig that turns provenance off', { tarballs: [cli('0.82.0-beta.9', { publishConfig: { access: 'public', provenance: false } })] }, 'whose publishConfig may set access and nothing else'],
+ [
+ 'a second manifest that npm would read instead',
+ { tarballs: [{ files: { 'package/package.json': cli('0.82.0-beta.9'), 'x/package.json': cli('0.82.0-beta.9', { publishConfig: { provenance: false } }) }, members: ['package', 'x'] }] },
+ '"publishConfig":{"provenance":false}',
+ ],
+ [
+ 'a later package/package.json that differs',
+ {
+ tarballs: [
+ tgz(
+ tarEntry('package/package.json', cli('0.82.0-beta.9')),
+ tarEntry('package/package.json', cli('9.9.9', { publishConfig: { access: 'public', tag: 'latest' } })),
+ ),
+ ],
+ },
+ '"version":"9.9.9"',
+ ],
+ [
+ 'a second package/package.json hidden behind one zero block',
+ {
+ tarballs: [
+ tgz(
+ tarEntry('package/package.json', cli('0.82.0-beta.9')),
+ tarEntry('package/index.js', 'module.exports = 1;\n'),
+ ZERO_BLOCK,
+ tarEntry('package/package.json', cli('9.9.9', { publishConfig: { access: 'public', tag: 'latest' } })),
+ ),
+ ],
+ },
+ '"version":"9.9.9"',
+ ],
+ ['two tarballs', { tarballs: [cli('0.82.0-beta.9'), cli('0.82.0-beta.9')] }, 'holds 2 tarballs, not one'],
+ ['no tarball', { tarballs: [] }, 'holds 0 tarballs, not one'],
+ ])('the step refuses %s', (_, input, message) => {
+ const result = publishWith(input);
+ expect(result.status).toBe(1);
+ expect(result.stdout).toContain(message);
+ expect(result.publishes).toEqual([]);
+ });
+
+ test.each([
+ ['name', { ...cli('0.82.0-beta.9'), name: 'evil\n::warning::injected' }],
+ ['version', cli('0.82.0-beta.9\n::warning::injected')],
+ ['publishConfig', cli('0.82.0-beta.9', { publishConfig: { access: 'public', 'x\n::warning::injected': true } })],
+ ])('a manifest %s carrying a newline cannot start a workflow command in the refusal', (_, manifest) => {
+ const result = publishWith({ tarballs: [manifest] });
+ expect(result.status).toBe(1);
+ expect(result.publishes).toEqual([]);
+ const lines = `${result.stdout}\n${result.stderr}`.split('\n');
+ expect(lines.filter((line) => line.startsWith('::warning::'))).toEqual([]);
+ expect(lines.some((line) => line.startsWith('::error::'))).toBe(true);
+ });
+ });
+
+ describe('read-releases hands build the previous beta that a contents: write read sees, drafts included', () => {
+ const READ_STEP = 'Read the newest beta Release, drafts included, and its body';
+ const COMPUTE_STEP = 'Compute next beta base version + render release notes';
+ const BETA_PATH = "github.event_name == 'push' || github.event_name == 'workflow_dispatch'";
+
+ test('read-releases always runs, runs only gh release list and view, and build takes its outputs under their own names', () => {
+ const { 'read-releases': readReleases, build } = releaseWorkflow.jobs;
+ expect(readReleases.if).toBeUndefined();
+ expect(readReleases.needs).toBeUndefined();
+ expect(steps(readReleases).map((step) => step.name)).toEqual([READ_STEP]);
+ const read = stepNamed(readReleases, READ_STEP);
+ expect(read.uses).toBeUndefined();
+ expect(read.if).toBe(BETA_PATH);
+ expect(read.if).toBe(stepNamed(build, COMPUTE_STEP).if);
+ expect(read.env).toEqual({ GH_TOKEN: '${{ github.token }}' });
+ expect(commands(read).match(/\bgh\s+\S+\s+\S+/g)).toEqual(['gh release list', 'gh release view']);
+ expect(build.needs).toBe('read-releases');
+ const names = Object.keys(readReleases.outputs);
+ expect(names.length).toBeGreaterThan(0);
+ for (const name of names) {
+ expect(readReleases.outputs[name]).toBe(`\${{ steps.read.outputs.${name} }}`);
+ expect(commands(read)).toContain(`record ${name} `);
+ }
+ const reads = expressions(build)
+ .filter(({ expr }) => /\bneeds\b/.test(expr))
+ .map(({ path, expr }) => `${(path[0] === 'steps' ? ['steps', build.steps[path[1]].name, ...path.slice(2)] : path).join(' > ')}: ${expr}`);
+ expect(reads).toEqual(
+ names.map((name) => `steps > ${COMPUTE_STEP} > env > ${name.toUpperCase()}: needs.read-releases.outputs.${name}`),
+ );
+ });
+
+ const root = mkdtempSync(join(tmpdir(), 'release-read-releases-'));
+ afterAll(() => rmSync(root, { recursive: true, force: true }));
+ const bin = join(root, 'bin');
+ mkdirSync(bin);
+ const stub = join(bin, 'gh');
+ writeFileSync(
+ stub,
+ [
+ '#!/usr/bin/env node',
+ "const { appendFileSync } = require('node:fs');",
+ 'const args = process.argv.slice(2);',
+ "appendFileSync(process.env.STUB_LOG, `${JSON.stringify(args)}\\n`);",
+ 'const world = JSON.parse(process.env.STUB_WORLD);',
+ 'const failure = (world.fail ?? {})[args[1]];',
+ "if (failure) { process.stderr.write(failure.stderr ?? ''); process.exit(failure.status); }",
+ "const visible = world.releases.filter((release) => world.token === 'write' || !release.isDraft);",
+ "if (args[1] === 'list') {",
+ ' const first = visible.find((release) => release.isPrerelease && /^v[0-9]+\\.[0-9]+\\.[0-9]+-beta\\.[0-9]+$/.test(release.tagName));',
+ " process.stdout.write(`${first ? first.tagName : ''}\\n`);",
+ ' process.exit(0);',
+ '}',
+ 'const release = visible.find((candidate) => candidate.tagName === args[2]);',
+ "if (!release) { process.stderr.write('release not found\\n'); process.exit(1); }",
+ 'process.stdout.write(`${release.body}\\n`);',
+ '',
+ ].join('\n'),
+ { mode: 0o755 },
+ );
+ let runs = 0;
+ const parseOutputs = (text) => {
+ const out = {};
+ const lines = text.split('\n');
+ for (let i = 0; i < lines.length; i++) {
+ const heredoc = /^([^=<]+)<<(.+)$/.exec(lines[i]);
+ if (heredoc) {
+ const end = lines.indexOf(heredoc[2], i + 1);
+ out[heredoc[1]] = lines.slice(i + 1, end).join('\n');
+ i = end;
+ } else if (lines[i].includes('=')) {
+ out[lines[i].slice(0, lines[i].indexOf('='))] = lines[i].slice(lines[i].indexOf('=') + 1);
+ }
+ }
+ return out;
+ };
+ const throughTheWorkflow = (workflow, world, repo) => {
+ runs += 1;
+ const dir = join(root, `run-${runs}`);
+ mkdirSync(dir);
+ const outputFile = join(dir, 'github-output');
+ writeFileSync(outputFile, '');
+ const log = join(dir, 'gh-calls.log');
+ const read = stepNamed(workflow.jobs['read-releases'], READ_STEP);
+ const result = runBash(read.run, {
+ PATH: `${bin}:${process.env.PATH}`,
+ GITHUB_OUTPUT: outputFile,
+ GITHUB_REPOSITORY: repo,
+ GH_TOKEN: 'stub',
+ STUB_LOG: log,
+ STUB_WORLD: JSON.stringify({ ...world, token: 'write' }),
+ });
+ expect(result.status, result.stderr).toBe(0);
+ const stepOutputs = parseOutputs(readFileSync(outputFile, 'utf8'));
+ const jobOutputs = Object.fromEntries(
+ Object.entries(workflow.jobs['read-releases'].outputs).map(([name, expression]) => [
+ name,
+ stepOutputs[/^\$\{\{ steps\.read\.outputs\.([\w-]+) \}\}$/.exec(expression)?.[1]] ?? '',
+ ]),
+ );
+ const env = Object.fromEntries(
+ Object.entries(stepNamed(workflow.jobs.build, COMPUTE_STEP).env ?? {}).flatMap(([name, expression]) => {
+ const output = /^\$\{\{ needs\.read-releases\.outputs\.([\w-]+) \}\}$/.exec(expression)?.[1];
+ return output ? [[name, jobOutputs[output] ?? '']] : [];
+ }),
+ );
+ const calls = readFileSync(log, 'utf8').trim().split('\n').map((line) => JSON.parse(line));
+ return { env, calls };
+ };
+ const directly = (world, token) => (args) => {
+ try {
+ return {
+ status: 0,
+ stdout: execFileSync(stub, args, {
+ encoding: 'utf8',
+ env: { PATH: process.env.PATH, STUB_LOG: join(root, 'direct.log'), STUB_WORLD: JSON.stringify({ ...world, token }) },
+ stdio: ['ignore', 'pipe', 'pipe'],
+ }),
+ stderr: '',
+ };
+ } catch (error) {
+ if (typeof error.status !== 'number') throw error;
+ return { status: error.status, stdout: error.stdout, stderr: error.stderr };
+ }
+ };
+ const marker = (ids) => `Notes for the beta.\n\n<!-- ok-consumed-set: ${JSON.stringify(ids)} -->\n\n`;
+ const beta = (tagName, body, isDraft = false) => ({ tagName, isDraft, isPrerelease: true, body });
+ const draftNewest = {
+ releases: [
+ beta('v0.82.0-beta.9', marker(['a', 'b']), true),
+ beta('v0.82.0-beta.8', marker(['a'])),
+ { tagName: 'v0.81.4', isDraft: false, isPrerelease: false, body: 'Stable.' },
+ ],
+ };
+ const worlds = {
+ 'the previous beta is still a draft': draftNewest,
+ 'the list fails': { releases: draftNewest.releases, fail: { list: { status: 1, stderr: 'HTTP 502: Bad Gateway\n' } } },
+ 'there is no beta Release': { releases: [{ tagName: 'v0.81.4', isDraft: false, isPrerelease: false, body: 'Stable.' }] },
+ 'the view fails': { releases: draftNewest.releases, fail: { view: { status: 1, stderr: 'HTTP 404\n' } } },
+ 'the body has no marker': { releases: [beta('v0.82.0-beta.9', 'Notes without a marker.\n')] },
+ 'the marker is not JSON': { releases: [beta('v0.82.0-beta.9', 'Notes.\n<!-- ok-consumed-set: [a, b] -->')] },
+ 'the marker is not a string array': { releases: [beta('v0.82.0-beta.9', 'Notes.\n<!-- ok-consumed-set: [1, 2] -->')] },
+ };
+
+ test.each(Object.keys(worlds))('when %s, build replays exactly what a contents: write gh read returns', async (name) => {
+ const { previousBeta, recordedReleases, RELEASE_LIST_ARGS, releaseViewArgs } = await import('../../scripts/compute-next-beta.mjs');
+ const world = worlds[name];
+ const { env, calls } = throughTheWorkflow(releaseWorkflow, world, RELEASE_LIST_ARGS[3]);
+ const replayed = previousBeta(recordedReleases(env));
+ expect(replayed).toEqual(previousBeta(directly(world, 'write')));
+ expect(calls).toEqual(replayed.prevBetaTag ? [RELEASE_LIST_ARGS, releaseViewArgs(replayed.prevBetaTag)] : [RELEASE_LIST_ARGS]);
+ });
+
+ test('with the previous beta still a draft, the replay is the draft and not what a contents: read token sees', async () => {
+ const { previousBeta, recordedReleases, RELEASE_LIST_ARGS } = await import('../../scripts/compute-next-beta.mjs');
+ const { env } = throughTheWorkflow(releaseWorkflow, draftNewest, RELEASE_LIST_ARGS[3]);
+ expect(previousBeta(recordedReleases(env))).toEqual({ prevBetaTag: 'v0.82.0-beta.9', recovered: ['a', 'b'] });
+ expect(previousBeta(directly(draftNewest, 'read'))).toEqual({ prevBetaTag: 'v0.82.0-beta.8', recovered: ['a'] });
+ });
+ });
+
+ test('the build job packs the only public workspace package and runs the only publish-time script it declares', () => {
+ const patterns = parse(readFileSync(join(OK_ROOT, 'pnpm-workspace.yaml'), 'utf8')).packages;
+ const dirs = patterns.flatMap((pattern) => {
+ if (pattern.endsWith('/*') && !/[*?{}[\]!]/.test(pattern.slice(0, -2))) {
+ const parent = pattern.slice(0, -2);
+ return readdirSync(join(OK_ROOT, parent), { withFileTypes: true })
+ .filter((entry) => entry.isDirectory())
+ .map((entry) => `${parent}/${entry.name}`);
+ }
+ if (/[*?{}[\]!]/.test(pattern)) throw new Error(`workspace pattern ${pattern} needs a matcher this test does not have`);
+ return [pattern];
+ });
+ const manifests = dirs
+ .filter((dir) => existsSync(join(OK_ROOT, dir, 'package.json')))
+ .map((dir) => ({ dir, manifest: JSON.parse(readFileSync(join(OK_ROOT, dir, 'package.json'), 'utf8')) }));
+ expect(manifests.filter(({ manifest }) => !manifest.private).map(({ dir, manifest }) => `${dir} ${manifest.name}`)).toEqual([
+ 'packages/cli @inkeep/open-knowledge',
+ ]);
+ const cli = manifests.find(({ dir }) => dir === 'packages/cli').manifest;
+ expect(Object.keys(cli.scripts).filter((name) => ['prepublishOnly', 'prepublish', 'publish', 'postpublish'].includes(name))).toEqual([
+ 'prepublishOnly',
+ ]);
+ const { build } = releaseWorkflow.jobs;
+ for (const name of ["Run the cli's prepublishOnly against the overridden versions", 'Pack the cli tarball']) {
+ expect(stepNamed(build, name)['working-directory'], name).toBe('packages/cli');
+ }
+ });
+ });
});
describe('Linux packaging ships the native prebuilds and checks them before upload', () => {
diff --git a/.github/scripts/share-contract-reader-gates-shape.test.mjs b/.github/scripts/share-contract-reader-gates-shape.test.mjs
index f56b25043..6c3dcd3a5 100644
--- a/.github/scripts/share-contract-reader-gates-shape.test.mjs
+++ b/.github/scripts/share-contract-reader-gates-shape.test.mjs
@@ -2,6 +2,7 @@ import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, test } from 'vitest';
+import { parse } from 'yaml';
const githubDir = join(dirname(fileURLToPath(import.meta.url)), '..');
@@ -29,7 +30,7 @@ const releaseLanes = [
gate: 'Attest production reader before release',
mutation: ' - name: Tag + create prerelease GitHub Release',
condition:
- "if: github.event.action == 'publish-stable' || steps.compute-beta.outputs.run_beta == 'true'",
+ "if: github.event.action == 'publish-stable' || needs.build.outputs.run_beta == 'true'",
},
{
name: 'stable promotion',
@@ -136,18 +137,20 @@ describe.each(releaseLanes)('$name reader attestation', ({
});
describe('stable release reader attestation', () => {
- test('restores the local composite action and its inputs from the workflow revision', () => {
- const release = readGithubFile('workflows', 'release.yml');
- const restore = stepBlock(release, 'Restore reader gate from workflow revision');
- expect(restore).toContain("if: github.event.action == 'publish-stable'");
- expect(restore).toContain('git checkout "$GITHUB_SHA" --');
- expect(restore).toContain('.github/composite-actions/share-contract-reader-gate/action.yml');
- expect(restore).toContain('.github/scripts/probe-share-contract.mjs');
- expect(restore).toContain('test-support/fixtures/share-url-v1-v2.json');
- const restoreAt = release.indexOf('- name: Restore reader gate from workflow revision');
- const attestAt = release.indexOf('- name: Attest production reader before release');
- expect(restoreAt, 'no "Restore reader gate" step').toBeGreaterThan(-1);
- expect(attestAt, 'no "Attest production reader" step').toBeGreaterThan(-1);
- expect(restoreAt).toBeLessThan(attestAt);
+ test('runs the gate in the one job that checks out the workflow revision rather than the stable tag', () => {
+ const gateAction = './.github/composite-actions/share-contract-reader-gate';
+ const { jobs } = parse(readGithubFile('workflows', 'release.yml'));
+ const gateJobs = Object.entries(jobs).filter(([, job]) =>
+ (job.steps ?? []).some((step) => step.uses === gateAction),
+ );
+ expect(gateJobs.map(([id]) => id)).toEqual(['release']);
+ const { steps } = gateJobs[0][1];
+ const gateAt = steps.findIndex((step) => step.uses === gateAction);
+ const checkouts = steps.filter((step) => step.uses?.startsWith('actions/checkout@'));
+ expect(checkouts).toHaveLength(1);
+ expect(checkouts[0].with.ref).toBe(workflowExpression('github.sha'));
+ expect(steps.indexOf(checkouts[0])).toBeLessThan(gateAt);
+ const beforeGate = steps.slice(0, gateAt).map((step) => step.run ?? '').join('\n');
+ expect(beforeGate).not.toMatch(/\bgit\s+(checkout|switch|restore|reset)\b/);
});
});
diff --git a/.github/workflows/promote-stable.yml b/.github/workflows/promote-stable.yml
index 3994259bc..cdb6452d7 100644
--- a/.github/workflows/promote-stable.yml
+++ b/.github/workflows/promote-stable.yml
@@ -94,8 +94,7 @@ permissions:
# `Generate GitHub App token` step). npm publish (id-token) is delegated
# to release.yml via repository_dispatch so this workflow does not need
# id-token; the dispatched run keeps its own OIDC identity. No packages:
- # write — see release.yml's permission comment for why npm publishing
- # does not need that scope here.
+ # write.
contents: write
# Read-only: lets the dispatch steps resolve the run URLs of the workflows they
# trigger (release.yml, desktop-release.yml, main-reset) for run-to-run links.
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 249e1274c..8878fbaf1 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -26,8 +26,7 @@ name: Release
# Fires when Copybara mirrors a commit touching `.changeset/**`.
# Workflow:
# - Runs `compute-next-beta.mjs` to read `.changeset/*.md` + `pre.json`,
-# recover the previous beta's consumed-set marker from
-# `gh release view inkeep/open-knowledge`, compute the delta,
+# compute the delta,
# and emit { baseVersion, maxBumpType, releaseNotes }. Empty delta
# (e.g., Copybara import that only touched non-changeset files
# matching the paths filter) → skip cleanly.
@@ -39,14 +38,12 @@ name: Release
# - Tags vX.Y.Z-beta.N at `github.sha` + creates a prerelease GitHub
# Release with the rendered notes (consumed-set marker embedded
# for the next cut to read).
-# - Publishes @inkeep/[email protected] to npm with
-# `--tag beta --provenance`.
# - Dispatches desktop-release.yml so the signed DMG +
# `beta-mac.yml` land on the prerelease.
#
# 2. event_name=workflow_dispatch (manual recovery):
# Same code path as the push trigger. Used after a transient flake
-# (e.g., npm publish failed mid-run) to re-fire from the current
+# to re-fire from the current
# main HEAD without waiting for a new mirror import.
#
# 3. repository_dispatch type=publish-stable (fired by promote-stable.yml
@@ -69,9 +66,8 @@ name: Release
# Why explicit `repository_dispatch` for the beta path here instead of
# relying on `release: published` to cascade: GitHub deliberately
# suppresses downstream workflow runs from GITHUB_TOKEN-authored
-# events. This workflow's `gh release create` runs under GITHUB_TOKEN
-# (the beta path stays on the workflow-default token), so a `release:
-# published` event from it would NOT cascade to desktop-release.yml.
+# events. This workflow's `gh release create` runs under GITHUB_TOKEN,
+# so a `release: published` event from it would NOT cascade to desktop-release.yml.
# `repository_dispatch` and `workflow_dispatch` are the documented
# exemption that cascade even when fired via GITHUB_TOKEN.
#
@@ -82,9 +78,7 @@ name: Release
# documents that path.
#
# Why ONE workflow handles both beta and stable: npm Trusted Publishing
-# only allows ONE trusted publisher per package. Centralizing all
-# `pnpm exec changeset publish` calls here means there is exactly one
-# trusted-publisher entry on npmjs.com (workflow=release.yml). Both
+# only allows ONE trusted publisher per package. Both
# event-driven betas and promote-stable route their npm publishes here.
#
# Concurrency group is shared with promote-stable.yml so beta cuts and
@@ -100,19 +94,7 @@ on:
types: [publish-stable]
permissions:
- # contents: write — for beta path's git tag + Release creation.
- # id-token: write — for npm OIDC. Used both for provenance attestation
- # (the SLSA Build L3 attestation surfaced to consumers) AND for npm
- # Trusted Publishing — the OIDC token's repo + workflow claims
- # authenticate the publish without an NPM_TOKEN secret. The trusted-
- # publisher entry is configured on npmjs.com against
- # inkeep/open-knowledge + this workflow filename. Only ONE
- # entry per package; both beta-cut and promote-stable route through
- # here to share this entry.
- # No packages: write — npm publishes go to npmjs.org, not GitHub
- # Packages (ghcr.io / npm.pkg.github.com).
- contents: write
- id-token: write
+ contents: read
concurrency:
group: ok-release-cadence
@@ -122,10 +104,67 @@ env:
TURBO_TELEMETRY_DISABLED: 1
jobs:
- release:
- name: Tag + publish (${{ github.event.action || github.event_name }})
+ read-releases:
+ name: Read the previous beta Release
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ permissions:
+ contents: write
+ outputs:
+ release_list_status: ${{ steps.read.outputs.release_list_status }}
+ release_list_stdout: ${{ steps.read.outputs.release_list_stdout }}
+ release_list_stderr: ${{ steps.read.outputs.release_list_stderr }}
+ release_view_tag: ${{ steps.read.outputs.release_view_tag }}
+ release_view_status: ${{ steps.read.outputs.release_view_status }}
+ release_view_stdout: ${{ steps.read.outputs.release_view_stdout }}
+ steps:
+ - name: Read the newest beta Release, drafts included, and its body
+ if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
+ id: read
+ env:
+ GH_TOKEN: ${{ github.token }}
+ run: |
+ set -euo pipefail
+ record() {
+ local delimiter
+ delimiter="ghadelimiter_$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"
+ printf '%s<<%s\n%s\n%s\n' "$1" "$delimiter" "$2" "$delimiter" >> "$GITHUB_OUTPUT"
+ }
+ list_stderr_file=$(mktemp)
+ if list_stdout=$(gh release list --repo "$GITHUB_REPOSITORY" --limit 50 --json tagName,isPrerelease --jq '[.[] | select(.isPrerelease) | select(.tagName | test("^v[0-9]+\\.[0-9]+\\.[0-9]+-beta\\.[0-9]+$")) | .tagName] | first // ""' 2>"$list_stderr_file"); then
+ list_status=0
+ else
+ list_status=$?
+ fi
+ list_stderr=$(cat "$list_stderr_file")
+ cat "$list_stderr_file" >&2
+ record release_list_status "$list_status"
+ record release_list_stdout "$list_stdout"
+ record release_list_stderr "$list_stderr"
+ if [[ "$list_status" -eq 0 && -n "$list_stdout" ]]; then
+ if view_stdout=$(gh release view "$list_stdout" --repo "$GITHUB_REPOSITORY" --json body --jq .body); then
+ view_status=0
+ else
+ view_status=$?
+ fi
+ record release_view_tag "$list_stdout"
+ record release_view_status "$view_status"
+ record release_view_stdout "$view_stdout"
+ fi
+ echo "Previous beta Release: ${list_stdout:-none} (list exit ${list_status}${view_status:+, view exit ${view_status}})"
+
+ build:
+ name: Build + pack (${{ github.event.action || github.event_name }})
+ needs: read-releases
runs-on: ubuntu-latest
timeout-minutes: 20
+ permissions:
+ contents: read
+ outputs:
+ run_beta: ${{ steps.compute-beta.outputs.run_beta }}
+ base_version: ${{ steps.compute-beta.outputs.base_version }}
+ notes_b64: ${{ steps.compute-beta.outputs.notes_b64 }}
+ version: ${{ steps.resolve-beta.outputs.version }}
steps:
- name: Link to triggering run
# Back-link: on the publish-stable path, promote-stable forwards its run
@@ -143,6 +182,7 @@ jobs:
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
+ persist-credentials: false
# publish-stable: client_payload.ref carries the stable tag
# created by promote-stable.yml (= same SHA as the source
# beta tag).
@@ -152,15 +192,6 @@ jobs:
# fix from Linear PRD-6701).
ref: ${{ github.event.client_payload.ref || github.sha }}
- - name: Restore reader gate from workflow revision
- if: github.event.action == 'publish-stable'
- run: |
- set -euo pipefail
- git checkout "$GITHUB_SHA" -- \
- .github/composite-actions/share-contract-reader-gate/action.yml \
- .github/scripts/probe-share-contract.mjs \
- test-support/fixtures/share-url-v1-v2.json
-
- uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10
- name: Get pnpm store directory
shell: bash
@@ -176,19 +207,12 @@ jobs:
restore-keys: |
${{ runner.os }}-ok-pnpm-store-
- - name: Setup Node for npm OIDC publish
+ - name: Setup Node
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
with:
node-version: "24"
- registry-url: "https://registry.npmjs.org"
- # Pinned to the 11.x line, NOT npm@latest: npm 12.0.0's published
- # tarball omits the top-level bundled `sigstore` module that
- # libnpmpublish requires, so publishing with provenance dies with
- # MODULE_NOT_FOUND the moment `latest` serves 12.x (broke the
- # 0.28.1-beta.0/.1 cuts on 2026-07-08). Bump the pin only after
- # verifying `npm publish` with NPM_CONFIG_PROVENANCE works on it.
- - name: Ensure npm 11.5.1+ for OIDC + provenance
+ - name: Ensure npm 11 for the cli's package surface check
run: |
npm install -g npm@11
echo "npm $(npm --version)"
@@ -295,13 +319,6 @@ jobs:
with:
toolchain: stable
- # `pnpm exec changeset publish` invokes `npm publish` per package, which
- # in turn fires the cli's `prepublishOnly` (-> `pnpm run build`). The
- # cli's own build chain includes `cp -r ../app/dist dist/public`
- # which silently fails when the app hasn't been built first. Run a
- # top-level turbo build (turbo orchestrates the dep order:
- # app -> cli) so every package's dist/ exists before the publish
- # step runs.
- name: Build packages
run: |
pnpm exec turbo run build --cache=local:,remote:
@@ -319,9 +336,8 @@ jobs:
# Compute baseVersion + maxBumpType + rendered release notes locally
# from the changeset pile that Copybara mirrored into the just-pushed
- # commit. compute-next-beta.mjs reads `.changeset/*.md`, `pre.json`,
- # and `gh release view <prev-beta-tag>` for the embedded consumed-set
- # marker; emits `{skip: true, reason}` when no new changesets exist
+ # commit. compute-next-beta.mjs reads `.changeset/*.md` and `pre.json`;
+ # emits `{skip: true, reason}` when no new changesets exist
# since the last beta (e.g., a Copybara import that touched
# `.changeset/config.json` but no `.md` files, or main-reset
# consolidation cycles). The script MUTATES the working tree
@@ -331,7 +347,12 @@ jobs:
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
id: compute-beta
env:
- GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ RELEASE_LIST_STATUS: ${{ needs.read-releases.outputs.release_list_status }}
+ RELEASE_LIST_STDOUT: ${{ needs.read-releases.outputs.release_list_stdout }}
+ RELEASE_LIST_STDERR: ${{ needs.read-releases.outputs.release_list_stderr }}
+ RELEASE_VIEW_TAG: ${{ needs.read-releases.outputs.release_view_tag }}
+ RELEASE_VIEW_STATUS: ${{ needs.read-releases.outputs.release_view_status }}
+ RELEASE_VIEW_STDOUT: ${{ needs.read-releases.outputs.release_view_stdout }}
run: |
set -euo pipefail
OUTPUT=$(node scripts/compute-next-beta.mjs)
@@ -415,7 +436,6 @@ jobs:
if: steps.compute-beta.outputs.run_beta == 'true'
id: resolve-beta
env:
- GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_VERSION: ${{ steps.compute-beta.outputs.base_version }}
run: |
set -euo pipefail
@@ -428,14 +448,6 @@ jobs:
exit 1
fi
- # Query existing v<BASE_VERSION>-beta.N tags on this repo and
- # find the max N. `git tag --list` only sees local tags; we
- # fetched them in the checkout step (fetch-depth: 0 pulls all
- # refs by default for actions/checkout@v7). Cross-check via
- # `gh api` for tags created by parallel runs that pushed to
- # origin since our checkout finished — the actual collision
- # window is between this step and the `git push origin TAG`
- # step below, which the concurrency group eliminates.
MAX_N=-1
while IFS= read -r tag; do
[[ -z "$tag" ]] && continue
@@ -455,13 +467,6 @@ jobs:
echo "tag=$TAG"
} >> "$GITHUB_OUTPUT"
- - name: Attest production reader before release
- if: github.event.action == 'publish-stable' || steps.compute-beta.outputs.run_beta == 'true'
- uses: ./.github/composite-actions/share-contract-reader-gate
- with:
- origin: https://openknowledge.ai
- evidence-name: share-contract-release-${{ github.run_id }}-${{ github.run_attempt }}
-
- name: Override fixed-group versions to X.Y.Z-beta.N
if: steps.compute-beta.outputs.run_beta == 'true'
env:
@@ -490,12 +495,167 @@ jobs:
done
echo "All fixed-group package.json files + both skill bundles' metadata.version now at ${VERSION} (in-memory only)."
+ - name: Validate stable_version + override package.json (publish-stable)
+ if: github.event.action == 'publish-stable'
+ env:
+ STABLE_VERSION: ${{ github.event.client_payload.version }}
+ run: |
+ set -euo pipefail
+
+ if [[ -z "${STABLE_VERSION:-}" ]]; then
+ echo "::error::publish-stable dispatch missing client_payload.version"
+ exit 1
+ fi
+ if [[ ! "$STABLE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
+ echo "::error::publish-stable client_payload.version '${STABLE_VERSION}' is not bare semver (X.Y.Z)"
+ exit 1
+ fi
+
+ for pkg in cli core server app desktop; do
+ f="packages/${pkg}/package.json"
+ jq --arg v "$STABLE_VERSION" '.version=$v' "$f" > "${f}.tmp"
+ mv "${f}.tmp" "$f"
+ done
+ for skill in packages/server/assets/skills/project/SKILL.md \
+ packages/server/assets/skills/discovery/SKILL.md; do
+ sed -i'.bak' -E "s/^([[:space:]]+version:[[:space:]]+\")[^\"]+(\".*)$/\1${STABLE_VERSION}\2/" "$skill"
+ rm -f "${skill}.bak"
+ done
+ echo "All fixed-group package.json files + both skill bundles' metadata.version now at ${STABLE_VERSION} (in-memory only)."
+
+ - name: Run the cli's prepublishOnly against the overridden versions
+ if: github.event.action == 'publish-stable' || steps.compute-beta.outputs.run_beta == 'true'
+ working-directory: packages/cli
+ run: pnpm run prepublishOnly
+
+ - name: Pack the cli tarball
+ if: github.event.action == 'publish-stable' || steps.compute-beta.outputs.run_beta == 'true'
+ working-directory: packages/cli
+ run: |
+ set -euo pipefail
+ mkdir -p "$RUNNER_TEMP/npm-package"
+ pnpm pack --pack-destination "$RUNNER_TEMP/npm-package"
+
+ - name: Upload the packed tarball for the publish job
+ if: github.event.action == 'publish-stable' || steps.compute-beta.outputs.run_beta == 'true'
+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
+ with:
+ name: npm-package
+ path: ${{ runner.temp }}/npm-package/*.tgz
+ if-no-files-found: error
+ retention-days: 30
+
+ - name: Restore working tree (defensive)
+ if: always()
+ run: git restore . || true
+
+ release:
+ name: Tag + release (${{ github.event.action || github.event_name }})
+ needs: build
+ if: github.event.action == 'publish-stable' || needs.build.outputs.run_beta == 'true'
+ runs-on: ubuntu-latest
+ timeout-minutes: 10
+ permissions:
+ contents: write
+ outputs:
+ version: ${{ steps.resolve-beta.outputs.version }}
+ steps:
+ - name: Checkout
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ fetch-depth: 0
+ persist-credentials: false
+ ref: ${{ github.sha }}
+
+ - name: Refuse build outputs that are not well-formed versions
+ if: needs.build.outputs.run_beta == 'true'
+ env:
+ BASE_VERSION: ${{ needs.build.outputs.base_version }}
+ BUILD_VERSION: ${{ needs.build.outputs.version }}
+ run: |
+ set -euo pipefail
+ if [[ ! "$BASE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
+ echo "::error::The build job's base_version is not bare semver (X.Y.Z). Refusing to use it."
+ exit 1
+ fi
+ if [[ ! "$BUILD_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+-beta\.[0-9]+$ ]]; then
+ echo "::error::The build job's version is not X.Y.Z-beta.N. Refusing to use it."
+ exit 1
+ fi
+
+ - name: Guard - beta base must lead the latest stable
+ if: needs.build.outputs.run_beta == 'true'
+ env:
+ BASE_VERSION: ${{ needs.build.outputs.base_version }}
+ run: |
+ set -euo pipefail
+ LATEST_STABLE_TAG=$(git tag --list 'v*' --sort=-v:refname \
+ | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n1 || true)
+ if [[ -z "$LATEST_STABLE_TAG" ]]; then
+ echo "No stable tag yet; skipping lead check (bootstrap)."
+ exit 0
+ fi
+ STABLE="${LATEST_STABLE_TAG#v}"
+ TOP=$(printf '%s\n%s\n' "$BASE_VERSION" "$STABLE" | sort -V | tail -n1)
+ if [[ "$TOP" != "$BASE_VERSION" ]]; then
+ echo "::error::Computed beta base ${BASE_VERSION} is BEHIND the latest stable ${STABLE} — genuine anchor drift, not the transient post-promotion window (base==stable is deferred upstream)."
+ echo "::error::Run public-open-knowledge-main-reset.yml with stable_version=${STABLE} on agents-private, then re-cut. If that reset fails its version guard, re-dispatch it with -f anchor_override=true (RELEASES.md, Recovering anchor drift)."
+ exit 1
+ fi
+ echo "Beta base ${BASE_VERSION} leads latest stable ${STABLE}."
+
+ - name: Resolve -beta.N counter
+ if: needs.build.outputs.run_beta == 'true'
+ id: resolve-beta
+ env:
+ BASE_VERSION: ${{ needs.build.outputs.base_version }}
+ run: |
+ set -euo pipefail
+ MAX_N=-1
+ while IFS= read -r tag; do
+ [[ -z "$tag" ]] && continue
+ if [[ "$tag" =~ ^v${BASE_VERSION//./\\.}-beta\.([0-9]+)$ ]]; then
+ n="${BASH_REMATCH[1]}"
+ if (( n > MAX_N )); then MAX_N="$n"; fi
+ fi
+ done < <(git tag --list "v${BASE_VERSION}-beta.*")
+
+ NEXT_N=$((MAX_N + 1))
+ VERSION="${BASE_VERSION}-beta.${NEXT_N}"
+ TAG="v${VERSION}"
+ echo "Existing max -beta.N for base ${BASE_VERSION}: ${MAX_N} -> emitting ${TAG}"
+
+ {
+ echo "version=$VERSION"
+ echo "tag=$TAG"
+ } >> "$GITHUB_OUTPUT"
+
+ - name: Refuse a beta the build job resolved differently
+ if: needs.build.outputs.run_beta == 'true'
+ env:
+ RESOLVED: ${{ steps.resolve-beta.outputs.version }}
+ BUILD_VERSION: ${{ needs.build.outputs.version }}
+ run: |
+ set -euo pipefail
+ if [[ "$RESOLVED" != "$BUILD_VERSION" ]]; then
+ echo "::error::The build job packed ${BUILD_VERSION}, but this job resolved ${RESOLVED} from the tags. Refusing to tag and publish a beta the build job did not pack. This usually means only the failed jobs were re-run after a tag landed, which reuses the build job's earlier result. Re-run all jobs."
+ exit 1
+ fi
+ echo "The build job packed the beta this job resolved: ${RESOLVED}"
+
+ - name: Attest production reader before release
+ if: github.event.action == 'publish-stable' || needs.build.outputs.run_beta == 'true'
+ uses: ./.github/composite-actions/share-contract-reader-gate
+ with:
+ origin: https://openknowledge.ai
+ evidence-name: share-contract-release-${{ github.run_id }}-${{ github.run_attempt }}
+
- name: Tag + create prerelease GitHub Release
- if: steps.compute-beta.outputs.run_beta == 'true'
+ if: needs.build.outputs.run_beta == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ steps.resolve-beta.outputs.tag }}
- NOTES_B64: ${{ steps.compute-beta.outputs.notes_b64 }}
+ NOTES_B64: ${{ needs.build.outputs.notes_b64 }}
run: |
set -euo pipefail
# Idempotency: a workflow re-fire (manual recovery from a flaky
@@ -508,7 +668,10 @@ jobs:
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag "$TAG"
- git push origin "$TAG"
+ # shellcheck disable=SC2016
+ git -c credential.helper= \
+ -c 'credential.helper=!f() { test "$1" = get || return 0; echo username=x-access-token; echo "password=$GH_TOKEN"; }; f' \
+ push origin "$TAG"
fi
BODY_FILE=$(mktemp)
@@ -538,16 +701,8 @@ jobs:
--notes-file "$BODY_FILE"
fi
- # The `gh release create` above runs as GITHUB_TOKEN, and GitHub
- # deliberately suppresses cascading workflow runs from
- # GITHUB_TOKEN-authored events to prevent recursion — so
- # `release: published` does NOT fire desktop-release.yml on its
- # own. The exemption to that rule is `repository_dispatch` (and
- # `workflow_dispatch`), which DO cascade even when fired via
- # GITHUB_TOKEN. Same-repo dispatch needs no PAT or App token —
- # the default `contents: write` token is enough.
- name: Trigger desktop-release.yml to build + upload the desktop installers
- if: steps.compute-beta.outputs.run_beta == 'true'
+ if: needs.build.outputs.run_beta == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ steps.resolve-beta.outputs.tag }}
@@ -560,68 +715,43 @@ jobs:
gh api -X POST "repos/${GITHUB_REPOSITORY}/dispatches" --input - <<<"$payload"
echo "Dispatched desktop-release for ${TAG}; desktop-release.yml will build every platform installer and upload them + the beta channel manifests to the prerelease."
- # --- STABLE DISPATCH PATH ---
+ publish:
+ name: Publish to npm (${{ github.event.action || github.event_name }})
+ needs: [build, release]
+ if: github.event.action == 'publish-stable' || needs.build.outputs.run_beta == 'true'
+ runs-on: ubuntu-latest
+ timeout-minutes: 10
+ permissions:
+ id-token: write
+ steps:
+ - name: Setup Node for npm OIDC publish
+ uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
+ with:
+ node-version: "24"
+ registry-url: "https://registry.npmjs.org"
- - name: Validate stable_version + override package.json (publish-stable)
- if: github.event.action == 'publish-stable'
- env:
- STABLE_VERSION: ${{ github.event.client_payload.version }}
+ # UPSTREAM(npm/cli#9722): npm 12.0.0 shipped without the sigstore module libnpmpublish loads for provenance.
+ - name: Install npm 11.21.0 for OIDC + provenance
run: |
- set -euo pipefail
-
- if [[ -z "${STABLE_VERSION:-}" ]]; then
- echo "::error::publish-stable dispatch missing client_payload.version"
- exit 1
- fi
- if [[ ! "$STABLE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
- echo "::error::publish-stable client_payload.version '${STABLE_VERSION}' is not bare semver (X.Y.Z)"
- exit 1
- fi
-
- # Same transient-jq primitive as the beta path: edit
- # version fields in place, never commit. The public repo's commit at
- # the source beta SHA carries whatever package.json version
- # agents-private mirrored (under the zero-commit-bump cadence,
- # that's the placeholder version on main); we override to the
- # clean X.Y.Z just for this publish.
- for pkg in cli core server app desktop; do
- f="packages/${pkg}/package.json"
- jq --arg v "$STABLE_VERSION" '.version=$v' "$f" > "${f}.tmp"
- mv "${f}.tmp" "$f"
- done
- # Match the beta path's skill-bundle sync so the published stable
- # carries SKILL.md bundles whose metadata.version reads X.Y.Z, not
- # whatever -beta.N was last stamped (or, in the steady state,
- # the placeholder).
- for skill in packages/server/assets/skills/project/SKILL.md \
- packages/server/assets/skills/discovery/SKILL.md; do
- sed -i'.bak' -E "s/^([[:space:]]+version:[[:space:]]+\")[^\"]+(\".*)$/\1${STABLE_VERSION}\2/" "$skill"
- rm -f "${skill}.bak"
- done
- echo "All fixed-group package.json files + both skill bundles' metadata.version now at ${STABLE_VERSION} (in-memory only)."
+ npm install -g [email protected]
+ echo "npm $(npm --version)"
- # --- SHARED PUBLISH STEP ---
+ - name: Download the tarball the build job packed
+ uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
+ with:
+ name: npm-package
+ path: ${{ runner.temp }}/npm-package
- name: Publish to npm via Trusted Publishing
- if: github.event.action == 'publish-stable' || steps.compute-beta.outputs.run_beta == 'true'
env:
- NPM_CONFIG_PROVENANCE: "true"
ACTION: ${{ github.event.action }}
- BETA_VERSION: ${{ steps.resolve-beta.outputs.version }}
+ BETA_VERSION: ${{ needs.release.outputs.version }}
STABLE_VERSION: ${{ github.event.client_payload.version }}
+ PACKAGE_NAME: "@inkeep/open-knowledge"
+ PACKAGE_DIR: ${{ runner.temp }}/npm-package
run: |
set -euo pipefail
- # Auth via npm Trusted Publishing (OIDC). The id-token: write
- # permission lets npm CLI 11.5.1+ exchange the GitHub Actions
- # OIDC token for a short-lived npm token automatically — no
- # NPM_TOKEN secret, no ~/.npmrc auth write. The trusted-
- # publisher entry on npmjs.com matches the OIDC token's repo
- # (inkeep/open-knowledge) + workflow filename
- # (release.yml) claims. Provenance attestation also uses OIDC
- # (NPM_CONFIG_PROVENANCE). See
- # https://docs.npmjs.com/trusted-publishers.
-
if [[ "$ACTION" == "publish-stable" ]]; then
TAG=latest
VERSION="$STABLE_VERSION"
@@ -630,32 +760,39 @@ jobs:
VERSION="$BETA_VERSION"
fi
- # Remove pre.json from the working tree before publish so
- # `changeset publish` always runs as a "normal publish" — same
- # design intent that held when Copybara excluded `.changeset/`
- # entirely (pre.json now mirrors so that `compute-next-beta.mjs`
- # can read `initialVersions` for the cycle anchor, but pre-mode
- # publish semantics aren't well-documented and we don't want
- # to depend on them). The runner discards working-tree state
- # after the job, and the `Restore working tree` step at the
- # end runs `git restore .` defensively.
- rm -f .changeset/pre.json
-
- # Changesets reads .changeset/config.json from the repo root
- # and publishes every package in the fixed group. On the beta
- # path the jq override has set every fixed-group
- # package.json#version to X.Y.Z-beta.N (a pre-release semver);
- # `npm publish --tag beta` accepts that as-is. On the stable
- # path the override is X.Y.Z. Only @inkeep/open-knowledge
- # actually reaches the registry; the four private:true packages
- # are skipped by `changeset publish`.
- echo "Publishing with --tag ${TAG} (VERSION=${VERSION})"
- pnpm exec changeset publish --tag "$TAG"
+ shopt -s nullglob
+ tarballs=("$PACKAGE_DIR"/*.tgz)
+ if (( ${#tarballs[@]} != 1 )); then
+ echo "::error::The build job's npm-package artifact holds ${#tarballs[@]} tarballs, not one. Refusing to publish."
+ exit 1
+ fi
+ TARBALL="${tarballs[0]}"
+ pacote="$(npm root -g)/npm/node_modules/pacote"
+ if ! manifest=$(node -e '
+ require(process.argv[1])
+ .manifest("file:" + process.argv[2], { fullMetadata: true, fullReadJson: true })
+ .then(
+ (manifest) => process.stdout.write(JSON.stringify(manifest)),
+ (error) => {
+ process.stderr.write(JSON.stringify(String(error && error.message)) + "\n");
+ process.exit(1);
+ },
+ )
+ ' "$pacote" "$TARBALL"); then
+ echo "::error::npm cannot read the packed tarball's manifest; its reason is printed above, JSON-encoded. Refusing to publish."
+ exit 1
+ fi
+ if ! jq -e --arg name "$PACKAGE_NAME" --arg version "$VERSION" \
+ '.name == $name and .version == $version and ((.publishConfig // {}) | keys - ["access"] | length == 0)' \
+ <<<"$manifest" >/dev/null; then
+ echo "::error::The packed tarball's manifest, as npm reads it, is $(jq -c '{name, version, publishConfig}' <<<"$manifest"). This job publishes only ${PACKAGE_NAME}@${VERSION}, whose publishConfig may set access and nothing else. Refusing to publish."
+ exit 1
+ fi
- - name: Restore working tree (defensive)
- # Both paths mutate package.json + SKILL.md transiently via jq/sed.
- # The beta path additionally mutates pre.json + CHANGELOG.md and
- # deletes consumed .changeset/*.md files via compute-next-beta.mjs.
- # `git restore .` reverses all of these, including deletions.
- if: always()
- run: git restore . || true
+ if [[ "$(npm view "${PACKAGE_NAME}@${VERSION}" version 2>/dev/null || true)" == "$VERSION" ]]; then
+ echo "::notice::${PACKAGE_NAME}@${VERSION} is already on npm; nothing to publish."
+ exit 0
+ fi
+
+ echo "Publishing with --tag ${TAG} (VERSION=${VERSION})"
+ npm publish "$TARBALL" --access public --tag "$TAG" --provenance --registry https://registry.npmjs.org/
diff --git a/scripts/compute-next-beta.mjs b/scripts/compute-next-beta.mjs
index 898cf5b50..683ece4c2 100644
--- a/scripts/compute-next-beta.mjs
+++ b/scripts/compute-next-beta.mjs
@@ -58,23 +58,47 @@ export function maxReleaseType(releases) {
return maxType;
}
-function findPrevBetaTag() {
- const res = spawnSync(
- 'gh',
- [
- 'release',
- 'list',
- '--repo',
- PUBLIC_REPO,
- '--limit',
- '50',
- '--json',
- 'tagName,isPrerelease',
- '--jq',
- '[.[] | select(.isPrerelease) | select(.tagName | test("^v[0-9]+\\\\.[0-9]+\\\\.[0-9]+-beta\\\\.[0-9]+$")) | .tagName] | first // ""',
- ],
- { encoding: 'utf8' },
- );
+export const RELEASE_LIST_ARGS = [
+ 'release',
+ 'list',
+ '--repo',
+ PUBLIC_REPO,
+ '--limit',
+ '50',
+ '--json',
+ 'tagName,isPrerelease',
+ '--jq',
+ '[.[] | select(.isPrerelease) | select(.tagName | test("^v[0-9]+\\\\.[0-9]+\\\\.[0-9]+-beta\\\\.[0-9]+$")) | .tagName] | first // ""',
+];
+
+export function releaseViewArgs(tag) {
+ return ['release', 'view', tag, '--repo', PUBLIC_REPO, '--json', 'body', '--jq', '.body'];
+}
+
+export function recordedReleases(env) {
+ const sameArgs = (args, expected) => args.length === expected.length && args.every((arg, i) => arg === expected[i]);
+ const status = (name) => {
+ const value = env[name];
+ if (typeof value !== 'string' || !/^\d+$/.test(value)) {
+ throw new Error(
+ `${name} is ${value === undefined ? 'missing' : JSON.stringify(value)}, not an exit status: the read-releases job's outputs did not reach this step`,
+ );
+ }
+ return Number(value);
+ };
+ return (args) => {
+ if (sameArgs(args, RELEASE_LIST_ARGS)) {
+ return { status: status('RELEASE_LIST_STATUS'), stdout: env.RELEASE_LIST_STDOUT, stderr: env.RELEASE_LIST_STDERR };
+ }
+ if (env.RELEASE_VIEW_TAG && sameArgs(args, releaseViewArgs(env.RELEASE_VIEW_TAG))) {
+ return { status: status('RELEASE_VIEW_STATUS'), stdout: env.RELEASE_VIEW_STDOUT, stderr: '' };
+ }
+ throw new Error(`the read-releases job recorded no result for gh ${args.join(' ')}`);
+ };
+}
+
+function findPrevBetaTag(gh) {
+ const res = gh(RELEASE_LIST_ARGS);
if (res.status !== 0) {
log(`[warn] gh release list failed (exit ${res.status}); treating as bootstrap.`);
log(res.stderr);
@@ -84,12 +108,8 @@ function findPrevBetaTag() {
return tag || null;
}
-function recoverConsumedSet(tag) {
- const res = spawnSync(
- 'gh',
- ['release', 'view', tag, '--repo', PUBLIC_REPO, '--json', 'body', '--jq', '.body'],
- { encoding: 'utf8' },
- );
+function recoverConsumedSet(tag, gh) {
+ const res = gh(releaseViewArgs(tag));
if (res.status !== 0) {
log(`[warn] gh release view ${tag} failed (exit ${res.status}); treating as bootstrap.`);
return null;
@@ -112,6 +132,11 @@ function recoverConsumedSet(tag) {
}
}
+export function previousBeta(gh) {
+ const prevBetaTag = findPrevBetaTag(gh);
+ return { prevBetaTag, recovered: prevBetaTag ? recoverConsumedSet(prevBetaTag, gh) : null };
+}
+
function readChangelogs() {
const out = {};
for (const pkg of CHANGELOG_PKGS) {
@@ -242,12 +267,9 @@ async function main() {
const allIds = changesets.map(({ id }) => id);
const allIdsSet = new Set(allIds);
- const prevBetaTag = findPrevBetaTag();
+ const { prevBetaTag, recovered } = previousBeta(recordedReleases(process.env));
let priorConsumed = [];
- if (prevBetaTag) {
- const recovered = recoverConsumedSet(prevBetaTag);
- if (recovered) priorConsumed = recovered.filter((id) => allIdsSet.has(id));
- }
+ if (recovered) priorConsumed = recovered.filter((id) => allIdsSet.has(id));
const priorSet = new Set(priorConsumed);
const newIds = allIds.filter((id) => !priorSet.has(id));
diff --git a/scripts/compute-next-beta.test.mjs b/scripts/compute-next-beta.test.mjs
index 9da68215f..3d7f3d541 100644
--- a/scripts/compute-next-beta.test.mjs
+++ b/scripts/compute-next-beta.test.mjs
@@ -1,4 +1,9 @@
-import { describe, expect, test } from 'vitest';
+import { execFileSync } from 'node:child_process';
+import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { afterAll, describe, expect, test } from 'vitest';
import {
bumpSemver,
computeBaseVersion,
@@ -6,6 +11,10 @@ import {
maxBumpType,
maxReleaseType,
parseSection,
+ previousBeta,
+ RELEASE_LIST_ARGS,
+ recordedReleases,
+ releaseViewArgs,
renderNotes,
} from './compute-next-beta.mjs';
@@ -451,3 +460,130 @@ describe('maxReleaseType', () => {
).toBe('patch');
});
});
+
+describe('previousBeta reads the previous beta through the gh calls it is handed', () => {
+ const TAG = 'v0.82.0-beta.8';
+ const answering = (list, view) => (args) => {
+ if (args[1] === 'list') return list;
+ if (args[1] === 'view' && args[2] === TAG) return view;
+ throw new Error(`unexpected gh ${args.join(' ')}`);
+ };
+ const ok = (stdout) => ({ status: 0, stdout, stderr: '' });
+ const body = (marker) => `Notes.\n${marker}\n`;
+
+ test.each([
+ ['a failed list bootstraps', { status: 1, stdout: '', stderr: 'HTTP 502' }, null, { prevBetaTag: null, recovered: null }],
+ ['an empty list bootstraps', ok('\n'), null, { prevBetaTag: null, recovered: null }],
+ ['a failed view keeps the tag and bootstraps the set', ok(`${TAG}\n`), { status: 1, stdout: '', stderr: '' }, { prevBetaTag: TAG, recovered: null }],
+ ['a body without a marker bootstraps the set', ok(`${TAG}\n`), ok(body('no marker here')), { prevBetaTag: TAG, recovered: null }],
+ ['a marker that is not JSON bootstraps the set', ok(`${TAG}\n`), ok(body('<!-- ok-consumed-set: [a, b] -->')), { prevBetaTag: TAG, recovered: null }],
+ ['a marker that is not a string array bootstraps the set', ok(`${TAG}\n`), ok(body('<!-- ok-consumed-set: [1, 2] -->')), { prevBetaTag: TAG, recovered: null }],
+ ['a well-formed marker is the consumed set', ok(`${TAG}\n`), ok(body('<!-- ok-consumed-set: ["a","b"] -->')), { prevBetaTag: TAG, recovered: ['a', 'b'] }],
+ ['a quoted tag is unquoted', ok(`"${TAG}"\n`), ok(body('<!-- ok-consumed-set: ["a"] -->')), { prevBetaTag: TAG, recovered: ['a'] }],
+ ])('%s', (_, list, view, expected) => {
+ expect(previousBeta(answering(list, view))).toEqual(expected);
+ });
+
+ test('the list and view calls are the queries the read-releases job runs', () => {
+ const calls = [];
+ previousBeta((args) => {
+ calls.push(args);
+ return args[1] === 'list' ? ok(`${TAG}\n`) : ok(body('<!-- ok-consumed-set: [] -->'));
+ });
+ expect(calls).toEqual([RELEASE_LIST_ARGS, releaseViewArgs(TAG)]);
+ expect(RELEASE_LIST_ARGS.slice(0, 3)).toEqual(['release', 'list', '--repo']);
+ expect(RELEASE_LIST_ARGS.at(-1)).toBe(
+ '[.[] | select(.isPrerelease) | select(.tagName | test("^v[0-9]+\\\\.[0-9]+\\\\.[0-9]+-beta\\\\.[0-9]+$")) | .tagName] | first // ""',
+ );
+ });
+});
+
+describe('recordedReleases replays what the read-releases job recorded', () => {
+ const TAG = 'v0.82.0-beta.8';
+ const recorded = {
+ RELEASE_LIST_STATUS: '0',
+ RELEASE_LIST_STDOUT: TAG,
+ RELEASE_LIST_STDERR: '',
+ RELEASE_VIEW_TAG: TAG,
+ RELEASE_VIEW_STATUS: '0',
+ RELEASE_VIEW_STDOUT: 'Notes.\n<!-- ok-consumed-set: ["a"] -->',
+ };
+
+ test('answers the recorded list and view calls', () => {
+ const gh = recordedReleases(recorded);
+ expect(gh(RELEASE_LIST_ARGS)).toEqual({ status: 0, stdout: TAG, stderr: '' });
+ expect(gh(releaseViewArgs(TAG))).toEqual({ status: 0, stdout: recorded.RELEASE_VIEW_STDOUT, stderr: '' });
+ expect(previousBeta(gh)).toEqual({ prevBetaTag: TAG, recovered: ['a'] });
+ });
+
+ test('replays a failed list with its status and stderr', () => {
+ const gh = recordedReleases({ RELEASE_LIST_STATUS: '1', RELEASE_LIST_STDOUT: '', RELEASE_LIST_STDERR: 'HTTP 502' });
+ expect(gh(RELEASE_LIST_ARGS)).toEqual({ status: 1, stdout: '', stderr: 'HTTP 502' });
+ expect(previousBeta(gh)).toEqual({ prevBetaTag: null, recovered: null });
+ });
+
+ test.each([
+ ['an empty list status', { ...recorded, RELEASE_LIST_STATUS: '' }, RELEASE_LIST_ARGS, 'RELEASE_LIST_STATUS is ""'],
+ ['a missing list status', (({ RELEASE_LIST_STATUS: _, ...rest }) => rest)(recorded), RELEASE_LIST_ARGS, 'RELEASE_LIST_STATUS is missing'],
+ ['a list status that is not a number', { ...recorded, RELEASE_LIST_STATUS: '0\n' }, RELEASE_LIST_ARGS, 'RELEASE_LIST_STATUS is "0\\n"'],
+ ['an empty view status', { ...recorded, RELEASE_VIEW_STATUS: '' }, releaseViewArgs(TAG), 'RELEASE_VIEW_STATUS is ""'],
+ ['a missing view status', (({ RELEASE_VIEW_STATUS: _, ...rest }) => rest)(recorded), releaseViewArgs(TAG), 'RELEASE_VIEW_STATUS is missing'],
+ ])('fails loudly on %s, naming the read-releases handoff', (_, env, args, message) => {
+ const gh = recordedReleases(env);
+ expect(() => gh(args)).toThrow(message);
+ expect(() => gh(args)).toThrow("the read-releases job's outputs did not reach this step");
+ expect(() => previousBeta(gh)).toThrow("the read-releases job's outputs did not reach this step");
+ });
+
+ test.each([
+ ['0', { prevBetaTag: TAG, recovered: ['a'] }],
+ ['1', { prevBetaTag: null, recovered: null }],
+ ])('a recorded list status of %s behaves as the exit status it is', (listStatus, expected) => {
+ expect(previousBeta(recordedReleases({ ...recorded, RELEASE_LIST_STATUS: listStatus }))).toEqual(expected);
+ });
+
+ test('a recorded view status of 1 keeps the tag and bootstraps the set', () => {
+ expect(previousBeta(recordedReleases({ ...recorded, RELEASE_VIEW_STATUS: '1' }))).toEqual({ prevBetaTag: TAG, recovered: null });
+ });
+
+ test('refuses any call the job did not record', () => {
+ const gh = recordedReleases(recorded);
+ expect(() => gh(releaseViewArgs('v0.82.0-beta.7'))).toThrow('the read-releases job recorded no result for gh release view v0.82.0-beta.7');
+ expect(() => gh(['release', 'list', '--repo', 'other/repo'])).toThrow('recorded no result');
+ expect(() => recordedReleases({ ...recorded, RELEASE_VIEW_TAG: '' })(releaseViewArgs(''))).toThrow('recorded no result');
+ });
+});
+
+describe('the compute-next-beta CLI takes the previous beta from the read-releases job, never from gh', () => {
+ const root = mkdtempSync(join(tmpdir(), 'compute-next-beta-cli-'));
+ afterAll(() => rmSync(root, { recursive: true, force: true }));
+ const script = fileURLToPath(new URL('./compute-next-beta.mjs', import.meta.url));
+
+ test('a recorded previous beta that consumed every pending changeset skips the cut without running gh', () => {
+ mkdirSync(join(root, '.changeset'));
+ writeFileSync(
+ join(root, '.changeset', 'pre.json'),
+ JSON.stringify({ mode: 'pre', tag: 'beta', initialVersions: { '@inkeep/open-knowledge': '0.81.4' }, changesets: [] }),
+ );
+ writeFileSync(join(root, '.changeset', 'fixture-change.md'), "---\n'@inkeep/open-knowledge': patch\n---\n\nFixture change.\n");
+ const bin = join(root, 'bin');
+ mkdirSync(bin);
+ const tripwire = join(root, 'gh-calls.log');
+ writeFileSync(join(bin, 'gh'), `#!/bin/sh\necho "$*" >> '${tripwire}'\nexit 1\n`, { mode: 0o755 });
+ const out = execFileSync(process.execPath, [script], {
+ cwd: root,
+ encoding: 'utf8',
+ env: {
+ PATH: `${bin}:${process.env.PATH}`,
+ RELEASE_LIST_STATUS: '0',
+ RELEASE_LIST_STDOUT: 'v0.81.5-beta.0',
+ RELEASE_LIST_STDERR: '',
+ RELEASE_VIEW_TAG: 'v0.81.5-beta.0',
+ RELEASE_VIEW_STATUS: '0',
+ RELEASE_VIEW_STDOUT: 'Draft notes.\n<!-- ok-consumed-set: ["fixture-change"] -->',
+ },
+ });
+ expect(JSON.parse(out)).toEqual({ skip: true, reason: 'no new changesets since v0.81.5-beta.0' });
+ expect(existsSync(tripwire)).toBe(false);
+ });
+});