Files
muSRFlow/scripts/ci-publish-packages.js
T
salman 12ecbf4f7d Automate Windows publication and coordinated cross-platform releases
Build and publish Windows installers on version tags using the tested WSL packaging path. Keep Windows metadata in its own package and support publication recovery from saved Linux or Windows artifacts.

Create one draft Release only after matching Linux and Windows provenance is published. Preserve reviewed release notes and handle concurrent creation safely. Cover publishing, recovery, and coordination with regression tests and update deployment and installation guides.
2026-10-30 09:22:59 +01:00

159 lines
9.4 KiB
JavaScript

// 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 application = files.filter(name => /^musrflow-.+-x86_64\.rpm$/.test(name) && !name.startsWith('musrflow-runtime-'));
const windows = files.filter(name => /^muSRFlow-.+-x64\.exe$/.test(name));
if (platform === 'linux' && application.length !== 1) throw new Error('Expected exactly one application RPM');
if (platform === 'windows' && windows.length !== 1) throw new Error('Expected exactly one Windows installer');
// Read the version from the selected artifact, not the publisher checkout.
const version = platform === 'windows'
? windows[0].slice('muSRFlow-'.length, -'-x64.exe'.length)
: application[0].slice('musrflow-'.length, -'-x86_64.rpm'.length);
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 = platform === 'windows' ? { 'musrflow-windows': [windows[0]] } : {
musrflow: [application[0], `muSRFlow-${version}-x86_64.AppImage`],
'musrflow-runtime': [`musrflow-runtime-${version}-x86_64.rpm`, `musrflow-runtime-${version}-x86_64.AppImage`]
};
// Validate the complete set before the first upload. Stream large runtime 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}`);
const hashes = { sha256: crypto.createHash('sha256'), md5: crypto.createHash('md5') };
for await (const chunk of fs.createReadStream(file)) {
for (const hash of Object.values(hashes)) hash.update(chunk);
}
for (const [algorithm, hash] of Object.entries(hashes)) {
const sidecar = `${name}.${algorithm}`;
const checksum = fs.readFileSync(path.join(directory, sidecar), 'utf8').trim();
const expected = `${hash.digest('hex')} ${name}`;
if (checksum !== expected) throw new Error(`Checksum mismatch: ${sidecar}`);
names.push(sidecar);
}
}
// The first manual Windows builds used a platform-specific filename.
names.push(platform === 'windows' && !release && !files.includes('build-source.txt')
? 'windows-build-source.txt' : 'build-source.txt');
}
if (platform === 'windows') {
groups['musrflow-windows'].push('runtime-manifest.json', 'wsl-rootfs.sha256');
} else {
groups['musrflow-runtime'].push('runtime-manifest.json');
}
// 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);
}
// Shared by application releases, durable runtime bundles, and Windows builds.
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)
});
// Gitea returns 400 when any repository link already exists. Confirm
// the destination rather than hiding a bad request or relinking it.
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;
});
}