Revised Docker image build process.

This commit is contained in:
2026-02-02 06:11:15 +01:00
parent bcc7983849
commit c21c0f863e
5 changed files with 68 additions and 75 deletions

View File

@@ -2,6 +2,8 @@
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const { parseArgs } = require('util');
function execCommand(command, description) {
@@ -31,20 +33,27 @@ function getContainerTool() {
}
}
function getVersion() {
try {
// Try to get version from git tag
const gitTag = execSync('git tag --points-at HEAD', { encoding: 'utf8' }).trim();
if (gitTag) {
return { version: gitTag.replace(/^v/, ''), isRelease: true };
}
} catch (error) {
// Git command failed, ignore
async function generateVersionFile() {
const versionModuleUrl = pathToFileURL(path.join(__dirname, 'version.mjs')).href;
const { generateVersionFile: generate } = await import(versionModuleUrl);
const versionFilePath = path.join(__dirname, '..', 'src', 'version.js');
generate(versionFilePath);
return versionFilePath;
}
function readVersionFile(versionFilePath) {
const contents = fs.readFileSync(versionFilePath, 'utf8');
const versionMatch = contents.match(/export const VERSION = '([^']+)';/);
const releaseMatch = contents.match(/export const IS_RELEASE = (true|false);/);
if (!versionMatch || !releaseMatch) {
throw new Error(`Could not parse version file at ${versionFilePath}`);
}
// Development build - use package.json version with -dev suffix
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
return { version: `${packageJson.version}-dev`, isRelease: false };
return {
version: versionMatch[1],
isRelease: releaseMatch[1] === 'true'
};
}
function getHostArchitecture() {
@@ -77,7 +86,7 @@ Examples:
build-image.js -h # Show help`);
}
function main() {
async function main() {
const { values } = parseArgs({
options: {
help: {
@@ -105,7 +114,8 @@ function main() {
}
const containerTool = getContainerTool();
const { version, isRelease } = getVersion();
const versionFilePath = await generateVersionFile();
const { version, isRelease } = readVersionFile(versionFilePath);
let architectures;
if (values['all-arch']) {
@@ -160,5 +170,8 @@ function main() {
}
if (require.main === module) {
main();
}
main().catch((error) => {
console.error(`Error: ${error.message}`);
process.exit(1);
});
}

View File

@@ -1,48 +0,0 @@
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Read package.json for base version
const packagePath = './package.json';
const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
let version = pkg.version;
let isRelease = false;
try {
// Check if current commit is tagged
const gitTag = execSync('git tag --points-at HEAD', { encoding: 'utf8' }).trim();
if (gitTag) {
// We're at a tagged commit - extract version from tag
const tagVersion = gitTag.replace(/^v/, ''); // Remove 'v' prefix if present
version = tagVersion;
console.log(`✅ Building release version ${version} (tagged: ${gitTag})`);
isRelease = true;
} else {
// We're not at a tagged commit - use unknown version
version = 'unknown';
console.log(`📦 Building development version with unknown version`);
isRelease = false;
}
} catch (error) {
// Git command failed (maybe not in a git repo)
version = 'unknown';
console.log(`⚠️ Cannot determine git status, using unknown version`);
isRelease = false;
}
// Generate version.js file
const versionFile = path.join('./src', 'version.js');
const versionContent = `// Auto-generated version file - do not edit manually
// Generated at: ${new Date().toISOString()}
export const VERSION = '${version}';
export const IS_RELEASE = ${isRelease};
export const BUILD_TIME = '${new Date().toISOString()}';
`;
fs.writeFileSync(versionFile, versionContent);
console.log(`📝 Generated ${versionFile} with version ${version}`);

View File

@@ -63,3 +63,7 @@ export const BUILD_TIME = '${buildDate}';
`,
);
}
if (import.meta.url === `file://${process.argv[1]}`) {
generateVersionFile("src/version.js");
}