Skip to content

Commit

Permalink
feat(core): allow specifying Docker build targets (#20654)
Browse files Browse the repository at this point in the history
Add support for passing `--target [...]` to docker build.
  • Loading branch information
indrora authored Jun 7, 2022
1 parent 23160e1 commit f243f9e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/@aws-cdk/core/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export class DockerImage extends BundlingDockerImage {
'build', '-t', tag,
...(options.file ? ['-f', join(path, options.file)] : []),
...(options.platform ? ['--platform', options.platform] : []),
...(options.targetStage ? ['--target', options.targetStage] : []),
...flatten(Object.entries(buildArgs).map(([k, v]) => ['--build-arg', `${k}=${v}`])),
path,
];
Expand Down Expand Up @@ -487,6 +488,15 @@ export interface DockerBuildOptions {
* @default - no platform specified
*/
readonly platform?: string;

/**
* Set build target for multi-stage container builds. Any stage defined afterwards will be ignored.
*
* Example value: `build-env`
*
* @default - Build all stages defined in the Dockerfile
*/
readonly targetStage?: string;
}

function flatten(x: string[][]) {
Expand Down
40 changes: 39 additions & 1 deletion packages/@aws-cdk/core/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('bundling', () => {
fingerprintStub.callsFake(() => imageHash);
const platform = 'linux/someArch99';

const image = DockerImage.fromBuild('docker-path', { platform });
const image = DockerImage.fromBuild('docker-path', { platform: platform });
image.run();

const tagHash = crypto.createHash('sha256').update(JSON.stringify({
Expand All @@ -125,6 +125,44 @@ describe('bundling', () => {

});

test('bundling with image from asset with target stage', () => {
const spawnSyncStub = sinon.stub(child_process, 'spawnSync').returns({
status: 0,
stderr: Buffer.from('stderr'),
stdout: Buffer.from('stdout'),
pid: 123,
output: ['stdout', 'stderr'],
signal: null,
});

const imageHash = '123456abcdef';
const fingerprintStub = sinon.stub(FileSystem, 'fingerprint');
fingerprintStub.callsFake(() => imageHash);
const targetStage = 'i-love-testing';

const image = DockerImage.fromBuild('docker-path', { targetStage: targetStage });
image.run();

const tagHash = crypto.createHash('sha256').update(JSON.stringify({
path: 'docker-path',
targetStage,
})).digest('hex');
const tag = `cdk-${tagHash}`;

expect(spawnSyncStub.firstCall.calledWith('docker', [
'build', '-t', tag,
'--target', targetStage,
'docker-path',
])).toEqual(true);

expect(spawnSyncStub.secondCall.calledWith('docker', [
'run', '--rm',
tag,
])).toEqual(true);

});


test('throws in case of spawnSync error', () => {
sinon.stub(child_process, 'spawnSync').returns({
status: 0,
Expand Down

0 comments on commit f243f9e

Please sign in to comment.