Skip to content

Commit d92ed04

Browse files
committed
buildx(imagetools): add skip support and configurable create command silence
Signed-off-by: CrazyMax <[email protected]>
1 parent 09c0f6a commit d92ed04

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

__tests__/buildx/imagetools.test.itg.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,14 @@ maybe('attestationDigests', () => {
9494
expect(digests).toEqual(['sha256:0709528fae1747ce17638ad2978ee7936b38a294136eaadaf692e415f64b1e03']);
9595
});
9696
});
97+
98+
maybe('create', () => {
99+
it('skips create command execution when skipExec is set', async () => {
100+
const result = await new ImageTools().create({
101+
sources: ['sha256:0709528fae1747ce17638ad2978ee7936b38a294136eaadaf692e415f64b1e03'],
102+
tags: ['docker.io/user/app', 'docker.io/user/app2'],
103+
skipExec: true
104+
});
105+
expect(result).toBeUndefined();
106+
});
107+
});

__tests__/buildx/imagetools.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ describe('create', () => {
7070

7171
const result = await new ImageTools({buildx}).create({
7272
sources: ['cwd://descriptor.json', 'docker.io/library/alpine:latest'],
73-
tags: ['docker.io/user/app:latest']
73+
tags: ['docker.io/user/app:latest'],
74+
silent: true
7475
});
7576

7677
expect(getCommand).toHaveBeenCalledWith(['imagetools', 'create', '--tag', 'docker.io/user/app:latest', '--metadata-file', metadataFile, '--file', 'descriptor.json', 'docker.io/library/alpine:latest']);
@@ -104,7 +105,8 @@ describe('create', () => {
104105

105106
const result = await new ImageTools({buildx}).create({
106107
sources: ['docker.io/library/alpine:latest'],
107-
dryRun: true
108+
dryRun: true,
109+
silent: true
108110
});
109111

110112
expect(getCommand).toHaveBeenCalledWith(['imagetools', 'create', '--dry-run', 'docker.io/library/alpine:latest']);
@@ -114,4 +116,26 @@ describe('create', () => {
114116
});
115117
expect(result).toBeUndefined();
116118
});
119+
120+
it('skips command execution when skipExec is enabled', async () => {
121+
const getCommand = vi.fn().mockResolvedValue({
122+
command: 'docker',
123+
args: ['buildx', 'imagetools', 'create']
124+
});
125+
const buildx = {getCommand} as unknown as Buildx;
126+
const execSpy = vi.spyOn(Exec, 'getExecOutput').mockResolvedValue({
127+
exitCode: 0,
128+
stdout: '',
129+
stderr: ''
130+
});
131+
132+
const result = await new ImageTools({buildx}).create({
133+
sources: ['docker.io/library/alpine:latest'],
134+
skipExec: true
135+
});
136+
137+
expect(getCommand).toHaveBeenCalledWith(['imagetools', 'create', '--metadata-file', metadataFile, 'docker.io/library/alpine:latest']);
138+
expect(execSpy).not.toHaveBeenCalled();
139+
expect(result).toBeUndefined();
140+
});
117141
});

src/buildx/imagetools.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
import fs from 'fs';
18+
import * as core from '@actions/core';
19+
1820
import {Buildx} from './buildx.js';
1921
import {Context} from '../context.js';
2022
import {Exec} from '../exec.js';
@@ -164,9 +166,15 @@ export class ImageTools {
164166
}
165167

166168
const cmd = await this.getCreateCommand(args);
169+
if (opts.skipExec) {
170+
core.info(`[command]${cmd.command} ${cmd.args.join(' ')}`);
171+
core.info(`Skipped create command`);
172+
return undefined;
173+
}
174+
167175
return await Exec.getExecOutput(cmd.command, cmd.args, {
168176
ignoreReturnCode: true,
169-
silent: true
177+
silent: opts.silent
170178
}).then(res => {
171179
if (res.stderr.length > 0 && res.exitCode != 0) {
172180
throw new Error(res.stderr.trim());

src/types/buildx/imagetools.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export interface CreateOpts {
3333
tags?: Array<string>;
3434
platforms?: Array<string>;
3535
dryRun?: boolean;
36+
silent?: boolean;
37+
skipExec?: boolean;
3638
}
3739

3840
export interface CreateResponse {

0 commit comments

Comments
 (0)