Skip to content

Commit b12bc67

Browse files
authored
fix(cli): specifying --quiet does not suppress asset building and publishing logs (#26493)
> We're currently maintaining a patched version of CDK to use with [SST](https://docs.sst.dev/) because this flag isn't fully respected. When the quiet flag is specified, the docker build and docker push shell commands still print to the terminal. This PR propagates the quiet flag down to the cdk-assets's container-images handler. Replaces #26265 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c562075 commit b12bc67

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

packages/aws-cdk/lib/util/asset-publishing.ts

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export async function publishAssets(
5454
publishInParallel: options.parallel ?? true,
5555
buildAssets: options.buildAssets ?? true,
5656
publishAssets: true,
57+
quiet: options.quiet,
5758
});
5859
await publisher.publish();
5960
if (publisher.hasFailures) {

packages/cdk-assets/lib/private/asset-handler.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ export interface IHandlerHost {
2828
readonly dockerFactory: DockerFactory;
2929

3030
emitMessage(type: EventType, m: string): void;
31-
}
31+
}
32+
33+
export interface IHandlerOptions {
34+
/**
35+
* Suppress all output
36+
*/
37+
readonly quiet?: boolean;
38+
}

packages/cdk-assets/lib/private/docker.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ interface BuildOptions {
2121
readonly outputs?: string[];
2222
readonly cacheFrom?: DockerCacheOption[];
2323
readonly cacheTo?: DockerCacheOption;
24+
readonly quiet?: boolean;
25+
}
26+
27+
interface PushOptions {
28+
readonly tag: string;
29+
readonly quiet?: boolean;
2430
}
2531

2632
export interface DockerCredentialsConfig {
@@ -101,7 +107,10 @@ export class Docker {
101107
...options.cacheTo ? ['--cache-to', this.cacheOptionToFlag(options.cacheTo)] : [],
102108
'.',
103109
];
104-
await this.execute(buildCommand, { cwd: options.directory });
110+
await this.execute(buildCommand, {
111+
cwd: options.directory,
112+
quiet: options.quiet,
113+
});
105114
}
106115

107116
/**
@@ -128,8 +137,8 @@ export class Docker {
128137
await this.execute(['tag', sourceTag, targetTag]);
129138
}
130139

131-
public async push(tag: string) {
132-
await this.execute(['push', tag]);
140+
public async push(options: PushOptions) {
141+
await this.execute(['push', options.tag], { quiet: options.quiet });
133142
}
134143

135144
/**

packages/cdk-assets/lib/private/handlers/container-images.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DockerImageDestination } from '@aws-cdk/cloud-assembly-schema';
33
import type * as AWS from 'aws-sdk';
44
import { DockerImageManifestEntry } from '../../asset-manifest';
55
import { EventType } from '../../progress';
6-
import { IAssetHandler, IHandlerHost } from '../asset-handler';
6+
import { IAssetHandler, IHandlerHost, IHandlerOptions } from '../asset-handler';
77
import { Docker } from '../docker';
88
import { replaceAwsPlaceholders } from '../placeholders';
99
import { shell } from '../shell';
@@ -21,7 +21,8 @@ export class ContainerImageAssetHandler implements IAssetHandler {
2121
constructor(
2222
private readonly workDir: string,
2323
private readonly asset: DockerImageManifestEntry,
24-
private readonly host: IHandlerHost) {
24+
private readonly host: IHandlerHost,
25+
private readonly options: IHandlerOptions) {
2526
}
2627

2728
public async build(): Promise<void> {
@@ -36,7 +37,9 @@ export class ContainerImageAssetHandler implements IAssetHandler {
3637
ecr: initOnce.ecr,
3738
});
3839

39-
const builder = new ContainerImageBuilder(dockerForBuilding, this.workDir, this.asset, this.host);
40+
const builder = new ContainerImageBuilder(dockerForBuilding, this.workDir, this.asset, this.host, {
41+
quiet: this.options.quiet,
42+
});
4043
const localTagName = await builder.build();
4144

4245
if (localTagName === undefined || this.host.aborted) { return; }
@@ -70,7 +73,7 @@ export class ContainerImageAssetHandler implements IAssetHandler {
7073
if (this.host.aborted) { return; }
7174

7275
this.host.emitMessage(EventType.UPLOAD, `Push ${initOnce.imageUri}`);
73-
await dockerForPushing.push(initOnce.imageUri);
76+
await dockerForPushing.push({ tag: initOnce.imageUri, quiet: this.options.quiet });
7477
}
7578

7679
private async initOnce(options: { quiet?: boolean } = {}): Promise<ContainerImageAssetHandlerInit> {
@@ -120,12 +123,17 @@ export class ContainerImageAssetHandler implements IAssetHandler {
120123
}
121124
}
122125

126+
interface ContainerImageBuilderOptions {
127+
readonly quiet?: boolean;
128+
}
129+
123130
class ContainerImageBuilder {
124131
constructor(
125132
private readonly docker: Docker,
126133
private readonly workDir: string,
127134
private readonly asset: DockerImageManifestEntry,
128-
private readonly host: IHandlerHost) {
135+
private readonly host: IHandlerHost,
136+
private readonly options: ContainerImageBuilderOptions) {
129137
}
130138

131139
async build(): Promise<string | undefined> {
@@ -188,6 +196,7 @@ class ContainerImageBuilder {
188196
outputs: source.dockerOutputs,
189197
cacheFrom: source.cacheFrom,
190198
cacheTo: source.cacheTo,
199+
quiet: this.options.quiet,
191200
});
192201
}
193202

packages/cdk-assets/lib/private/handlers/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { ContainerImageAssetHandler } from './container-images';
22
import { FileAssetHandler } from './files';
33
import { AssetManifest, DockerImageManifestEntry, FileManifestEntry, IManifestEntry } from '../../asset-manifest';
4-
import { IAssetHandler, IHandlerHost } from '../asset-handler';
4+
import { IAssetHandler, IHandlerHost, IHandlerOptions } from '../asset-handler';
55

6-
export function makeAssetHandler(manifest: AssetManifest, asset: IManifestEntry, host: IHandlerHost): IAssetHandler {
6+
export function makeAssetHandler(manifest: AssetManifest, asset: IManifestEntry, host: IHandlerHost, options: IHandlerOptions): IAssetHandler {
77
if (asset instanceof FileManifestEntry) {
88
return new FileAssetHandler(manifest.directory, asset, host);
99
}
1010
if (asset instanceof DockerImageManifestEntry) {
11-
return new ContainerImageAssetHandler(manifest.directory, asset, host);
11+
return new ContainerImageAssetHandler(manifest.directory, asset, host, options);
1212
}
1313

1414
throw new Error(`Unrecognized asset type: '${asset}'`);

packages/cdk-assets/lib/publishing.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export interface AssetPublishingOptions {
4545
* @default true
4646
*/
4747
readonly publishAssets?: boolean;
48+
49+
/**
50+
* Whether to print publishing logs
51+
*
52+
* @default true
53+
*/
54+
readonly quiet?: boolean;
4855
}
4956

5057
/**
@@ -240,7 +247,9 @@ export class AssetPublishing implements IPublishProgress {
240247
if (existing) {
241248
return existing;
242249
}
243-
const ret = makeAssetHandler(this.manifest, asset, this.handlerHost);
250+
const ret = makeAssetHandler(this.manifest, asset, this.handlerHost, {
251+
quiet: this.options.quiet,
252+
});
244253
this.handlerCache.set(asset, ret);
245254
return ret;
246255
}

0 commit comments

Comments
 (0)