Skip to content

Commit 9b4db42

Browse files
authored
feat(assets): Add deploy-time content hash (#2334)
Introduces an `IAsset` interface that centralizes common aspects about assets, such as the `sourceHash` and `bundleHash` properties. The `sourceHash` fingerprints the objects that are used as the source for the asset bundling logic, and is available at synthesis time (it can for example be injected in construct IDs when it one wants to ensure a new logical ID is issued for every new version of the asset). The `bundleHash` fingerprints the result of the bundling logic, and is more accurate than `sourceHash` (in that, if the same source can produce different artifacts at different points in time, the `sourceHash` will remain un-changed, but the `bundleHash` will change. The `bundleHash` is however a deploy-time value and thus cannot be used in construct IDs. Fixes #1400
1 parent 28942d2 commit 9b4db42

File tree

64 files changed

+1382
-544
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1382
-544
lines changed

Diff for: .gitignore

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
*.tsbuildinfo
2-
.cdk.staging
3-
4-
.vscode
51
# VSCode extension
2+
.vscode/
63
/.favorites.json
4+
5+
# TypeScript incremental build states
6+
*.tsbuildinfo
7+
8+
# Local state files & OS specifics
79
.DS_Store
8-
node_modules
10+
node_modules/
911
lerna-debug.log
10-
dist
11-
pack
12+
dist/
13+
pack/
1214
.BUILD_COMPLETED
13-
.local-npm
14-
.tools
15-
coverage
15+
.local-npm/
16+
.tools/
17+
coverage/
1618
.nyc_output
1719
.LAST_BUILD
1820
*.sw[a-z]
1921
*~
2022

21-
# we don't want tsconfig at the root
23+
# We don't want tsconfig at the root
2224
/tsconfig.json
25+
26+
# CDK Context & Staging files
2327
cdk.context.json
24-
tsconfig.tsbuildinfo
28+
.cdk.staging/

Diff for: packages/@aws-cdk/assets-docker/lib/adopt-repository/handler.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ exports.handler = async function(event, context, _callback, respond) {
2626
}
2727
}
2828

29-
const repo = event.ResourceProperties.RepositoryName;
29+
let repo = event.ResourceProperties.RepositoryName;
3030
if (!repo) {
3131
throw new Error('Missing required property "RepositoryName"');
3232
}
33+
const isRepoUri = repo.match(/^(\d+\.dkr\.ecr\.[^.]+\.[^/]+\/)(.+)$/i);
34+
if (isRepoUri) {
35+
repo = isRepoUri[2];
36+
}
3337

3438
const adopter = await getAdopter(repo);
3539
if (event.RequestType === 'Delete') {

Diff for: packages/@aws-cdk/assets-docker/lib/adopted-repository.ts

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export class AdoptedRepository extends ecr.RepositoryBase {
5959
PolicyDocument: this.policyDocument
6060
}
6161
});
62+
if (fn.role) {
63+
// Need to explicitly depend on the role's policies, so they are applied before we try to use them
64+
adopter.node.addDependency(fn.role);
65+
}
6266

6367
// we use the Fn::GetAtt with the RepositoryName returned by the custom
6468
// resource in order to implicitly create a dependency between consumers

Diff for: packages/@aws-cdk/assets-docker/lib/image-asset.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs = require('fs');
66
import path = require('path');
77
import { AdoptedRepository } from './adopted-repository';
88

9-
export interface DockerImageAssetProps {
9+
export interface DockerImageAssetProps extends assets.CopyOptions {
1010
/**
1111
* The directory where the Dockerfile is stored
1212
*/
@@ -36,7 +36,7 @@ export interface DockerImageAssetProps {
3636
*
3737
* The image will be created in build time and uploaded to an ECR repository.
3838
*/
39-
export class DockerImageAsset extends cdk.Construct {
39+
export class DockerImageAsset extends cdk.Construct implements assets.IAsset {
4040
/**
4141
* The full URI of the image (including a tag). Use this reference to pull
4242
* the asset.
@@ -48,6 +48,9 @@ export class DockerImageAsset extends cdk.Construct {
4848
*/
4949
public repository: ecr.IRepository;
5050

51+
public readonly sourceHash: string;
52+
public readonly artifactHash: string;
53+
5154
/**
5255
* Directory where the source files are stored
5356
*/
@@ -66,31 +69,35 @@ export class DockerImageAsset extends cdk.Construct {
6669
}
6770

6871
const staging = new assets.Staging(this, 'Staging', {
72+
...props,
6973
sourcePath: dir
7074
});
7175

7276
this.directory = staging.stagedPath;
77+
this.sourceHash = staging.sourceHash;
7378

7479
const imageNameParameter = new cdk.CfnParameter(this, 'ImageName', {
7580
type: 'String',
7681
description: `ECR repository name and tag asset "${this.node.path}"`,
7782
});
7883

7984
const asset: cxapi.ContainerImageAssetMetadataEntry = {
85+
id: this.node.uniqueId,
8086
packaging: 'container-image',
8187
path: this.directory,
82-
id: this.node.uniqueId,
88+
sourceHash: this.sourceHash,
8389
imageNameParameter: imageNameParameter.logicalId,
8490
repositoryName: props.repositoryName,
8591
buildArgs: props.buildArgs
8692
};
8793

8894
this.node.addMetadata(cxapi.ASSET_METADATA, asset);
8995

90-
// parse repository name and tag from the parameter (<REPO_NAME>:<TAG>)
91-
const components = cdk.Fn.split(':', imageNameParameter.stringValue);
96+
// Parse repository name and tag from the parameter (<REPO_NAME>@sha256:<TAG>)
97+
// Example: cdk/cdkexampleimageb2d7f504@sha256:72c4f956379a43b5623d529ddd969f6826dde944d6221f445ff3e7add9875500
98+
const components = cdk.Fn.split('@sha256:', imageNameParameter.stringValue);
9299
const repositoryName = cdk.Fn.select(0, components).toString();
93-
const imageTag = cdk.Fn.select(1, components).toString();
100+
const imageSha = cdk.Fn.select(1, components).toString();
94101

95102
// Require that repository adoption happens first, so we route the
96103
// input ARN into the Custom Resource and then get the URI which we use to
@@ -99,6 +106,7 @@ export class DockerImageAsset extends cdk.Construct {
99106
// If adoption fails (because the repository might be twice-adopted), we
100107
// haven't already started using the image.
101108
this.repository = new AdoptedRepository(this, 'AdoptRepository', { repositoryName });
102-
this.imageUri = this.repository.repositoryUriForTag(imageTag);
109+
this.imageUri = `${this.repository.repositoryUri}@sha256:${imageSha}`;
110+
this.artifactHash = imageSha;
103111
}
104112
}

0 commit comments

Comments
 (0)