diff --git a/.gitea/workflows/build-and-package.yml b/.gitea/workflows/build-and-package.yml index 72d6d83..8dc4a5f 100644 --- a/.gitea/workflows/build-and-package.yml +++ b/.gitea/workflows/build-and-package.yml @@ -1,6 +1,9 @@ name: Build and package on: + push: + tags: + - 'v*' workflow_dispatch: inputs: runtime_run_id: @@ -8,12 +11,13 @@ on: required: false permissions: - contents: read + contents: write actions: read packages: write jobs: build-package: + if: ${{ gitea.event_name != 'push' || gitea.event.deleted != true }} runs-on: ubuntu-latest-full timeout-minutes: 120 defaults: @@ -23,6 +27,8 @@ jobs: ELECTRON_SKIP_BINARY_DOWNLOAD: '1' RUNTIME_RUN_ID: ${{ inputs.runtime_run_id || vars.MUSRFLOW_RUNTIME_RUN_ID }} PACKAGE_RUN_ID: ${{ gitea.run_id }} + GITEA_EVENT_NAME: ${{ gitea.event_name }} + GITEA_REF: ${{ gitea.ref }} steps: - name: Check out muSRFlow uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 @@ -30,6 +36,14 @@ jobs: github-server-url: ${{ gitea.server_url }} persist-credentials: false + - name: Set up Node.js + uses: https://github.com/actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: '22.23.2' + + - name: Validate release identity + run: node scripts/ci-release.js prepare + - name: Check runtime selection run: | [[ "$RUNTIME_RUN_ID" =~ ^[1-9][0-9]*$ ]] || { echo 'Set the MUSRFLOW_RUNTIME_RUN_ID repository variable or enter a numeric runtime_run_id' >&2; exit 1; } @@ -45,11 +59,6 @@ jobs: repository: ${{ gitea.repository }} github-token: ${{ secrets.GITEA_TOKEN }} - - name: Set up Node.js - uses: https://github.com/actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 - with: - node-version: '22.23.2' - - name: Install dependencies run: npm ci --no-audit --no-fund @@ -89,6 +98,8 @@ jobs: cp -a dist/runtime-downloads artifacts/ cp "$RUNTIME_DIR/runtime-manifest.json" artifacts/ printf 'runtime_workflow_run=%s\nmuSRFlow=%s\n' "$RUNTIME_RUN_ID" "$(git rev-parse HEAD)" > artifacts/build-source.txt + cp .ci-release-context.json artifacts/release-context.json + - name: Upload packages uses: https://github.com/actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 with: @@ -106,3 +117,10 @@ jobs: GITEA_REPOSITORY: ${{ gitea.repository }} PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }} run: node scripts/ci-publish-packages.js artifacts + + - name: Create draft Release for version tag + env: + GITEA_SERVER_URL: ${{ gitea.server_url }} + GITEA_REPOSITORY: ${{ gitea.repository }} + RELEASES_TOKEN: ${{ secrets.RELEASES_TOKEN || secrets.GITEA_TOKEN }} + run: node scripts/ci-release.js draft artifacts diff --git a/.gitea/workflows/publish-packages.yml b/.gitea/workflows/publish-packages.yml index 473b941..26e848d 100644 --- a/.gitea/workflows/publish-packages.yml +++ b/.gitea/workflows/publish-packages.yml @@ -8,7 +8,7 @@ on: required: true permissions: - contents: read + contents: write actions: read packages: write @@ -54,3 +54,10 @@ jobs: # Optional fallback for servers that restrict registry access by job tokens. PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }} run: node scripts/ci-publish-packages.js artifacts + + # The original artifact's context selects manual vs tagged publication, + # never the branch/tag on which this recovery workflow was dispatched. + - name: Create or preserve draft Release + env: + RELEASES_TOKEN: ${{ secrets.RELEASES_TOKEN || secrets.GITEA_TOKEN }} + run: node scripts/ci-release.js draft artifacts diff --git a/docs/DESKTOP_DEPLOYMENT.md b/docs/DESKTOP_DEPLOYMENT.md index 4eff628..b7c0be2 100644 --- a/docs/DESKTOP_DEPLOYMENT.md +++ b/docs/DESKTOP_DEPLOYMENT.md @@ -125,22 +125,21 @@ checksums, runtime download bundle, runtime manifest, and build-source record. Application packaging populates electron-builder's tool cache before the runtime packagers use it. Missing output fails the job; collection errors are not suppressed. `--publish never` prevents implicit electron-builder release -publishing; the final explicit publisher handles the package registry. Neither -step creates a Release page or deploys a site. Publication only starts after +publishing; the final explicit publisher handles the package registry. Manual +builds do not create a Release page or deploy a site. Publication only starts after the complete artifact is saved, so a registry failure can be retried without recompiling (see below). -This is the first automation step: the combined workflow is still started -manually. Tag triggers, release-version naming, draft Releases, and durable -runtime reuse are not enabled yet. Runtime artifacts still expire according -to their retention policy; saving an ID in a variable does not extend it. +The same workflow also runs for version-tag pushes, as described below. +Durable runtime reuse is not enabled yet: runtime artifacts still expire +according to their retention policy; saving an ID in a variable does not extend it. The old `build_runtime=false` option is removed: skipping a build does not supply a runtime on a fresh runner. The two workflow files are [build-runtime.yml](../.gitea/workflows/build-runtime.yml) and [build-and-package.yml](../.gitea/workflows/build-and-package.yml). -The latter currently builds **Linux packages only**. Windows/WSL packaging and -release-page publication are separate next steps. The standalone publisher +The latter currently builds **Linux packages only**. Windows/WSL packaging is +a separate next step. The standalone publisher remains available for recovery as described below. Both workflows use pinned artifact actions. PSI's runner must support their @@ -150,11 +149,66 @@ incorrect run ID, expired artifact, or Actions-token permissions. Artifact download does not require a personal token; PSI registry publication uses the **PACKAGES_TOKEN** secret described below. +### Release automatically from a version tag + +**Prerequisites:** set **MUSRFLOW_RUNTIME_RUN_ID** to an approved, unexpired +runtime artifact and keep **PACKAGES_TOKEN** configured. The tagged commit must +contain this workflow. Restrict who can push `v*` tags: those pushes publish +packages using repository secrets. + +1. Update `package.json` and `package-lock.json` together, for example with + `npm version 1.0.0-rc.6 --no-git-tag-version`. Update release notes/version + references as needed, test, and commit the version changes. +2. Create an annotated tag matching the package version exactly and push it to + Gitea. For example, after committing version `1.0.0-rc.6`: + + ```bash + git tag -a v1.0.0-rc.6 -m "muSRFlow 1.0.0-rc.6" + git push gitea master + git push gitea v1.0.0-rc.6 + ``` + +3. **Build and package** starts automatically. It rejects mismatched tags + before downloading the runtime, then validates, tests, builds, archives, + and publishes the Linux application and runtime packages. +4. Open **Packages**: tag `v1.0.0-rc.6` publishes registry version + **1.0.0-rc.6**, without a build-ID suffix. Existing different files are never + overwritten; do not move a released tag to rebuild different contents. +5. Open **Releases** and review the newly created **draft**. It contains direct + registry download links for all four RPM/AppImage files, SHA-256 values and + checksum links, source commit, packaging run ID, and runtime manifest link. + Smoke-test the downloads, edit the release notes, then publish the draft. + +Release-candidate/beta versions are marked as prereleases; stable versions are +not. The Release remains a draft in either case. Package files are already in +the registry, whose visibility follows Gitea's package access policy; a draft +Release does not make those files private. Release downloads are links to the +registry, not duplicated attachments. Publishing the draft does not trigger a +second build: only tag pushes and explicit manual dispatches trigger this workflow. +Tag deletion is ignored. Manual dispatches, even when selecting a tag, still +use build-specific registry versions and do not create a Release page. + +Draft creation requests `contents: write` using the built-in job token. If PSI +rejects that API call with HTTP 401/403, configure a separate **RELEASES_TOKEN** +Actions secret scoped to **write:repository**, owned by a user with release +write access. The package-only **PACKAGES_TOKEN** is not reused for release +API calls. Retry **Publish packages** with the original packaging run ID; no +rebuild is needed. See [Gitea's release API](https://docs.gitea.com/api/next/operations/repo-create-release/). + +The artifact includes `release-context.json` with the original tag, version, +source commit, and packaging run ID. Recovery uses this file, not the newer +checkout's `package.json` or the recovery workflow's ref. The draft step checks +that the remote tag still points to that commit and never creates a missing +tag. It preserves existing release notes and draft/published status when the +release contains the matching build-provenance marker; an unrelated existing +Release fails rather than being replaced. Keep the generated HTML provenance +comment when editing release notes if you want future retries to recognize it. + ### Retry publication without rebuilding **Build and package** now publishes automatically. Use the standalone -**Publish packages** workflow only for older builds or to retry a registry -failure. A combined run may be marked failed even though its complete package +**Publish packages** workflow only for older builds or to retry a registry or +draft-creation failure. A combined run may be marked failed even though its complete package artifact was uploaded successfully. The recovery workflow verifies that it is a finished packaging run with the expected, unexpired artifact; a runtime run, missing artifact, or unfinished run is rejected with a clear error. @@ -180,8 +234,12 @@ run, missing artifact, or unfinished run is rejected with a clear error. **musrflow** (application RPM/AppImage) and **musrflow-runtime** (runtime RPM/AppImage). Each includes checksums and build-source information; the runtime also includes its manifest. +5. For tagged artifacts, the final step creates the missing draft Release or + preserves an already recognized Release. For manual/older artifacts it does + nothing. Retry from the updated default branch to use publisher fixes while + retaining the original release version. -Registry versions use `-build.`, for example +Manual registry versions use `-build.`, for example `1.0.0-rc.5-build.12345`. The application version and downloaded filenames stay unchanged. The version is read from the selected artifact, not from the newer publisher checkout. Separate builds cannot silently replace each other. @@ -207,7 +265,8 @@ fallback, but do not remove the working PSI secret. This uses Gitea's [Generic Package Registry](https://docs.gitea.com/usage/packages/generic/), not an RPM installation repository, and explicitly [links packages to the repository](https://docs.gitea.com/usage/packages/overview/). -It creates neither a Git tag nor a Release page. Registry files are independent +It never creates a Git tag; the final step creates a draft Release only for a +validated tagged artifact. Registry files are independent of the 30-day Actions artifact retention, but remain subject to server package cleanup policies. The offline runtime `.tar.gz`/`runtime-download.json` bundle remains in the original Actions artifact; this first publisher exposes the diff --git a/package.json b/package.json index 6dcc517..e8832db 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "checksums:md5": "sh -c 'd=dist; [ -d \"$d\" ] || exit 0; for f in \"$d\"/*; do [ -f \"$f\" ] || continue; case \"$f\" in *.rpm|*.AppImage) base=$(basename \"$f\"); (cd \"$d\" && md5sum \"$base\" > \"$base.md5\");; esac; done'", "checksums:sha256": "sh -c 'd=dist; [ -d \"$d\" ] || exit 0; for f in \"$d\"/*; do [ -f \"$f\" ] || continue; case \"$f\" in *.rpm|*.AppImage) base=$(basename \"$f\"); (cd \"$d\" && sha256sum \"$base\" > \"$base.sha256\");; esac; done'", "checksums": "npm run checksums:md5 && npm run checksums:sha256", - "test:electron": "node tests/log-highlighting.test.js && node tests/action-state.test.js && node tests/electron-missing-data.test.js && node tests/electron-run-data.test.js && node tests/electron-msr-title.test.js && node tests/electron-musrfit-threads.test.js && node tests/electron-t0-prompt-peak.test.js && node tests/window-layering.test.js && node tests/msr-generation.test.js && node tests/root-plot-clean.test.js && node tests/parameter-plot.test.js && node tests/help-window.test.js && node tests/release-packaging.test.js && node tests/ci-publish-packages.test.js && node tests/runtime-validation.test.js && node tests/local-runtime.test.js && node tests/addrun.test.js && node tests/fit-statistic.test.js && node tests/series-fit-planner.test.js && node tests/series-fit-result.test.js && node tests/series-fit-inventory.test.js && node tests/series-fit-msr-preparation.test.js && node tests/series-artifact-contract.test.js && node tests/series-global-merge.test.js && node tests/series-fit-staging.test.js && node tests/series-fit-runner.test.js && node tests/series-runner-adapters.test.js && node tests/series-fit-routing.test.js && node tests/series-action-chips.test.js && node tests/singlehist-parity.test.js && node tests/url-parameters.test.js && node tests/run-naming.test.js && node tests/electron-portable-naming.test.js && node tests/shared-configuration.test.js && node tests/session-persistence.test.js && node tests/reference-fixtures.test.js && node tests/electron-bridge.test.js && node tests/electron-bundled-runtime.test.js && node tests/wsl-runtime.test.js" + "test:electron": "node tests/log-highlighting.test.js && node tests/action-state.test.js && node tests/electron-missing-data.test.js && node tests/electron-run-data.test.js && node tests/electron-msr-title.test.js && node tests/electron-musrfit-threads.test.js && node tests/electron-t0-prompt-peak.test.js && node tests/window-layering.test.js && node tests/msr-generation.test.js && node tests/root-plot-clean.test.js && node tests/parameter-plot.test.js && node tests/help-window.test.js && node tests/release-packaging.test.js && node tests/ci-publish-packages.test.js && node tests/ci-release.test.js && node tests/runtime-validation.test.js && node tests/local-runtime.test.js && node tests/addrun.test.js && node tests/fit-statistic.test.js && node tests/series-fit-planner.test.js && node tests/series-fit-result.test.js && node tests/series-fit-inventory.test.js && node tests/series-fit-msr-preparation.test.js && node tests/series-artifact-contract.test.js && node tests/series-global-merge.test.js && node tests/series-fit-staging.test.js && node tests/series-fit-runner.test.js && node tests/series-runner-adapters.test.js && node tests/series-fit-routing.test.js && node tests/series-action-chips.test.js && node tests/singlehist-parity.test.js && node tests/url-parameters.test.js && node tests/run-naming.test.js && node tests/electron-portable-naming.test.js && node tests/shared-configuration.test.js && node tests/session-persistence.test.js && node tests/reference-fixtures.test.js && node tests/electron-bridge.test.js && node tests/electron-bundled-runtime.test.js && node tests/wsl-runtime.test.js" }, "repository": { "type": "git", diff --git a/scripts/ci-publish-packages.js b/scripts/ci-publish-packages.js index 41b4280..a41e954 100644 --- a/scripts/ci-publish-packages.js +++ b/scripts/ci-publish-packages.js @@ -4,6 +4,7 @@ const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); +const { readReleaseContext } = require('./ci-release'); async function publishPackages(directory, env = process.env, request = fetch) { const { GITEA_SERVER_URL, GITEA_REPOSITORY, PACKAGES_TOKEN, PACKAGE_RUN_ID } = env; @@ -22,7 +23,9 @@ async function publishPackages(directory, env = process.env, request = fetch) { // Read the version from the selected artifact, not the publisher checkout. const version = application[0].slice('musrflow-'.length, -'-x86_64.rpm'.length); if (!/^[\w.+-]+$/.test(version)) throw new Error('Invalid package version'); - const registryVersion = `${version}-build.${PACKAGE_RUN_ID}`; + const release = readReleaseContext(directory, PACKAGE_RUN_ID); + if (release && release.version !== version) throw new Error('Artifact filenames do not match the recorded release version'); + const registryVersion = release?.tag ? version : `${version}-build.${PACKAGE_RUN_ID}`; const groups = { musrflow: [application[0], `muSRFlow-${version}-x86_64.AppImage`], 'musrflow-runtime': [`musrflow-runtime-${version}-x86_64.rpm`, `musrflow-runtime-${version}-x86_64.AppImage`] @@ -46,6 +49,7 @@ async function publishPackages(directory, env = process.env, request = fetch) { } } names.push('build-source.txt'); + if (release) names.push('release-context.json'); } groups['musrflow-runtime'].push('runtime-manifest.json'); for (const names of Object.values(groups)) { diff --git a/scripts/ci-release.js b/scripts/ci-release.js new file mode 100644 index 0000000..85d9318 --- /dev/null +++ b/scripts/ci-release.js @@ -0,0 +1,137 @@ +// Copyright (C) 2026 Zaher Salman +// SPDX-License-Identifier: GPL-3.0-or-later + +const fs = require('fs'); +const path = require('path'); +const { execFileSync } = require('child_process'); + +const versionPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?$/; + +function prepareRelease(root, env = process.env) { + const version = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')).version; + if (!versionPattern.test(version)) throw new Error('package.json must contain a valid release version'); + if (!/^[1-9][0-9]*$/.test(env.PACKAGE_RUN_ID || '')) throw new Error('A packaging run ID is required'); + let tag = null; + // Manual builds keep build-specific versions, even when dispatched on a tag. + if (env.GITEA_EVENT_NAME === 'push') { + if (env.GITEA_REF !== `refs/tags/v${version}`) { + throw new Error(`Release tag must match package.json: expected refs/tags/v${version}, got ${env.GITEA_REF}`); + } + tag = `v${version}`; + } else if (env.GITEA_EVENT_NAME !== 'workflow_dispatch') { + throw new Error('Only version-tag pushes and manual builds are supported'); + } + const commit = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim(); + if (tag) { + const taggedCommit = execFileSync('git', ['rev-parse', `refs/tags/${tag}^{commit}`], { cwd: root, encoding: 'utf8' }).trim(); + if (taggedCommit !== commit) throw new Error('The checked-out commit does not match the release tag'); + } + return { schema: 1, version, tag, commit, runId: env.PACKAGE_RUN_ID }; +} + +function readReleaseContext(directory, runId) { + const file = path.join(directory, 'release-context.json'); + // Artifacts from before tag automation remain publishable as manual builds. + if (!fs.existsSync(file)) return null; + const context = JSON.parse(fs.readFileSync(file, 'utf8')); + if (!context || context.schema !== 1 || typeof context.version !== 'string' || !versionPattern.test(context.version) + || !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(context.commit || '') || !/^[1-9][0-9]*$/.test(context.runId || '') + || context.runId !== runId || (context.tag !== null && context.tag !== `v${context.version}`)) { + throw new Error('Invalid release context or packaging run ID mismatch'); + } + const source = fs.readFileSync(path.join(directory, 'build-source.txt'), 'utf8'); + if (!source.split(/\r?\n/).includes(`muSRFlow=${context.commit}`)) throw new Error('Release context does not match the artifact source commit'); + return context; +} + +async function createDraftRelease(directory, env = process.env, request = fetch) { + const context = readReleaseContext(directory, env.PACKAGE_RUN_ID); + if (!context || !context.tag) { + console.log('Manual build: no Release page will be created'); + return null; + } + const { GITEA_SERVER_URL, GITEA_REPOSITORY, RELEASES_TOKEN } = env; + if (!RELEASES_TOKEN) throw new Error('A release write token is required'); + if (!/^[\w.-]+\/[\w.-]+$/.test(GITEA_REPOSITORY || '')) throw new Error('Expected owner/repository'); + const server = new URL(GITEA_SERVER_URL); + if (server.protocol !== 'https:' || server.username || server.password || server.search || server.hash) { + throw new Error('Expected an HTTPS Gitea server URL without credentials, query, or fragment'); + } + const base = server.href.replace(/\/$/, ''); + const api = `${base}/api/v1/repos/${GITEA_REPOSITORY}`; + const headers = { Authorization: `token ${RELEASES_TOKEN}` }; + // Never let the release API silently create a missing tag or use a moved one. + const tagResponse = await request(`${api}/tags/${encodeURIComponent(context.tag)}`, { + headers, redirect: 'error', signal: AbortSignal.timeout(60000) + }); + if (tagResponse.status !== 200) throw new Error(`Cannot verify release tag: HTTP ${tagResponse.status}`); + const tag = await tagResponse.json(); + if (tag.commit?.sha !== context.commit) throw new Error('Release tag moved since this artifact was built; refusing to create a Release'); + + const marker = ``; + const existing = await request(`${api}/releases/tags/${encodeURIComponent(context.tag)}`, { + headers, redirect: 'error', signal: AbortSignal.timeout(60000) + }); + if (existing.status === 200) { + const release = await existing.json(); + if (release.tag_name !== context.tag || !String(release.body || '').includes(marker)) { + throw new Error('A Release already exists for this tag without matching build provenance; it was not changed'); + } + console.log(`Release already exists; preserving its notes and publication status: ${release.html_url}`); + return release; + } + if (existing.body) await existing.body.cancel(); + if (existing.status !== 404) throw new Error(`Cannot inspect existing Release: HTTP ${existing.status}`); + + const owner = GITEA_REPOSITORY.split('/')[0]; + const downloads = [ + ['musrflow', `musrflow-${context.version}-x86_64.rpm`], + ['musrflow', `muSRFlow-${context.version}-x86_64.AppImage`], + ['musrflow-runtime', `musrflow-runtime-${context.version}-x86_64.rpm`], + ['musrflow-runtime', `musrflow-runtime-${context.version}-x86_64.AppImage`] + ]; + const lines = [marker, '', `# muSRFlow ${context.version}`, '', + 'Linux x86_64 packages. Install the application; install the separate runtime only if you need ROOT/musrfit.', '', + 'Review and smoke-test these downloads before publishing this draft.', '', + '| Download | SHA-256 |', '| --- | --- |']; + for (const [name, file] of downloads) { + const checksum = fs.readFileSync(path.join(directory, `${file}.sha256`), 'utf8').trim(); + if (!/^[a-f0-9]{64} /.test(checksum) || checksum.slice(66) !== file) throw new Error(`Invalid checksum sidecar for ${file}`); + const url = `${base}/api/packages/${owner}/generic/${name}/${encodeURIComponent(context.version)}/${encodeURIComponent(file)}`; + lines.push(`| [${file}](${url}) | \`${checksum.slice(0, 64)}\` ([checksum](${url}.sha256)) |`); + } + lines.push('', `Source commit: [${context.commit}](${base}/${GITEA_REPOSITORY}/commit/${context.commit})`, + '', `Packaging workflow run ID: ${context.runId}.`, '', + `[Runtime manifest](${base}/api/packages/${owner}/generic/musrflow-runtime/${encodeURIComponent(context.version)}/runtime-manifest.json)`, + '', 'Windows/macOS installers are not produced by this workflow.'); + const response = await request(`${api}/releases`, { + method: 'POST', headers: { ...headers, 'Content-Type': 'application/json' }, + redirect: 'error', signal: AbortSignal.timeout(60000), + body: JSON.stringify({ tag_name: context.tag, target_commitish: context.commit, + name: `muSRFlow ${context.version}`, body: lines.join('\n'), draft: true, + prerelease: context.version.split('+')[0].includes('-') }) + }); + if (response.status !== 201) throw new Error(`Create draft Release: HTTP ${response.status}. Check release write permission; retry Publish packages without rebuilding`); + const release = await response.json(); + console.log(`Draft Release created: ${release.html_url}`); + return release; +} + +module.exports = { prepareRelease, readReleaseContext, createDraftRelease }; + +if (require.main === module) { + (async () => { + if (process.argv[2] === 'prepare') { + const context = prepareRelease(process.cwd()); + fs.writeFileSync('.ci-release-context.json', JSON.stringify(context, null, 2) + '\n'); + console.log(context.tag ? `Validated release ${context.tag} at ${context.commit}` : 'Manual build: using build-specific package versions'); + } else if (process.argv[2] === 'draft') { + await createDraftRelease(process.argv[3] || 'artifacts'); + } else { + throw new Error('Usage: ci-release.js prepare | draft [artifacts-directory]'); + } + })().catch(error => { + console.error(error.message); + process.exitCode = 1; + }); +} diff --git a/tests/ci-publish-packages.test.js b/tests/ci-publish-packages.test.js index 8b6e496..9f618cd 100644 --- a/tests/ci-publish-packages.test.js +++ b/tests/ci-publish-packages.test.js @@ -149,6 +149,23 @@ async function main() { return new Response(null, { status: options.method === 'POST' ? 403 : 201 }); }), /Link musrflow to repository: HTTP 403/); assert(!calls.some(call => call.method === 'DELETE')); + // Recovery uses the artifact's release identity, not a newer checkout + // or the ref on which Publish packages was manually dispatched. + const context = { schema: 1, tag: 'v9.8.7', version: '9.8.7', commit: 'a'.repeat(40), runId: '123' }; + const contextFile = path.join(directory, 'release-context.json'); + fs.writeFileSync(path.join(directory, 'build-source.txt'), `muSRFlow=${context.commit}\n`); + fs.writeFileSync(contextFile, JSON.stringify(context)); + const releaseEnv = { ...env, GITEA_REF: 'refs/heads/master', GITEA_EVENT_NAME: 'workflow_dispatch' }; + assert.strictEqual(await publishPackages(directory, releaseEnv, registry), '9.8.7'); + assert([...stored.keys()].some(url => url.endsWith('/9.8.7/release-context.json'))); + await publishPackages(directory, releaseEnv, registry); + const beforeInvalid = calls.length; + await assert.rejects(publishPackages(directory, { ...env, PACKAGE_RUN_ID: '999' }, registry), /run ID mismatch/); + fs.writeFileSync(contextFile, JSON.stringify({ ...context, version: '9.8.8', tag: 'v9.8.8' })); + await assert.rejects(publishPackages(directory, env, registry), /filenames do not match/); + assert.strictEqual(calls.length, beforeInvalid); + fs.writeFileSync(contextFile, JSON.stringify({ ...context, tag: null, runId: '125' })); + assert.strictEqual(await publishPackages(directory, { ...env, PACKAGE_RUN_ID: '125' }, registry), '9.8.7-build.125'); console.log('Gitea package publishing tests passed'); } finally { fs.rmSync(directory, { recursive: true, force: true }); diff --git a/tests/ci-release.test.js b/tests/ci-release.test.js new file mode 100644 index 0000000..3dc6c14 --- /dev/null +++ b/tests/ci-release.test.js @@ -0,0 +1,120 @@ +// Copyright (C) 2026 Zaher Salman +// SPDX-License-Identifier: GPL-3.0-or-later + +const assert = require('assert'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const { execFileSync } = require('child_process'); +const { prepareRelease, readReleaseContext, createDraftRelease } = require('../scripts/ci-release'); + +async function main() { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'musrflow-tag-release-')); + try { + const gitOptions = { cwd: directory, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }; + fs.writeFileSync(path.join(directory, 'package.json'), JSON.stringify({ version: '2.3.4-rc.1' })); + execFileSync('git', ['init'], gitOptions); + execFileSync('git', ['add', 'package.json'], gitOptions); + const identity = ['-c', 'user.name=Release Tests', '-c', 'user.email=tests@example.test', '-c', 'commit.gpgsign=false', '-c', 'tag.gpgSign=false']; + execFileSync('git', [...identity, 'commit', '-m', 'Fixture'], gitOptions); + execFileSync('git', [...identity, 'tag', '-a', 'v2.3.4-rc.1', '-m', 'Annotated fixture'], gitOptions); + const env = { PACKAGE_RUN_ID: '456', GITEA_EVENT_NAME: 'push', GITEA_REF: 'refs/tags/v2.3.4-rc.1', + GITEA_SERVER_URL: 'https://gitea.example.test', GITEA_REPOSITORY: 'owner/muSRFlow', RELEASES_TOKEN: 'release-token' }; + const context = prepareRelease(directory, env); + assert.strictEqual(context.tag, 'v2.3.4-rc.1'); + execFileSync('git', ['tag', '-d', context.tag], gitOptions); + execFileSync('git', [...identity, 'tag', context.tag], gitOptions); + assert.deepStrictEqual(prepareRelease(directory, env), context, 'lightweight tags resolve to the same commit'); + assert.strictEqual(prepareRelease(directory, { ...env, GITEA_EVENT_NAME: 'workflow_dispatch' }).tag, null); + for (const ref of ['refs/tags/v2.3.5', 'refs/heads/master', 'refs/tags/invalid']) { + assert.throws(() => prepareRelease(directory, { ...env, GITEA_REF: ref }), /must match package.json/); + } + assert.throws(() => prepareRelease(directory, { ...env, GITEA_EVENT_NAME: 'release' }), /Only version-tag/); + execFileSync('git', [...identity, 'commit', '--allow-empty', '-m', 'Wrong checkout'], gitOptions); + assert.throws(() => prepareRelease(directory, env), /does not match the release tag/); + assert.strictEqual(readReleaseContext(directory, '456'), null); + const sourceFile = path.join(directory, 'build-source.txt'); + const contextFile = path.join(directory, 'release-context.json'); + fs.writeFileSync(sourceFile, `muSRFlow=${context.commit}\n`); + fs.writeFileSync(contextFile, JSON.stringify(context)); + assert.deepStrictEqual(readReleaseContext(directory, '456'), context); + assert.throws(() => readReleaseContext(directory, '457'), /run ID mismatch/); + fs.writeFileSync(sourceFile, 'muSRFlow=wrong\n'); + assert.throws(() => readReleaseContext(directory, '456'), /source commit/); + fs.writeFileSync(sourceFile, `muSRFlow=${context.commit}\n`); + for (const change of [{ schema: 2 }, { tag: 'v9.9.9' }, { version: '2.3.4-01' }, { commit: 'invalid' }]) { + fs.writeFileSync(contextFile, JSON.stringify({ ...context, ...change })); + assert.throws(() => readReleaseContext(directory, '456'), /Invalid release context/); + } + fs.writeFileSync(contextFile, JSON.stringify(context)); + + for (const file of [`musrflow-${context.version}-x86_64.rpm`, `muSRFlow-${context.version}-x86_64.AppImage`, + `musrflow-runtime-${context.version}-x86_64.rpm`, `musrflow-runtime-${context.version}-x86_64.AppImage`]) { + fs.writeFileSync(path.join(directory, `${file}.sha256`), `${'a'.repeat(64)} ${file}\n`); + } + let existing = null; + let tagCommit = context.commit; + let tagStatus = 200; + let createStatus = 201; + const writes = []; + async function api(url, options) { + assert.strictEqual(options.headers.Authorization, 'token release-token'); + assert.strictEqual(options.redirect, 'error'); + if (options.method === 'POST') { + assert(url.endsWith('/releases')); + const payload = JSON.parse(options.body); + writes.push(payload); + return Response.json({ ...payload, id: 10, html_url: 'https://gitea.example.test/draft' }, { status: createStatus }); + } + if (url.includes('/releases/tags/')) return existing ? Response.json(existing) : new Response(null, { status: 404 }); + assert(url.endsWith(`/tags/${context.tag}`)); + return Response.json({ commit: { sha: tagCommit } }, { status: tagStatus }); + } + const draft = await createDraftRelease(directory, env, api); + assert.strictEqual(draft.draft, true); + assert.strictEqual(draft.prerelease, true); + assert.strictEqual(draft.tag_name, context.tag); + assert.strictEqual(draft.target_commitish, context.commit); + assert(draft.body.includes('/generic/musrflow/2.3.4-rc.1/')); + assert(draft.body.includes('/generic/musrflow-runtime/2.3.4-rc.1/')); + assert(draft.body.includes('a'.repeat(64))); + assert(draft.body.includes('Packaging workflow run ID: 456')); + existing = { ...draft, body: draft.body + '\nUser-written notes', draft: false }; + assert.deepStrictEqual(await createDraftRelease(directory, env, api), existing); + assert.strictEqual(writes.length, 1, 'do not edit or unpublish an existing Release'); + existing = { ...draft, body: 'Unrelated release' }; + await assert.rejects(createDraftRelease(directory, env, api), /not changed/); + tagCommit = 'b'.repeat(40); + await assert.rejects(createDraftRelease(directory, env, api), /tag moved/); + tagCommit = context.commit; + tagStatus = 404; + await assert.rejects(createDraftRelease(directory, env, api), /verify release tag: HTTP 404/); + assert.strictEqual(writes.length, 1, 'never create a missing tag'); + tagStatus = 200; + existing = null; + createStatus = 403; + await assert.rejects(createDraftRelease(directory, env, api), /Create draft Release: HTTP 403/); + await assert.rejects(createDraftRelease(directory, { ...env, RELEASES_TOKEN: '' }, api), /write token/); + // Stable versions are not marked as prereleases. + const stable = { ...context, tag: 'v2.3.4', version: '2.3.4' }; + fs.writeFileSync(contextFile, JSON.stringify(stable)); + for (const name of fs.readdirSync(directory).filter(name => name.endsWith('.sha256'))) { + fs.writeFileSync(path.join(directory, name.replace('-rc.1', '')), fs.readFileSync(path.join(directory, name), 'utf8').replace('-rc.1', '')); + } + const stableDraft = await createDraftRelease(directory, env, async (url, options) => { + if (options.method === 'POST') return Response.json({ ...JSON.parse(options.body), html_url: 'https://gitea.example.test/stable-draft' }, { status: 201 }); + if (url.includes('/releases/tags/')) return new Response(null, { status: 404 }); + return Response.json({ commit: { sha: context.commit } }); + }); + assert.strictEqual(stableDraft.prerelease, false); + fs.writeFileSync(contextFile, JSON.stringify({ ...context, tag: null })); + assert.strictEqual(await createDraftRelease(directory, env, async () => { throw new Error('Manual builds must not call the API'); }), null); + fs.unlinkSync(contextFile); + assert.strictEqual(await createDraftRelease(directory, env, async () => { throw new Error('Old artifacts must not call the API'); }), null); + console.log('Tag validation and draft release tests passed'); + } finally { + fs.rmSync(directory, { recursive: true, force: true }); + } +} + +main().catch(error => { console.error(error); process.exitCode = 1; }); diff --git a/tests/release-packaging.test.js b/tests/release-packaging.test.js index 88b520e..7a81c04 100644 --- a/tests/release-packaging.test.js +++ b/tests/release-packaging.test.js @@ -114,5 +114,15 @@ assert(publishWorkflow.includes('node scripts/ci-publish-packages.js artifacts') assert(!publishWorkflow.includes('electron-builder')); assert(!publishWorkflow.includes('npm ci')); assert(packageWorkflow.includes('Packaging run ID for Publish packages:')); +assert(packageWorkflow.includes("tags:\n - 'v*'")); +assert(!packageWorkflow.includes('\n release:'), 'do not build twice for a tag and its Release'); +assert(packageWorkflow.includes("gitea.event.deleted != true")); +assert(packageWorkflow.includes('contents: write')); +assert(packageWorkflow.indexOf('node scripts/ci-release.js prepare') < packageWorkflow.indexOf('name: Download selected runtime')); +assert(packageWorkflow.includes('cp .ci-release-context.json artifacts/release-context.json')); +assert(packageWorkflow.indexOf('node scripts/ci-publish-packages.js artifacts') < packageWorkflow.indexOf('node scripts/ci-release.js draft artifacts')); +assert(publishWorkflow.indexOf('node scripts/ci-publish-packages.js artifacts') < publishWorkflow.indexOf('node scripts/ci-release.js draft artifacts')); +assert(publishWorkflow.includes('contents: write')); +assert(publishWorkflow.includes('RELEASES_TOKEN: ${{ secrets.RELEASES_TOKEN || secrets.GITEA_TOKEN }}')); console.log('release packaging tests passed');