1
- import { Duration } from '@aws-cdk/core' ;
2
1
import * as codebuild from '@aws-cdk/aws-codebuild' ;
3
2
import * as ec2 from '@aws-cdk/aws-ec2' ;
4
3
import * as iam from '@aws-cdk/aws-iam' ;
4
+ import { Duration } from '@aws-cdk/core' ;
5
5
import { ShellStep , ShellStepProps } from '../blueprint' ;
6
+ import { mergeBuildSpecs } from './private/buildspecs' ;
7
+ import { makeCodePipelineOutput } from './private/outputs' ;
6
8
7
9
/**
8
10
* Construction props for a CodeBuildStep
@@ -96,6 +98,17 @@ export interface CodeBuildStepProps extends ShellStepProps {
96
98
97
99
/**
98
100
* Run a script as a CodeBuild Project
101
+ *
102
+ * The BuildSpec must be available inline--it cannot reference a file
103
+ * on disk. If your current build instructions are in a file like
104
+ * `buildspec.yml` in your repository, extract them to a script
105
+ * (say, `build.sh`) and invoke that script as part of the build:
106
+ *
107
+ * ```ts
108
+ * new pipelines.CodeBuildStep('Synth', {
109
+ * commands: ['./build.sh'],
110
+ * });
111
+ * ```
99
112
*/
100
113
export class CodeBuildStep extends ShellStep {
101
114
/**
@@ -105,13 +118,6 @@ export class CodeBuildStep extends ShellStep {
105
118
*/
106
119
public readonly projectName ?: string ;
107
120
108
- /**
109
- * Additional configuration that can only be configured via BuildSpec
110
- *
111
- * @default - No value specified at construction time, use defaults
112
- */
113
- public readonly partialBuildSpec ?: codebuild . BuildSpec ;
114
-
115
121
/**
116
122
* The VPC where to execute the SimpleSynth.
117
123
*
@@ -164,13 +170,16 @@ export class CodeBuildStep extends ShellStep {
164
170
readonly timeout ?: Duration ;
165
171
166
172
private _project ?: codebuild . IProject ;
173
+ private _partialBuildSpec ?: codebuild . BuildSpec ;
174
+ private readonly exportedVariables = new Set < string > ( ) ;
175
+ private exportedVarsRendered = false ;
167
176
168
177
constructor ( id : string , props : CodeBuildStepProps ) {
169
178
super ( id , props ) ;
170
179
171
180
this . projectName = props . projectName ;
172
181
this . buildEnvironment = props . buildEnvironment ;
173
- this . partialBuildSpec = props . partialBuildSpec ;
182
+ this . _partialBuildSpec = props . partialBuildSpec ;
174
183
this . vpc = props . vpc ;
175
184
this . subnetSelection = props . subnetSelection ;
176
185
this . role = props . role ;
@@ -198,6 +207,44 @@ export class CodeBuildStep extends ShellStep {
198
207
return this . project . grantPrincipal ;
199
208
}
200
209
210
+ /**
211
+ * Additional configuration that can only be configured via BuildSpec
212
+ *
213
+ * Contains exported variables
214
+ *
215
+ * @default - Contains the exported variables
216
+ */
217
+ public get partialBuildSpec ( ) : codebuild . BuildSpec | undefined {
218
+ this . exportedVarsRendered = true ;
219
+
220
+ const varsBuildSpec = this . exportedVariables . size > 0 ? codebuild . BuildSpec . fromObject ( {
221
+ version : '0.2' ,
222
+ env : {
223
+ 'exported-variables' : Array . from ( this . exportedVariables ) ,
224
+ } ,
225
+ } ) : undefined ;
226
+
227
+ return mergeBuildSpecs ( varsBuildSpec , this . _partialBuildSpec ) ;
228
+ }
229
+
230
+ /**
231
+ * Reference a CodePipeline variable defined by the CodeBuildStep.
232
+ *
233
+ * The variable must be set in the shell of the CodeBuild step when
234
+ * it finishes its `post_build` phase.
235
+ *
236
+ * @param variableName the name of the variable for reference.
237
+ */
238
+ public exportedVariable ( variableName : string ) : string {
239
+ if ( this . exportedVarsRendered && ! this . exportedVariables . has ( variableName ) ) {
240
+ throw new Error ( 'exportVariable(): Pipeline has already been produced, cannot call this function anymore' ) ;
241
+ }
242
+
243
+ this . exportedVariables . add ( variableName ) ;
244
+
245
+ return makeCodePipelineOutput ( this , variableName ) ;
246
+ }
247
+
201
248
/**
202
249
* Set the internal project value
203
250
*
0 commit comments