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 user to specify --platform #26368

Merged
merged 4 commits into from
Jul 14, 2023
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
21 changes: 21 additions & 0 deletions packages/aws-cdk-lib/core/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ export interface BundlingOptions {
* @default - BundlingFileAccess.BIND_MOUNT
*/
readonly bundlingFileAccess?: BundlingFileAccess;

/**
* Platform to build for. _Requires Docker Buildx_.
*
* Specify this property to build images on a specific platform.
*
* @default - no platform specified (the current machine architecture will be used)
*/
readonly platform?: string;
}

/**
Expand Down Expand Up @@ -256,6 +265,9 @@ export class BundlingDockerImage {
...options.network
? ['--network', options.network]
: [],
...options.platform
? ['--platform', options.platform]
: [],
...options.user
? ['-u', options.user]
: [],
Expand Down Expand Up @@ -535,6 +547,15 @@ export interface DockerRunOptions {
* @default - no networking options
*/
readonly network?: string;

/**
* Set platform if server is multi-platform capable. _Requires Docker Engine API v1.38+_.
*
* Example value: `linux/amd64`
*
* @default - no platform specified
*/
readonly platform?: string;
}

/**
Expand Down
33 changes: 33 additions & 0 deletions packages/aws-cdk-lib/core/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,39 @@ describe('bundling', () => {
], { encoding: 'utf-8', stdio: ['ignore', process.stderr, 'inherit'] })).toEqual(true);
});

test('adding user provided platform', () => {
// GIVEN
sinon.stub(process, 'platform').value('darwin');
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 image = DockerImage.fromRegistry('alpine');

// GIVEN
image.run({
command: ['cool', 'command'],
platform: 'linux/amd64',
volumes: [{ hostPath: '/host-path', containerPath: '/container-path' }],
workingDirectory: '/working-directory',
user: 'user:group',
});

expect(spawnSyncStub.calledWith(dockerCmd, [
'run', '--rm',
'--platform', 'linux/amd64',
'-u', 'user:group',
'-v', '/host-path:/container-path:delegated',
'-w', '/working-directory',
'alpine',
'cool', 'command',
], { encoding: 'utf-8', stdio: ['ignore', process.stderr, 'inherit'] })).toEqual(true);
});

test('adding user provided docker volume options', () => {
// GIVEN
sinon.stub(process, 'platform').value('darwin');
Expand Down