Add Gitea CI for validation and package publishing
Validate TRIMSP / validate (push) Successful in 9s

This commit is contained in:
salman committed 2026-11-16 16:40:53 +01:00
1 parent 0a86f85231
commit 98a973a9fd
7 files changed
+727

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
name: Build and package
on:
push:
tags:
- 'v*'
workflow_dispatch:
permissions:
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:
run:
shell: bash
env:
ELECTRON_SKIP_BINARY_DOWNLOAD: '1'
PACKAGE_RUN_ID: ${{ gitea.run_id }}
GITEA_EVENT_NAME: ${{ gitea.event_name }}
GITEA_REF: ${{ gitea.ref }}
PACKAGE_PLATFORM: linux
steps:
- name: Check out TRIMSP
uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
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: Install system dependencies
run: |
set -euo pipefail
sudo apt-get update
sudo apt-get install -y gfortran rpm dpkg fakeroot
- name: Install dependencies
run: npm ci --no-audit --no-fund
- name: Run tests
run: npm run test:wsl-runtime
- name: Build static engine and Linux packages
run: |
set -euo pipefail
npm run build:fortran
npx electron-forge make --targets=@electron-forge/maker-deb --platform=linux --arch=x64
npx electron-forge make --targets=@electron-forge/maker-rpm --platform=linux --arch=x64
npm run checksums || true
- name: Collect package artifacts
run: |
set -euo pipefail
mkdir -p artifacts
shopt -s nullglob
found=0
for f in out/make/deb/x64/*.deb out/make/rpm/x64/*.rpm; do
test -s "$f"
cp "$f" artifacts/
if [ -f "$f.sha256" ]; then cp "$f.sha256" artifacts/; fi
if [ -f "$f.md5" ]; then cp "$f.md5" artifacts/; fi
found=1
done
if [ "$found" -eq 0 ]; then
echo "No Linux packages produced" >&2
find out -type f || true
exit 1
fi
printf 'trimsp=%s\n' "$(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:
name: trimsp-linux-packages
path: artifacts/
if-no-files-found: error
compression-level: 0
retention-days: 30
- name: Publish and link packages to repository
env:
GITEA_SERVER_URL: ${{ gitea.server_url }}
GITEA_REPOSITORY: ${{ gitea.repository }}
PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }}
run: node scripts/ci-publish-packages.js artifacts
- name: Create draft Release when both platforms are ready
env:
GITEA_SERVER_URL: ${{ gitea.server_url }}
GITEA_REPOSITORY: ${{ gitea.repository }}
RELEASES_TOKEN: ${{ secrets.RELEASES_TOKEN || secrets.GITEA_TOKEN }}
PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }}
run: node scripts/ci-release.js draft artifacts
+101
View File
@@ -0,0 +1,101 @@
name: Build Windows installer
on:
push:
tags:
- 'v*'
workflow_dispatch:
permissions:
contents: write
actions: read
packages: write
jobs:
build-windows:
if: ${{ gitea.event_name != 'push' || gitea.event.deleted != true }}
runs-on: ubuntu-latest-full
timeout-minutes: 120
defaults:
run:
shell: bash
env:
ELECTRON_SKIP_BINARY_DOWNLOAD: '1'
PACKAGE_RUN_ID: ${{ gitea.run_id }}
GITEA_EVENT_NAME: ${{ gitea.event_name }}
GITEA_REF: ${{ gitea.ref }}
PACKAGE_PLATFORM: windows
ENGINE: podman
steps:
- name: Check out TRIMSP
uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
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: Install system dependencies
run: |
set -euo pipefail
sudo apt-get update
sudo apt-get install -y gfortran podman
- name: Install dependencies
run: npm ci --no-audit --no-fund
- name: Run tests
run: npm run test:wsl-runtime
- name: Build static engine and WSL2 rootfs
run: |
set -euo pipefail
npm run build:fortran
bash packaging/wsl2/build-wsl2.sh
node packaging/wsl2/require-rootfs.js
- name: Build Windows NSIS installer
run: |
set -euo pipefail
npx --yes electron-builder@26.15.3 --win nsis --x64 --publish never
- name: Collect Windows artifacts
run: |
set -euo pipefail
mkdir -p artifacts
test -s dist/TRIMSP-NL-Workbench-Setup.exe
cp dist/TRIMSP-NL-Workbench-Setup.exe artifacts/
(cd artifacts && sha256sum TRIMSP-NL-Workbench-Setup.exe > TRIMSP-NL-Workbench-Setup.exe.sha256)
(cd artifacts && md5sum TRIMSP-NL-Workbench-Setup.exe > TRIMSP-NL-Workbench-Setup.exe.md5)
printf 'trimsp=%s\n' "$(git rev-parse HEAD)" > artifacts/build-source.txt
cp .ci-release-context.json artifacts/release-context.json
- name: Upload Windows installer
uses: https://github.com/actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: trimsp-windows-x64
path: artifacts/
if-no-files-found: error
compression-level: 0
retention-days: 30
- name: Publish and link Windows packages
env:
GITEA_SERVER_URL: ${{ gitea.server_url }}
GITEA_REPOSITORY: ${{ gitea.repository }}
PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }}
run: node scripts/ci-publish-packages.js artifacts
- name: Create draft Release when both platforms are ready
env:
GITEA_SERVER_URL: ${{ gitea.server_url }}
GITEA_REPOSITORY: ${{ gitea.repository }}
RELEASES_TOKEN: ${{ secrets.RELEASES_TOKEN || secrets.GITEA_TOKEN }}
PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }}
run: node scripts/ci-release.js draft artifacts
+77
View File
@@ -0,0 +1,77 @@
name: Publish packages
on:
workflow_dispatch:
inputs:
package_run_id:
description: 'Linux or Windows packaging workflow run ID with an uploaded artifact (also retries failed publication)'
required: true
platform:
description: 'Package platform: linux or windows (must match the selected run)'
required: true
default: linux
permissions:
contents: write
actions: read
packages: write
jobs:
publish:
runs-on: ubuntu-latest-full
timeout-minutes: 120
defaults:
run:
shell: bash
env:
GITEA_SERVER_URL: ${{ gitea.server_url }}
GITEA_REPOSITORY: ${{ gitea.repository }}
PACKAGE_RUN_ID: ${{ inputs.package_run_id }}
PACKAGE_PLATFORM: ${{ inputs.platform || 'linux' }}
steps:
- name: Check out publisher
uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
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: Check selected package artifact
env:
ACTIONS_TOKEN: ${{ secrets.GITEA_TOKEN }}
run: node scripts/ci-check-package-run.js
- name: Download existing Linux packages
if: ${{ env.PACKAGE_PLATFORM == 'linux' }}
uses: https://github.com/actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
with:
name: trimsp-linux-packages
path: artifacts
run-id: ${{ inputs.package_run_id }}
repository: ${{ gitea.repository }}
github-token: ${{ secrets.GITEA_TOKEN }}
- name: Download existing Windows packages
if: ${{ env.PACKAGE_PLATFORM == 'windows' }}
uses: https://github.com/actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
with:
name: trimsp-windows-x64
path: artifacts
run-id: ${{ inputs.package_run_id }}
repository: ${{ gitea.repository }}
github-token: ${{ secrets.GITEA_TOKEN }}
- name: Publish and link packages to repository
env:
PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }}
run: node scripts/ci-publish-packages.js artifacts
- name: Create or preserve draft Release
env:
RELEASES_TOKEN: ${{ secrets.RELEASES_TOKEN || secrets.GITEA_TOKEN }}
PACKAGES_TOKEN: ${{ secrets.PACKAGES_TOKEN || secrets.GITEA_TOKEN }}
run: node scripts/ci-release.js draft artifacts
+45
View File
@@ -0,0 +1,45 @@
name: Validate TRIMSP
on:
workflow_dispatch:
push:
branches:
- master
- electron-builder-nsis
permissions:
contents: read
jobs:
validate:
runs-on: ubuntu-latest-full
timeout-minutes: 20
defaults:
run:
shell: bash
env:
ELECTRON_SKIP_BINARY_DOWNLOAD: '1'
steps:
- name: Check out TRIMSP
uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
github-server-url: ${{ gitea.server_url }}
persist-credentials: false
- name: Inspect runner and prerequisites
run: |
set -euo pipefail
uname -sm
cat /etc/os-release
for tool in git python3 bash tar gzip node npm; do
command -v "$tool" || echo "missing: $tool"
done
git --version
node --version
npm --version
- name: Install dependencies
run: npm ci --no-audit --no-fund
- name: Run tests
run: npm run test:wsl-runtime
+54
View File
@@ -0,0 +1,54 @@
// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
async function checkPackageRun(env = process.env, request = fetch) {
const { GITEA_SERVER_URL, GITEA_REPOSITORY, PACKAGE_RUN_ID, ACTIONS_TOKEN } = env;
if (!/^[1-9][0-9]*$/.test(PACKAGE_RUN_ID || '')) throw new Error('A numeric packaging workflow run ID is required');
if (!/^[\w.-]+\/[\w.-]+$/.test(GITEA_REPOSITORY || '')) throw new Error('Expected owner/repository');
if (!ACTIONS_TOKEN) throw new Error('An Actions read token is required');
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(/\/$/, '')}/api/v1/repos/${GITEA_REPOSITORY}/actions/runs/${PACKAGE_RUN_ID}`;
const headers = { Authorization: `token ${ACTIONS_TOKEN}` };
const response = await request(base, {
headers, redirect: 'error', signal: AbortSignal.timeout(60000)
});
if (!response.ok) throw new Error(`Cannot inspect packaging run: HTTP ${response.status}`);
const run = await response.json();
const workflow = String(run.path || '').split('@')[0].split('/').pop();
const platform = env.PACKAGE_PLATFORM || 'linux';
if (!['linux', 'windows'].includes(platform)) throw new Error('Expected linux or windows package platform');
const expectedWorkflow = platform === 'windows' ? 'build-windows.yml' : 'build-and-package.yml';
const artifactName = platform === 'windows' ? 'trimsp-windows-x64' : 'trimsp-linux-packages';
if (workflow !== expectedWorkflow) {
throw new Error(`Run ${PACKAGE_RUN_ID} is not ${platform === 'windows' ? 'Build Windows installer' : 'Build and package'} (${run.path || 'unknown workflow'}). Select the matching platform and packaging run ID`);
}
if (!['success', 'failure'].includes(run.conclusion)) {
throw new Error('The packaging run must finish before publishing its artifacts');
}
const artifactResponse = await request(`${base}/artifacts?name=${artifactName}`, {
headers, redirect: 'error', signal: AbortSignal.timeout(60000)
});
if (!artifactResponse.ok) throw new Error(`Cannot inspect package artifacts: HTTP ${artifactResponse.status}`);
const listing = await artifactResponse.json();
const matches = (listing.artifacts || []).filter(artifact => artifact.name === artifactName);
if (matches.length !== 1) throw new Error(`Run ${PACKAGE_RUN_ID} must contain one ${artifactName} artifact; check its upload step`);
const artifact = matches[0];
if (artifact.expired || (artifact.expires_at && Date.parse(artifact.expires_at) <= Date.now())) {
throw new Error(`The package artifact for run ${PACKAGE_RUN_ID} has expired`);
}
console.log(`Selected packaging run ${PACKAGE_RUN_ID}, artifact ${artifact.id}: ${artifact.name}`);
if (run.conclusion === 'failure') console.log('Retrying publication from the saved package artifact; no rebuild needed');
return artifact;
}
module.exports = { checkPackageRun };
if (require.main === module) {
checkPackageRun().catch(error => {
console.error(error.message);
process.exitCode = 1;
});
}
+156
View File
@@ -0,0 +1,156 @@
// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
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;
if (!/^[1-9][0-9]*$/.test(PACKAGE_RUN_ID || '')) throw new Error('A packaging workflow run ID is required');
if (!PACKAGES_TOKEN) throw new Error('A package 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('Gitea must use an HTTPS server URL without credentials, query, or fragment');
}
const files = fs.readdirSync(directory);
const platform = env.PACKAGE_PLATFORM || 'linux';
if (!['linux', 'windows'].includes(platform)) throw new Error('Expected linux or windows package platform');
const debs = files.filter(name => /\.deb$/.test(name) && !/\.md5$|\.sha256$/.test(name));
const rpms = files.filter(name => /\.rpm$/.test(name) && !/\.md5$|\.sha256$/.test(name));
const windows = files.filter(name => /^TRIMSP-NL-Workbench-Setup\.exe$/.test(name));
if (platform === 'linux' && debs.length + rpms.length === 0) throw new Error('Expected at least one DEB or RPM package');
if (platform === 'windows' && windows.length !== 1) throw new Error('Expected exactly one Windows installer');
const pkgJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'));
const version = pkgJson.version;
if (!/^[\w.+-]+$/.test(version)) throw new Error('Invalid package version');
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 = {};
if (platform === 'windows') {
groups['trimsp-windows'] = [windows[0]];
} else {
groups['trimsp'] = [...debs, ...rpms];
}
// Validate and add checksums. Stream large files.
for (const names of Object.values(groups)) {
for (const name of [...names]) {
const file = path.join(directory, name);
if (!fs.lstatSync(file).isFile() || fs.statSync(file).size === 0) throw new Error(`Missing or empty package: ${name}`);
for (const [algorithm, factory] of Object.entries({ sha256: () => crypto.createHash('sha256'), md5: () => crypto.createHash('md5') })) {
const sidecar = `${name}.${algorithm}`;
const sidecarPath = path.join(directory, sidecar);
if (!fs.existsSync(sidecarPath)) {
const hash = factory();
for await (const chunk of fs.createReadStream(file)) hash.update(chunk);
fs.writeFileSync(sidecarPath, `${hash.digest('hex')} ${name}\n`);
}
const hash = factory();
for await (const chunk of fs.createReadStream(file)) hash.update(chunk);
const checksum = fs.readFileSync(sidecarPath, 'utf8').trim();
const expected = `${hash.digest('hex')} ${name}`;
if (checksum !== expected) throw new Error(`Checksum mismatch: ${sidecar}`);
names.push(sidecar);
}
}
names.push('build-source.txt');
}
// Publish provenance last in each package. Release aggregation uses its
// presence as the completion marker, never an installer still uploading.
if (release) {
for (const names of Object.values(groups)) names.push('release-context.json');
}
for (const names of Object.values(groups)) {
for (const name of names) {
const stat = fs.lstatSync(path.join(directory, name));
if (!stat.isFile() || !stat.size) throw new Error(`Missing or empty artifact: ${name}`);
}
}
return publishRegistryFiles(directory, groups, registryVersion, env, request);
}
async function publishRegistryFiles(directory, groups, registryVersion, env = process.env, request = fetch) {
const { GITEA_SERVER_URL, GITEA_REPOSITORY, PACKAGES_TOKEN } = env;
if (!PACKAGES_TOKEN) throw new Error('A package 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('Gitea must use an HTTPS server URL without credentials, query, or fragment');
}
const base = server.href.replace(/\/$/, '');
const [owner, repository] = GITEA_REPOSITORY.split('/').map(encodeURIComponent);
const headers = { Authorization: `token ${PACKAGES_TOKEN}` };
for (const [name, names] of Object.entries(groups)) {
for (const file of names) {
const url = `${base}/api/packages/${owner}/generic/${name}/${encodeURIComponent(registryVersion)}/${encodeURIComponent(file)}`;
const body = fs.createReadStream(path.join(directory, file));
let response;
try {
response = await request(url, {
method: 'PUT', headers: { ...headers, 'Content-Type': 'application/octet-stream',
'Content-Length': String(fs.statSync(path.join(directory, file)).size) },
body, duplex: 'half', redirect: 'error', signal: AbortSignal.timeout(30 * 60 * 1000)
});
} finally {
body.destroy();
}
// A retried publish can safely skip identical files, but never replace them.
if (response.status === 409) {
const existing = await request(url, {
headers, redirect: 'error', signal: AbortSignal.timeout(30 * 60 * 1000)
});
if (existing.status !== 200) throw new Error(`Cannot verify existing ${file}: HTTP ${existing.status}`);
const remoteHash = crypto.createHash('sha256');
for await (const chunk of existing.body) remoteHash.update(chunk);
const localHash = crypto.createHash('sha256');
for await (const chunk of fs.createReadStream(path.join(directory, file))) localHash.update(chunk);
if (remoteHash.digest('hex') !== localHash.digest('hex')) throw new Error(`Existing package differs: ${file}; nothing was overwritten`);
} else if (response.status !== 201) {
throw new Error(`Upload ${file}: HTTP ${response.status}. Check package write permissions and server upload limits`);
}
if (response.body) await response.body.cancel();
console.log(`Published ${name}/${registryVersion}/${file}`);
}
const linked = await request(`${base}/api/v1/packages/${owner}/generic/${name}/-/link/${repository}`, {
method: 'POST', headers, redirect: 'error', signal: AbortSignal.timeout(60000)
});
if (linked.status === 400) {
if (linked.body) await linked.body.cancel();
const metadata = await request(`${base}/api/v1/packages/${owner}/generic/${name}/${encodeURIComponent(registryVersion)}`, {
headers, redirect: 'error', signal: AbortSignal.timeout(60000)
});
if (metadata.status !== 200) {
if (metadata.body) await metadata.body.cancel();
throw new Error(`Link ${name} returned HTTP 400; cannot verify existing repository link: HTTP ${metadata.status}`);
}
const pkg = await metadata.json();
const destination = pkg.repository && pkg.repository.full_name;
if (typeof destination !== 'string' || destination.toLowerCase() !== GITEA_REPOSITORY.toLowerCase()) {
throw new Error(`Link ${name} returned HTTP 400; repository is ${destination || 'not linked'}, expected ${GITEA_REPOSITORY}`);
}
console.log(`${name} is already linked to ${destination}`);
} else {
if (linked.body) await linked.body.cancel();
if (linked.status !== 201) throw new Error(`Link ${name} to repository: HTTP ${linked.status}`);
}
}
console.log(`Packages: ${base}/${owner}/${repository}/packages (version ${registryVersion})`);
return registryVersion;
}
module.exports = { publishPackages, publishRegistryFiles };
if (require.main === module) {
publishPackages(process.argv[2] || 'artifacts').catch(error => {
console.error(error.message);
process.exitCode = 1;
});
}
+188
View File
@@ -0,0 +1,188 @@
// 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(`trimsp=${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 = `<!-- trimsp-release:${context.tag}:${context.commit} -->`;
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];
if (!env.PACKAGES_TOKEN) throw new Error('A package read token is required to assemble the cross-platform Release');
const packageHeaders = { Authorization: `token ${env.PACKAGES_TOKEN}` };
const registry = `${base}/api/packages/${owner}/generic`;
const contexts = {};
// Either build may finish first. Only assemble a draft after all
// package groups have published their final provenance file.
for (const name of ['trimsp', 'trimsp-windows']) {
const response = await request(`${registry}/${name}/${encodeURIComponent(context.version)}/release-context.json`, {
headers: packageHeaders, redirect: 'error', signal: AbortSignal.timeout(60000)
});
if (response.status === 404) {
if (response.body) await response.body.cancel();
console.log(`Release pending: ${name}/${context.version} is not published yet. The other build will finish the draft; retry Publish packages if needed.`);
return null;
}
if (response.status !== 200) throw new Error(`Cannot inspect ${name} release context: HTTP ${response.status}`);
const remote = await response.json();
if (remote.schema !== 1 || remote.tag !== context.tag || remote.version !== context.version
|| remote.commit !== context.commit || !/^[1-9][0-9]*$/.test(remote.runId || '')) {
throw new Error(`${name} does not match the release tag and source commit`);
}
contexts[name] = remote;
}
// Discover published files by probing known name patterns.
const candidates = [
['trimsp', `trimsp_${context.version}_amd64.deb`],
['trimsp', `TrimSP_${context.version}_amd64.deb`],
['trimsp', `trimsp-${context.version}-1.x86_64.rpm`],
['trimsp', `TrimSP-${context.version}-1.x86_64.rpm`],
['trimsp-windows', `TRIMSP-NL-Workbench-Setup.exe`]
];
const downloads = [];
for (const [name, file] of candidates) {
const url = `${registry}/${name}/${encodeURIComponent(context.version)}/${encodeURIComponent(file)}.sha256`;
const r = await request(url, { headers: packageHeaders, redirect: 'error', signal: AbortSignal.timeout(60000) });
if (r.status === 200) { await r.body.cancel(); downloads.push([name, file]); }
else if (r.body) await r.body.cancel();
}
const lines = [marker, '', `# TRIMSP-NL Workbench ${context.version}`, '',
'Linux x86_64 packages (DEB/RPM) bundle the static `trimspNL` engine.', '',
'Windows x64: the unsigned NSIS installer bundles the engine and a private WSL2 runtime (WSL2 required).', '',
'Review and smoke-test these downloads before publishing this draft.', '',
'| Download | SHA-256 |', '| --- | --- |'];
for (const [name, file] of downloads) {
const url = `${registry}/${name}/${encodeURIComponent(context.version)}/${encodeURIComponent(file)}`;
const checksumResponse = await request(`${url}.sha256`, {
headers: packageHeaders, redirect: 'error', signal: AbortSignal.timeout(60000)
});
if (checksumResponse.status !== 200) throw new Error(`Cannot read checksum for ${file}: HTTP ${checksumResponse.status}`);
const checksum = (await checksumResponse.text()).trim();
if (!/^[a-f0-9]{64} /.test(checksum) || checksum.slice(66) !== file) throw new Error(`Invalid checksum sidecar for ${file}`);
lines.push(`| [${file}](${url}) | \`${checksum.slice(0, 64)}\` ([checksum](${url}.sha256)) |`);
}
lines.push('', `Source commit: [${context.commit}](${base}/${GITEA_REPOSITORY}/commit/${context.commit})`,
'', `Linux packaging workflow run ID: ${contexts.trimsp.runId}.`,
`Windows packaging workflow run ID: ${contexts['trimsp-windows'].runId}.`);
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: `TRIMSP-NL Workbench ${context.version}`, body: lines.join('\n'), draft: true,
prerelease: context.version.split('+')[0].includes('-') })
});
// Both platforms may observe a missing draft at the same time.
if ([409, 422].includes(response.status)) {
if (response.body) await response.body.cancel();
const concurrent = await request(`${api}/releases/tags/${encodeURIComponent(context.tag)}`, {
headers, redirect: 'error', signal: AbortSignal.timeout(60000)
});
if (concurrent.status === 200) {
const release = await concurrent.json();
if (release.tag_name === context.tag && String(release.body || '').includes(marker)) {
console.log(`Release created by the other workflow; preserving it: ${release.html_url}`);
return release;
}
}
throw new Error('Concurrent Release could not be verified; retry Publish packages without rebuilding');
}
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;
});
}