feat(docker): add support for no-cache builds and tag built images for local dependency resolution

This commit is contained in:
2026-02-06 13:25:21 +00:00
parent 32eb0d1d77
commit c09bef33c3
6 changed files with 42 additions and 7 deletions

View File

@@ -1,5 +1,14 @@
# Changelog # Changelog
## 2026-02-06 - 1.6.0 - feat(docker)
add support for no-cache builds and tag built images for local dependency resolution
- Introduce IBuildCommandOptions.noCache to control --no-cache behavior
- Propagate noCache from CLI (via cache flag) through TsDockerManager to Dockerfile.build
- Append --no-cache to docker build/buildx commands when noCache is true
- After building an image, tag it with full base image references used by dependent Dockerfiles so their FROM lines resolve to the locally-built image
- Log tagging actions and execute docker tag via smartshellInstance
## 2026-02-06 - 1.5.0 - feat(build) ## 2026-02-06 - 1.5.0 - feat(build)
add support for selective builds, platform override and build timeout add support for selective builds, platform override and build timeout

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@git.zone/tsdocker', name: '@git.zone/tsdocker',
version: '1.5.0', version: '1.6.0',
description: 'develop npm modules cross platform with docker' description: 'develop npm modules cross platform with docker'
} }

View File

@@ -138,10 +138,23 @@ export class Dockerfile {
*/ */
public static async buildDockerfiles( public static async buildDockerfiles(
sortedArrayArg: Dockerfile[], sortedArrayArg: Dockerfile[],
options?: { platform?: string; timeout?: number }, options?: { platform?: string; timeout?: number; noCache?: boolean },
): Promise<Dockerfile[]> { ): Promise<Dockerfile[]> {
for (const dockerfileArg of sortedArrayArg) { for (const dockerfileArg of sortedArrayArg) {
await dockerfileArg.build(options); 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; return sortedArrayArg;
} }
@@ -365,22 +378,23 @@ export class Dockerfile {
/** /**
* Builds the 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); logger.log('info', 'now building Dockerfile for ' + this.cleanTag);
const buildArgsString = await Dockerfile.getDockerBuildArgs(this.managerRef); const buildArgsString = await Dockerfile.getDockerBuildArgs(this.managerRef);
const config = this.managerRef.config; const config = this.managerRef.config;
const platformOverride = options?.platform; const platformOverride = options?.platform;
const timeout = options?.timeout; const timeout = options?.timeout;
const noCacheFlag = options?.noCache ? ' --no-cache' : '';
let buildCommand: string; let buildCommand: string;
if (platformOverride) { if (platformOverride) {
// Single platform override via buildx // 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) { } else if (config.platforms && config.platforms.length > 1) {
// Multi-platform build using buildx // Multi-platform build using buildx
const platformString = config.platforms.join(','); 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) { if (config.push) {
buildCommand += ' --push'; buildCommand += ' --push';
@@ -390,7 +404,7 @@ export class Dockerfile {
} else { } else {
// Standard build // Standard build
const versionLabel = this.managerRef.projectInfo?.npm?.version || 'unknown'; 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) { if (timeout) {

View File

@@ -139,6 +139,7 @@ export class TsDockerManager {
await Dockerfile.buildDockerfiles(toBuild, { await Dockerfile.buildDockerfiles(toBuild, {
platform: options?.platform, platform: options?.platform,
timeout: options?.timeout, timeout: options?.timeout,
noCache: options?.noCache,
}); });
logger.log('success', 'All Dockerfiles built successfully'); logger.log('success', 'All Dockerfiles built successfully');

View File

@@ -76,4 +76,5 @@ export interface IBuildCommandOptions {
patterns?: string[]; // Dockerfile name patterns (e.g., ['Dockerfile_base', 'Dockerfile_*']) patterns?: string[]; // Dockerfile name patterns (e.g., ['Dockerfile_base', 'Dockerfile_*'])
platform?: string; // Single platform override (e.g., 'linux/arm64') platform?: string; // Single platform override (e.g., 'linux/arm64')
timeout?: number; // Build timeout in seconds timeout?: number; // Build timeout in seconds
noCache?: boolean; // Force rebuild without Docker layer cache (--no-cache)
} }

View File

@@ -44,6 +44,9 @@ export let run = () => {
if (argvArg.timeout) { if (argvArg.timeout) {
buildOptions.timeout = Number(argvArg.timeout); buildOptions.timeout = Number(argvArg.timeout);
} }
if (argvArg.cache === false) {
buildOptions.noCache = true;
}
await manager.build(buildOptions); await manager.build(buildOptions);
logger.log('success', 'Build completed successfully'); logger.log('success', 'Build completed successfully');
@@ -78,6 +81,9 @@ export let run = () => {
if (argvArg.timeout) { if (argvArg.timeout) {
buildOptions.timeout = Number(argvArg.timeout); buildOptions.timeout = Number(argvArg.timeout);
} }
if (argvArg.cache === false) {
buildOptions.noCache = true;
}
// Build images first (if not already built) // Build images first (if not already built)
await manager.build(buildOptions); await manager.build(buildOptions);
@@ -130,7 +136,11 @@ export let run = () => {
await manager.prepare(); await manager.prepare();
// Build images first // Build images first
await manager.build(); const buildOptions: IBuildCommandOptions = {};
if (argvArg.cache === false) {
buildOptions.noCache = true;
}
await manager.build(buildOptions);
// Run tests // Run tests
await manager.test(); await manager.test();