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): support ssh build arg in DockerImageAsset #26356

Merged
merged 30 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e6c7fdf
add arg
Jul 12, 2023
8209409
Merge branch 'aws:main' into docker-build-ssh
JackWBoynton Jul 13, 2023
9cefcf2
Merge branch 'aws:main' into docker-build-ssh
JackWBoynton Jul 13, 2023
c6f2625
Merge branch 'main' into docker-build-ssh
JackWBoynton Jul 13, 2023
ee04041
readme
Jul 13, 2023
b8db334
Merge branch 'aws:main' into docker-build-ssh
JackWBoynton Jul 15, 2023
9146347
Merge branch 'aws:main' into docker-build-ssh
JackWBoynton Jul 16, 2023
7555746
lint
Jul 16, 2023
6fcb467
reset changes
Jul 16, 2023
5997838
update schema
Jul 16, 2023
acfdb68
clean
Jul 16, 2023
b54b2a8
update has
Jul 16, 2023
3c620e7
fix docker arg
Jul 16, 2023
a662e1f
space
Jul 16, 2023
d0820c8
fix example import string literal
Jul 16, 2023
d6545f7
rename
Jul 21, 2023
bd21e71
missed a few renames
Jul 22, 2023
c079d0c
new hash
Jul 22, 2023
e1e1967
get integ tests to pass
Jul 22, 2023
ec206b1
Merge branch 'main' into docker-build-ssh
JackWBoynton Jul 22, 2023
f4dff9f
add integ test for ssh flag
Jul 22, 2023
65a4511
add separate image for testing ssh arg
Jul 22, 2023
7ad9234
add separate image for testing ssh arg
Jul 22, 2023
b8264ca
fix integ test
Jul 22, 2023
0ab7246
Merge branch 'main' into docker-build-ssh
JackWBoynton Jul 26, 2023
6a7f399
Merge branch 'main' into docker-build-ssh
mrgrain Jul 26, 2023
e5aea42
Merge branch 'main' into docker-build-ssh
mrgrain Aug 1, 2023
5dad6cd
Merge branch 'main' into docker-build-ssh
mergify[bot] Aug 2, 2023
bd07d78
revert FEATURE_FLAGS.md
JackWBoynton Aug 4, 2023
ac6887b
Merge branch 'main' into docker-build-ssh
JackWBoynton Aug 4, 2023
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
32 changes: 32 additions & 0 deletions packages/aws-cdk-lib/aws-ecr-assets/lib/image-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export interface DockerImageAssetInvalidationOptions {
*/
readonly buildSecrets?: boolean;

/**
* Use `buildSSH` while calculating the asset hash
*
* @default true
*/
readonly buildSSH?: boolean;

/**
* Use `target` while calculating the asset hash
*
Expand Down Expand Up @@ -223,6 +230,23 @@ export interface DockerImageAssetOptions extends FingerprintOptions, FileFingerp
*/
readonly buildSecrets?: { [key: string]: string }

/**
* SSH agent socket or keys to pass to the `docker build` command.
*
* Docker BuildKit must be enabled to use the ssh flag
*
* @see https://docs.docker.com/build/buildkit/
*
* @default - no --ssh flag
*
* @example
* import { DockerBuildSSH } from `aws-cdk-lib`;
*
* const sshFlag = 'default';
*
*/
readonly buildSSH?: string;

/**
* Docker target to build to
*
Expand Down Expand Up @@ -364,6 +388,10 @@ export class DockerImageAsset extends Construct implements IAsset {
*/
private readonly dockerBuildSecrets?: { [key: string]: string };

/**
* SSH agent socket or keys to pass to the `docker build` command.
*/
private readonly dockerBuildSSH?: string;
/**
* Outputs to pass to the `docker build` command.
*/
Expand Down Expand Up @@ -446,6 +474,7 @@ export class DockerImageAsset extends Construct implements IAsset {
if (props.invalidation?.extraHash !== false && props.extraHash) { extraHash.user = props.extraHash; }
if (props.invalidation?.buildArgs !== false && props.buildArgs) { extraHash.buildArgs = props.buildArgs; }
if (props.invalidation?.buildSecrets !== false && props.buildSecrets) { extraHash.buildSecrets = props.buildSecrets; }
if (props.invalidation?.buildSSH !== false && props.buildSSH) {extraHash.buildSSH = props.buildSSH; }
if (props.invalidation?.target !== false && props.target) { extraHash.target = props.target; }
if (props.invalidation?.file !== false && props.file) { extraHash.file = props.file; }
if (props.invalidation?.repositoryName !== false && props.repositoryName) { extraHash.repositoryName = props.repositoryName; }
Expand Down Expand Up @@ -477,6 +506,7 @@ export class DockerImageAsset extends Construct implements IAsset {
this.assetName = props.assetName;
this.dockerBuildArgs = props.buildArgs;
this.dockerBuildSecrets = props.buildSecrets;
this.dockerBuildSSH = props.buildSSH;
this.dockerBuildTarget = props.target;
this.dockerOutputs = props.outputs;
this.dockerCacheFrom = props.cacheFrom;
Expand All @@ -487,6 +517,7 @@ export class DockerImageAsset extends Construct implements IAsset {
assetName: this.assetName,
dockerBuildArgs: this.dockerBuildArgs,
dockerBuildSecrets: this.dockerBuildSecrets,
dockerBuildSSH: this.dockerBuildSSH,
dockerBuildTarget: this.dockerBuildTarget,
dockerFile: props.file,
sourceHash: staging.assetHash,
Expand Down Expand Up @@ -530,6 +561,7 @@ export class DockerImageAsset extends Construct implements IAsset {
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY] = this.dockerfilePath;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_ARGS_KEY] = this.dockerBuildArgs;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_SECRETS_KEY] = this.dockerBuildSecrets;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_SSH_KEY] = this.dockerBuildSSH;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_TARGET_KEY] = this.dockerBuildTarget;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY] = resourceProperty;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_OUTPUTS_KEY] = this.dockerOutputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ describe('image asset', () => {
const asset6 = new DockerImageAsset(stack, 'Asset6', { directory, extraHash: 'random-extra' });
const asset7 = new DockerImageAsset(stack, 'Asset7', { directory, outputs: ['123'] });
const asset8 = new DockerImageAsset(stack, 'Asset8', { directory, buildSecrets: { mySecret: DockerBuildSecret.fromSrc('abc.txt') } });
const asset9 = new DockerImageAsset(stack, 'Asset9', { directory, buildSSH: 'default'});

expect(asset1.assetHash).toEqual('13248c55633f3b198a628bb2ea4663cb5226f8b2801051bd0c725950266fd590');
expect(asset2.assetHash).toEqual('36bf205fb9adc5e45ba1c8d534158a0aed96d190eff433af1d90f3b94f96e751');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export interface DockerImageSource {
*/
readonly dockerBuildArgs?: { [name: string]: string };

/**
* SSH agent socket or keys
*
* Requires building with docker buildkit.
*
* @default - No ssh flag is set
*/
readonly dockerBuildSSH?: string;
mrgrain marked this conversation as resolved.
Show resolved Hide resolved

/**
* Additional build secrets
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ export interface ContainerImageAssetMetadataEntry extends BaseAssetMetadataEntry
*/
readonly buildArgs?: { [key: string]: string };

/**
* SSH agent socket or keys to pass to the `docker build` command
*
* @default no ssh arg is passed
*/
readonly buildSSH?: string;

/**
* Build secrets to pass to the `docker build` command
*
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/core/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ export interface DockerImageAssetSource {
*/
readonly dockerBuildSecrets?: { [key: string]: string };

/**
* SSH agent socket or keys to pass to the `docker buildx` command.
*
*
* @default - no ssh arg is passed
*/
readonly dockerBuildSSH?: string;

/**
* Docker target to build to
*
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk-lib/cx-api/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const ASSET_RESOURCE_METADATA_PATH_KEY = 'aws:asset:path';
export const ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY = 'aws:asset:dockerfile-path';
export const ASSET_RESOURCE_METADATA_DOCKER_BUILD_ARGS_KEY = 'aws:asset:docker-build-args';
export const ASSET_RESOURCE_METADATA_DOCKER_BUILD_SECRETS_KEY = 'aws:asset:docker-build-secrets';
export const ASSET_RESOURCE_METADATA_DOCKER_BUILD_SSH_KEY = 'aws:asset:docker-build-ssh';
export const ASSET_RESOURCE_METADATA_DOCKER_BUILD_TARGET_KEY = 'aws:asset:docker-build-target';
export const ASSET_RESOURCE_METADATA_PROPERTY_KEY = 'aws:asset:property';
export const ASSET_RESOURCE_METADATA_IS_BUNDLED_KEY = 'aws:asset:is-bundled';
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ async function prepareDockerImageAsset(
assetManifest.addDockerImageAsset(asset.sourceHash, {
directory: asset.path,
dockerBuildArgs: asset.buildArgs,
dockerBuildSSH: asset.buildSSH,
dockerBuildTarget: asset.target,
dockerFile: asset.file,
networkMode: asset.networkMode,
Expand Down
2 changes: 2 additions & 0 deletions packages/cdk-assets/lib/private/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface BuildOptions {
readonly file?: string;
readonly buildArgs?: Record<string, string>;
readonly buildSecrets?: Record<string, string>;
readonly buildSSH?: string;
readonly networkMode?: string;
readonly platform?: string;
readonly outputs?: string[];
Expand Down Expand Up @@ -91,6 +92,7 @@ export class Docker {
'build',
...flatten(Object.entries(options.buildArgs || {}).map(([k, v]) => ['--build-arg', `${k}=${v}`])),
...flatten(Object.entries(options.buildSecrets || {}).map(([k, v]) => ['--secret', `id=${k},${v}`])),
'--ssh', options.buildSSH,
'--tag', options.tag,
...options.target ? ['--target', options.target] : [],
...options.file ? ['--file', options.file] : [],
Expand Down