/
image-asset.ts
174 lines (148 loc) · 5.46 KB
/
image-asset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as fs from 'fs';
import * as path from 'path';
import * as assets from '@aws-cdk/assets';
import * as ecr from '@aws-cdk/aws-ecr';
import { Annotations, Construct as CoreConstruct, Stack, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as minimatch from 'minimatch';
/**
* Options for DockerImageAsset
*/
export interface DockerImageAssetOptions extends assets.FingerprintOptions {
/**
* ECR repository name
*
* Specify this property if you need to statically address the image, e.g.
* from a Kubernetes Pod. Note, this is only the repository name, without the
* registry and the tag parts.
*
* @default - the default ECR repository for CDK assets
* @deprecated to control the location of docker image assets, please override
* `Stack.addDockerImageAsset`. this feature will be removed in future
* releases.
*/
readonly repositoryName?: string;
/**
* Build args to pass to the `docker build` command.
*
* Since Docker build arguments are resolved before deployment, keys and
* values cannot refer to unresolved tokens (such as `lambda.functionArn` or
* `queue.queueUrl`).
*
* @default - no build args are passed
*/
readonly buildArgs?: { [key: string]: string };
/**
* Docker target to build to
*
* @default - no target
*/
readonly target?: string;
/**
* Path to the Dockerfile (relative to the directory).
*
* @default 'Dockerfile'
*/
readonly file?: string;
}
/**
* Props for DockerImageAssets
*/
export interface DockerImageAssetProps extends DockerImageAssetOptions {
/**
* The directory where the Dockerfile is stored
*/
readonly directory: string;
}
/**
* An asset that represents a Docker image.
*
* The image will be created in build time and uploaded to an ECR repository.
*/
export class DockerImageAsset extends CoreConstruct implements assets.IAsset {
/**
* The full URI of the image (including a tag). Use this reference to pull
* the asset.
*/
public imageUri: string;
/**
* Repository where the image is stored
*/
public repository: ecr.IRepository;
public readonly sourceHash: string;
constructor(scope: Construct, id: string, props: DockerImageAssetProps) {
super(scope, id);
// none of the properties use tokens
validateProps(props);
// resolve full path
const dir = path.resolve(props.directory);
if (!fs.existsSync(dir)) {
throw new Error(`Cannot find image directory at ${dir}`);
}
// validate the docker file exists
const file = path.join(dir, props.file || 'Dockerfile');
if (!fs.existsSync(file)) {
throw new Error(`Cannot find file at ${file}`);
}
let exclude: string[] = props.exclude || [];
const ignore = path.join(dir, '.dockerignore');
if (fs.existsSync(ignore)) {
exclude = [...exclude, ...fs.readFileSync(ignore).toString().split('\n').filter(e => !!e)];
}
// make sure the docker file and the dockerignore file end up in the staging area
// see https://github.com/aws/aws-cdk/issues/6004
exclude = exclude.filter(ignoreExpression => {
return !(minimatch(file, ignoreExpression, { matchBase: true }) ||
minimatch(ignore, ignoreExpression, { matchBase: true }));
});
if (props.repositoryName) {
Annotations.of(this).addWarning('DockerImageAsset.repositoryName is deprecated. Override "core.Stack.addDockerImageAsset" to control asset locations');
}
// include build context in "extra" so it will impact the hash
const extraHash: { [field: string]: any } = { };
if (props.extraHash) { extraHash.user = props.extraHash; }
if (props.buildArgs) { extraHash.buildArgs = props.buildArgs; }
if (props.target) { extraHash.target = props.target; }
if (props.file) { extraHash.file = props.file; }
if (props.repositoryName) { extraHash.repositoryName = props.repositoryName; }
// add "salt" to the hash in order to invalidate the image in the upgrade to
// 1.21.0 which removes the AdoptedRepository resource (and will cause the
// deletion of the ECR repository the app used).
extraHash.version = '1.21.0';
const staging = new assets.Staging(this, 'Staging', {
...props,
exclude,
sourcePath: dir,
extraHash: Object.keys(extraHash).length === 0
? undefined
: JSON.stringify(extraHash),
});
this.sourceHash = staging.sourceHash;
const stack = Stack.of(this);
const location = stack.synthesizer.addDockerImageAsset({
directoryName: staging.stagedPath,
dockerBuildArgs: props.buildArgs,
dockerBuildTarget: props.target,
dockerFile: props.file,
repositoryName: props.repositoryName,
sourceHash: staging.sourceHash,
});
this.repository = ecr.Repository.fromRepositoryName(this, 'Repository', location.repositoryName);
this.imageUri = location.imageUri;
}
}
function validateProps(props: DockerImageAssetProps) {
for (const [key, value] of Object.entries(props)) {
if (Token.isUnresolved(value)) {
throw new Error(`Cannot use Token as value of '${key}': this value is used before deployment starts`);
}
}
validateBuildArgs(props.buildArgs);
}
function validateBuildArgs(buildArgs?: { [key: string]: string }) {
for (const [key, value] of Object.entries(buildArgs || {})) {
if (Token.isUnresolved(key) || Token.isUnresolved(value)) {
throw new Error('Cannot use tokens in keys or values of "buildArgs" since they are needed before deployment');
}
}
}