100 lines
4.9 KiB
JavaScript
100 lines
4.9 KiB
JavaScript
import {spawnSync} from 'node:child_process';
|
|
import {pathToFileURL} from 'node:url';
|
|
|
|
export const images = ['pageswiki-app', 'pageswiki-adaptor-gateway', 'pageswiki-excalidraw-viewer'];
|
|
// SemVer 2.0.0: numeric prerelease identifiers cannot have leading zeroes.
|
|
const number = '(0|[1-9][0-9]*)';
|
|
const identifier = '(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)';
|
|
const semver = new RegExp(`^${number}\\.${number}\\.${number}(?:-${identifier}(?:\\.${identifier})*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$`);
|
|
|
|
export function imageTag(ref) {
|
|
if (!ref.startsWith('refs/tags/v')) throw new Error('Release must run on a refs/tags/v… Git tag');
|
|
const tag = ref.slice('refs/tags/'.length);
|
|
if (!semver.test(tag.slice(1)) || /\s/.test(tag)) throw new Error(`Invalid SemVer 2.0.0 tag: ${tag}`);
|
|
// Gitea stores container tags case-insensitively; reject instead of silently
|
|
// collapsing distinct SemVer identifiers. '+' is not a Docker tag character.
|
|
if (/[A-Z]/.test(tag)) throw new Error('Release identifiers must be lowercase for the Gitea registry');
|
|
const result = tag.replace('+', '_');
|
|
if (result.length > 128) throw new Error('Container release tag exceeds 128 characters');
|
|
return result;
|
|
}
|
|
|
|
function run(command, args) {
|
|
const result = spawnSync(command, args, {encoding: 'utf8', timeout: 120000, maxBuffer: 4 * 1024 * 1024});
|
|
if (result.error) throw result.error;
|
|
if (result.status !== 0) throw new Error(`${command} failed: ${result.stderr.trim()}`);
|
|
return result.stdout.trim();
|
|
}
|
|
|
|
export function dockerClient(execute = run) {
|
|
function inspect(ref, field, optional = false) {
|
|
try {
|
|
return JSON.parse(execute('docker', ['buildx', 'imagetools', 'inspect', '--format', `{{json .${field}}}`, ref]));
|
|
} catch (error) {
|
|
// Only an explicit missing manifest is absence, never auth/TLS/timeouts.
|
|
if (optional && (error.message.endsWith(`ERROR: ${ref}: not found`) || /\bmanifest unknown\b/i.test(error.message))) return null;
|
|
throw error;
|
|
}
|
|
}
|
|
return {
|
|
digest(ref, optional = false) {
|
|
const manifest = inspect(ref, 'Manifest', optional);
|
|
if (manifest === null) return null;
|
|
if (!/^sha256:[0-9a-f]{64}$/.test(manifest.digest)) throw new Error(`Invalid registry digest: ${ref}`);
|
|
return manifest.digest;
|
|
},
|
|
revision(ref) {
|
|
const config = inspect(ref, 'Image');
|
|
if (config.os !== 'linux' || config.architecture !== 'amd64') throw new Error(`Expected linux/amd64 source: ${ref}`);
|
|
return config.config?.Labels?.['org.opencontainers.image.revision'];
|
|
},
|
|
copy(source, destination) {
|
|
execute('docker', ['buildx', 'imagetools', 'create', '--prefer-index=false', '--tag', destination, source]);
|
|
},
|
|
};
|
|
}
|
|
|
|
export function promote({ref, revision, prefix}, client = dockerClient(), log = console.log) {
|
|
const tag = imageTag(ref);
|
|
if (!/^[0-9a-f]{40}$/.test(revision)) throw new Error('Expected the release commit SHA');
|
|
if (!/^[a-z0-9][a-z0-9.-]*(?::[0-9]+)?\/[a-z0-9][a-z0-9_-]*$/.test(prefix)) throw new Error('Invalid registry/owner prefix');
|
|
// Resolve every source to a digest and check every destination before writes.
|
|
const plan = images.map(image => {
|
|
const repository = `${prefix}/${image}`;
|
|
const digest = client.digest(`${repository}:sha-${revision}`);
|
|
const source = `${repository}@${digest}`;
|
|
if (client.revision(source) !== revision) throw new Error(`Image revision does not match Git tag: ${image}`);
|
|
const destination = `${repository}:${tag}`;
|
|
const existing = client.digest(destination, true);
|
|
if (existing !== null && existing !== digest) throw new Error(`Refusing to overwrite released image: ${destination}`);
|
|
return {source, destination, digest};
|
|
});
|
|
for (const item of plan) {
|
|
// Recheck before writing. Registry tag protection/serialized releases are
|
|
// still required: the registry does not offer a portable compare-and-swap.
|
|
const existing = client.digest(item.destination, true);
|
|
if (existing !== null && existing !== item.digest) throw new Error(`Release changed concurrently: ${item.destination}`);
|
|
if (existing === null) client.copy(item.source, item.destination);
|
|
if (client.digest(item.destination) !== item.digest) throw new Error(`Promoted digest mismatch: ${item.destination}`);
|
|
log(`${item.destination} -> ${item.digest}`);
|
|
}
|
|
return plan;
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
try {
|
|
const ref = process.env.RELEASE_REF || '';
|
|
const tag = imageTag(ref);
|
|
if (process.argv[2] === '--validate') {
|
|
console.log(`Release ${ref.slice('refs/tags/'.length)}; container tag ${tag}`);
|
|
} else if (process.argv.length !== 2) {
|
|
throw new Error('Usage: node tools/ci/release-images.mjs [--validate]');
|
|
} else {
|
|
promote({ref, revision: run('git', ['rev-parse', 'HEAD^{commit}']), prefix: `${process.env.REGISTRY}/${process.env.IMAGE_OWNER}`});
|
|
}
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|