// Checks, builds and pushes the Docker image to the Gitea registry, tagged `latest` and with the // commit's short hash. Run with `npm run image:publish`; see docs/runbook.md, "Publishing an image". import { execFileSync } from 'node:child_process'; const IMAGE = 'primegit.primecontrols-dev.com/c.west/sharepoint-acc-sync'; const CHECKS = ['lint', 'check', 'test']; function run(command: string, args: string[]): void { console.log(`\n> ${command} ${args.join(' ')}`); execFileSync(command, args, { stdio: 'inherit' }); } function read(command: string, args: string[]): string { return execFileSync(command, args, { encoding: 'utf8' }).trim(); } function npmRun(script: string): void { // npm_execpath is set when this runs through `npm run`; going through node avoids npm.cmd on Windows const npm = process.env.npm_execpath; if (!npm) throw new Error('Run this with `npm run image:publish`.'); run(process.execPath, [npm, 'run', script]); } function publish(): void { // The hash tag has to describe exactly what was built, so uncommitted changes aren't allowed if (read('git', ['status', '--porcelain'])) { throw new Error('There are uncommitted changes. Commit or stash them first.'); } const revision = read('git', ['rev-parse', 'HEAD']); const shortRevision = read('git', ['rev-parse', '--short', 'HEAD']); // Fixed into the image at build time: set it only when the app is served over plain HTTP const origin = process.env.ORIGIN ?? ''; console.log(`Publishing ${IMAGE}:${shortRevision} with ORIGIN ${origin || '(unset, HTTPS)'}`); for (const check of CHECKS) npmRun(check); // Without --provenance=false, Docker Desktop wraps the image in an OCI index with a provenance // attestation, and Gitea's registry rejects that index (404 on the manifest push) run('docker', [ 'build', '--pull', '--provenance=false', '--build-arg', `ORIGIN=${origin}`, '--label', `org.opencontainers.image.revision=${revision}`, '--tag', `${IMAGE}:${shortRevision}`, '--tag', `${IMAGE}:latest`, '.' ]); // The hash tag goes first, so `latest` only moves once the exact build is safely stored run('docker', ['push', `${IMAGE}:${shortRevision}`]); run('docker', ['push', `${IMAGE}:latest`]); console.log(`\nPushed ${IMAGE}:${shortRevision} as latest. Redeploy the stack in Portainer.`); } try { publish(); } catch (error) { console.error(`\nNot published: ${error instanceof Error ? error.message : String(error)}`); process.exit(1); }