-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcodebuild-step.ts
336 lines (299 loc) · 9.56 KB
/
codebuild-step.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { mergeBuildSpecs } from './private/buildspecs';
import { makeCodePipelineOutput } from './private/outputs';
import * as codebuild from '../../../aws-codebuild';
import * as ec2 from '../../../aws-ec2';
import * as iam from '../../../aws-iam';
import { Duration, UnscopedValidationError } from '../../../core';
import { ShellStep, ShellStepProps } from '../blueprint';
/**
* Construction props for a CodeBuildStep
*/
export interface CodeBuildStepProps extends ShellStepProps {
/**
* Name for the generated CodeBuild project
*
* @default - Automatically generated
*/
readonly projectName?: string;
/**
* Additional configuration that can only be configured via BuildSpec
*
* You should not use this to specify output artifacts; those
* should be supplied via the other properties of this class, otherwise
* CDK Pipelines won't be able to inspect the artifacts.
*
* Set the `commands` to an empty array if you want to fully specify
* the BuildSpec using this field.
*
* The BuildSpec must be available inline--it cannot reference a file
* on disk.
*
* @default - BuildSpec completely derived from other properties
*/
readonly partialBuildSpec?: codebuild.BuildSpec;
/**
* The VPC where to execute the SimpleSynth.
*
* @default - No VPC
*/
readonly vpc?: ec2.IVpc;
/**
* Which subnets to use.
*
* Only used if 'vpc' is supplied.
*
* @default - All private subnets.
*/
readonly subnetSelection?: ec2.SubnetSelection;
/**
* Caching strategy to use.
*
* @default - No cache
*/
readonly cache?: codebuild.Cache;
/**
* Policy statements to add to role used during the synth
*
* Can be used to add acces to a CodeArtifact repository etc.
*
* @default - No policy statements added to CodeBuild Project Role
*/
readonly rolePolicyStatements?: iam.PolicyStatement[];
/**
* Custom execution role to be used for the CodeBuild project
*
* @default - A role is automatically created
*/
readonly role?: iam.IRole;
/**
* Custom execution role to be used for the Code Build Action
*
* @default - A role is automatically created
*/
readonly actionRole?: iam.IRole;
/**
* Changes to environment
*
* This environment will be combined with the pipeline's default
* environment.
*
* @default - Use the pipeline's default build environment
*/
readonly buildEnvironment?: codebuild.BuildEnvironment;
/**
* Which security group to associate with the script's project network interfaces.
* If no security group is identified, one will be created automatically.
*
* Only used if 'vpc' is supplied.
*
* @default - Security group will be automatically created.
*/
readonly securityGroups?: ec2.ISecurityGroup[];
/**
* The number of minutes after which AWS CodeBuild stops the build if it's
* not complete. For valid values, see the timeoutInMinutes field in the AWS
* CodeBuild User Guide.
*
* @default Duration.hours(1)
*/
readonly timeout?: Duration;
/**
* ProjectFileSystemLocation objects for CodeBuild build projects.
*
* A ProjectFileSystemLocation object specifies the identifier, location, mountOptions, mountPoint,
* and type of a file system created using Amazon Elastic File System.
*
* @default - no file system locations
*/
readonly fileSystemLocations?: codebuild.IFileSystemLocation[];
/**
* Information about logs for CodeBuild projects. A CodeBuild project can create logs in Amazon CloudWatch Logs, an S3 bucket, or both.
*
* @default - no log configuration is set
*/
readonly logging?: codebuild.LoggingOptions;
}
/**
* Run a script as a CodeBuild Project
*
* The BuildSpec must be available inline--it cannot reference a file
* on disk. If your current build instructions are in a file like
* `buildspec.yml` in your repository, extract them to a script
* (say, `build.sh`) and invoke that script as part of the build:
*
* ```ts
* new pipelines.CodeBuildStep('Synth', {
* commands: ['./build.sh'],
* });
* ```
*/
export class CodeBuildStep extends ShellStep {
/**
* Name for the generated CodeBuild project
*
* @default - No value specified at construction time, use defaults
*/
public readonly projectName?: string;
/**
* The VPC where to execute the SimpleSynth.
*
* @default - No value specified at construction time, use defaults
*/
public readonly vpc?: ec2.IVpc;
/**
* Which subnets to use.
*
* @default - No value specified at construction time, use defaults
*/
public readonly subnetSelection?: ec2.SubnetSelection;
/**
* Caching strategy to use.
*
* @default - No cache
*/
public readonly cache?: codebuild.Cache;
/**
* Policy statements to add to role used during the synth
*
* @default - No value specified at construction time, use defaults
*/
public readonly rolePolicyStatements?: iam.PolicyStatement[];
/**
* Custom execution role to be used for the CodeBuild project
*
* @default - No value specified at construction time, use defaults
*/
public readonly role?: iam.IRole;
/**
* Custom execution role to be used for the Code Build Action
*
* @default - A role is automatically created
*/
readonly actionRole?: iam.IRole;
/**
* Build environment
*
* @default - No value specified at construction time, use defaults
*/
readonly buildEnvironment?: codebuild.BuildEnvironment;
/**
* Which security group to associate with the script's project network interfaces.
*
* @default - No value specified at construction time, use defaults
*/
readonly securityGroups?: ec2.ISecurityGroup[];
/**
* The number of minutes after which AWS CodeBuild stops the build if it's
* not complete. For valid values, see the timeoutInMinutes field in the AWS
* CodeBuild User Guide.
*
* @default Duration.hours(1)
*/
readonly timeout?: Duration;
/**
* ProjectFileSystemLocation objects for CodeBuild build projects.
*
* A ProjectFileSystemLocation object specifies the identifier, location, mountOptions, mountPoint,
* and type of a file system created using Amazon Elastic File System.
*
* @default - no file system locations
*/
readonly fileSystemLocations?: codebuild.IFileSystemLocation[];
/**
* Information about logs for CodeBuild projects. A CodeBuilde project can create logs in Amazon CloudWatch Logs, an S3 bucket, or both.
*
* @default - no log configuration is set
*/
readonly logging?: codebuild.LoggingOptions;
private _project?: codebuild.IProject;
private _partialBuildSpec?: codebuild.BuildSpec;
private readonly exportedVariables = new Set<string>();
private exportedVarsRendered = false;
constructor(id: string, props: CodeBuildStepProps) {
super(id, props);
this.projectName = props.projectName;
this.buildEnvironment = props.buildEnvironment;
this._partialBuildSpec = props.partialBuildSpec;
this.vpc = props.vpc;
this.subnetSelection = props.subnetSelection;
this.cache = props.cache;
this.role = props.role;
this.actionRole = props.actionRole;
this.rolePolicyStatements = props.rolePolicyStatements;
this.securityGroups = props.securityGroups;
this.timeout = props.timeout;
this.fileSystemLocations = props.fileSystemLocations;
this.logging = props.logging;
}
/**
* CodeBuild Project generated for the pipeline
*
* Will only be available after the pipeline has been built.
*/
public get project(): codebuild.IProject {
if (!this._project) {
throw new UnscopedValidationError('Call pipeline.buildPipeline() before reading this property');
}
return this._project;
}
/**
* The CodeBuild Project's principal
*/
public get grantPrincipal(): iam.IPrincipal {
return this.project.grantPrincipal;
}
/**
* Additional configuration that can only be configured via BuildSpec
*
* Contains exported variables
*
* @default - Contains the exported variables
*/
public get partialBuildSpec(): codebuild.BuildSpec | undefined {
this.exportedVarsRendered = true;
const varsBuildSpec = this.exportedVariables.size > 0 ? codebuild.BuildSpec.fromObject({
version: '0.2',
env: {
'exported-variables': Array.from(this.exportedVariables),
},
}) : undefined;
return mergeBuildSpecs(varsBuildSpec, this._partialBuildSpec);
}
/**
* Reference a CodePipeline variable defined by the CodeBuildStep.
*
* The variable must be set in the shell of the CodeBuild step when
* it finishes its `post_build` phase.
*
* @param variableName the name of the variable for reference.
* @example
* // Access the output of one CodeBuildStep in another CodeBuildStep
* declare const pipeline: pipelines.CodePipeline;
*
* const step1 = new pipelines.CodeBuildStep('Step1', {
* commands: ['export MY_VAR=hello'],
* });
*
* const step2 = new pipelines.CodeBuildStep('Step2', {
* env: {
* IMPORTED_VAR: step1.exportedVariable('MY_VAR'),
* },
* commands: ['echo $IMPORTED_VAR'],
* });
*/
public exportedVariable(variableName: string): string {
if (this.exportedVarsRendered && !this.exportedVariables.has(variableName)) {
throw new UnscopedValidationError('exportVariable(): Pipeline has already been produced, cannot call this function anymore');
}
this.exportedVariables.add(variableName);
return makeCodePipelineOutput(this, variableName);
}
/**
* Set the internal project value
*
* @internal
*/
public _setProject(project: codebuild.IProject) {
this._project = project;
}
}