Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): allow specifying Docker build targets #20654

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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