feat(docker): add support for no-cache builds and tag built images for local dependency resolution
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@git.zone/tsdocker',
|
||||
version: '1.5.0',
|
||||
version: '1.6.0',
|
||||
description: 'develop npm modules cross platform with docker'
|
||||
}
|
||||
|
||||
@@ -138,10 +138,23 @@ export class Dockerfile {
|
||||
*/
|
||||
public static async buildDockerfiles(
|
||||
sortedArrayArg: Dockerfile[],
|
||||
options?: { platform?: string; timeout?: number },
|
||||
options?: { platform?: string; timeout?: number; noCache?: boolean },
|
||||
): Promise<Dockerfile[]> {
|
||||
for (const dockerfileArg of sortedArrayArg) {
|
||||
await dockerfileArg.build(options);
|
||||
|
||||
// Tag the built image with the full base image references used by dependent Dockerfiles,
|
||||
// so their FROM lines resolve to the locally-built image instead of pulling from a registry.
|
||||
const dependentBaseImages = new Set<string>();
|
||||
for (const other of sortedArrayArg) {
|
||||
if (other.localBaseDockerfile === dockerfileArg && other.baseImage !== dockerfileArg.buildTag) {
|
||||
dependentBaseImages.add(other.baseImage);
|
||||
}
|
||||
}
|
||||
for (const fullTag of dependentBaseImages) {
|
||||
logger.log('info', `Tagging ${dockerfileArg.buildTag} as ${fullTag} for local dependency resolution`);
|
||||
await smartshellInstance.exec(`docker tag ${dockerfileArg.buildTag} ${fullTag}`);
|
||||
}
|
||||
}
|
||||
return sortedArrayArg;
|
||||
}
|
||||
@@ -365,22 +378,23 @@ export class Dockerfile {
|
||||
/**
|
||||
* Builds the Dockerfile
|
||||
*/
|
||||
public async build(options?: { platform?: string; timeout?: number }): Promise<void> {
|
||||
public async build(options?: { platform?: string; timeout?: number; noCache?: boolean }): Promise<void> {
|
||||
logger.log('info', 'now building Dockerfile for ' + this.cleanTag);
|
||||
const buildArgsString = await Dockerfile.getDockerBuildArgs(this.managerRef);
|
||||
const config = this.managerRef.config;
|
||||
const platformOverride = options?.platform;
|
||||
const timeout = options?.timeout;
|
||||
const noCacheFlag = options?.noCache ? ' --no-cache' : '';
|
||||
|
||||
let buildCommand: string;
|
||||
|
||||
if (platformOverride) {
|
||||
// Single platform override via buildx
|
||||
buildCommand = `docker buildx build --platform ${platformOverride} --load -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
||||
buildCommand = `docker buildx build --platform ${platformOverride}${noCacheFlag} --load -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
||||
} else if (config.platforms && config.platforms.length > 1) {
|
||||
// Multi-platform build using buildx
|
||||
const platformString = config.platforms.join(',');
|
||||
buildCommand = `docker buildx build --platform ${platformString} -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
||||
buildCommand = `docker buildx build --platform ${platformString}${noCacheFlag} -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
||||
|
||||
if (config.push) {
|
||||
buildCommand += ' --push';
|
||||
@@ -390,7 +404,7 @@ export class Dockerfile {
|
||||
} else {
|
||||
// Standard build
|
||||
const versionLabel = this.managerRef.projectInfo?.npm?.version || 'unknown';
|
||||
buildCommand = `docker build --label="version=${versionLabel}" -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
||||
buildCommand = `docker build --label="version=${versionLabel}"${noCacheFlag} -t ${this.buildTag} -f ${this.filePath} ${buildArgsString} .`;
|
||||
}
|
||||
|
||||
if (timeout) {
|
||||
|
||||
@@ -139,6 +139,7 @@ export class TsDockerManager {
|
||||
await Dockerfile.buildDockerfiles(toBuild, {
|
||||
platform: options?.platform,
|
||||
timeout: options?.timeout,
|
||||
noCache: options?.noCache,
|
||||
});
|
||||
logger.log('success', 'All Dockerfiles built successfully');
|
||||
|
||||
|
||||
@@ -76,4 +76,5 @@ export interface IBuildCommandOptions {
|
||||
patterns?: string[]; // Dockerfile name patterns (e.g., ['Dockerfile_base', 'Dockerfile_*'])
|
||||
platform?: string; // Single platform override (e.g., 'linux/arm64')
|
||||
timeout?: number; // Build timeout in seconds
|
||||
noCache?: boolean; // Force rebuild without Docker layer cache (--no-cache)
|
||||
}
|
||||
|
||||
@@ -44,6 +44,9 @@ export let run = () => {
|
||||
if (argvArg.timeout) {
|
||||
buildOptions.timeout = Number(argvArg.timeout);
|
||||
}
|
||||
if (argvArg.cache === false) {
|
||||
buildOptions.noCache = true;
|
||||
}
|
||||
|
||||
await manager.build(buildOptions);
|
||||
logger.log('success', 'Build completed successfully');
|
||||
@@ -78,6 +81,9 @@ export let run = () => {
|
||||
if (argvArg.timeout) {
|
||||
buildOptions.timeout = Number(argvArg.timeout);
|
||||
}
|
||||
if (argvArg.cache === false) {
|
||||
buildOptions.noCache = true;
|
||||
}
|
||||
|
||||
// Build images first (if not already built)
|
||||
await manager.build(buildOptions);
|
||||
@@ -130,7 +136,11 @@ export let run = () => {
|
||||
await manager.prepare();
|
||||
|
||||
// Build images first
|
||||
await manager.build();
|
||||
const buildOptions: IBuildCommandOptions = {};
|
||||
if (argvArg.cache === false) {
|
||||
buildOptions.noCache = true;
|
||||
}
|
||||
await manager.build(buildOptions);
|
||||
|
||||
// Run tests
|
||||
await manager.test();
|
||||
|
||||
Reference in New Issue
Block a user