1
1
import '@aws-cdk/assert-internal/jest' ;
2
- import { arrayWith , objectLike } from '@aws-cdk/assert-internal' ;
2
+ import { arrayWith , objectLike , SynthUtils } from '@aws-cdk/assert-internal' ;
3
3
import * as codebuild from '@aws-cdk/aws-codebuild' ;
4
4
import * as codepipeline from '@aws-cdk/aws-codepipeline' ;
5
5
import { Stack } from '@aws-cdk/core' ;
@@ -148,26 +148,139 @@ describe('CodeStar Connections source Action', () => {
148
148
] ,
149
149
} ) ;
150
150
151
+ } ) ;
152
+
153
+ test ( 'exposes variables' , ( ) => {
154
+ const stack = new Stack ( ) ;
155
+ createBitBucketAndCodeBuildPipeline ( stack ) ;
156
+
157
+ expect ( stack ) . toHaveResourceLike ( 'AWS::CodePipeline::Pipeline' , {
158
+ 'Stages' : [
159
+ {
160
+ 'Name' : 'Source' ,
161
+ } ,
162
+ {
163
+ 'Name' : 'Build' ,
164
+ 'Actions' : [
165
+ {
166
+ 'Name' : 'CodeBuild' ,
167
+ 'Configuration' : {
168
+ 'EnvironmentVariables' : '[{"name":"CommitId","type":"PLAINTEXT","value":"#{Source_BitBucket_NS.CommitId}"}]' ,
169
+ } ,
170
+ } ,
171
+ ] ,
172
+ } ,
173
+ ] ,
174
+ } ) ;
175
+ } ) ;
176
+
177
+ test ( 'exposes variables with custom namespace' , ( ) => {
178
+ const stack = new Stack ( ) ;
179
+ createBitBucketAndCodeBuildPipeline ( stack , {
180
+ variablesNamespace : 'kornicameister' ,
181
+ } ) ;
182
+
183
+ expect ( stack ) . toHaveResourceLike ( 'AWS::CodePipeline::Pipeline' , {
184
+ 'Stages' : [
185
+ {
186
+ 'Name' : 'Source' ,
187
+ 'Actions' : [
188
+ {
189
+ 'Name' : 'BitBucket' ,
190
+ 'Namespace' : 'kornicameister' ,
191
+ } ,
192
+ ] ,
193
+ } ,
194
+ {
195
+ 'Name' : 'Build' ,
196
+ 'Actions' : [
197
+ {
198
+ 'Name' : 'CodeBuild' ,
199
+ 'Configuration' : {
200
+ 'EnvironmentVariables' : '[{"name":"CommitId","type":"PLAINTEXT","value":"#{kornicameister.CommitId}"}]' ,
201
+ } ,
202
+ } ,
203
+ ] ,
204
+ } ,
205
+ ] ,
206
+ } ) ;
207
+ } ) ;
208
+
209
+ test ( 'fail if variable from unused action is referenced' , ( ) => {
210
+ const stack = new Stack ( ) ;
211
+ const pipeline = createBitBucketAndCodeBuildPipeline ( stack ) ;
151
212
213
+ const unusedSourceOutput = new codepipeline . Artifact ( ) ;
214
+ const unusedSourceAction = new cpactions . CodeStarConnectionsSourceAction ( {
215
+ actionName : 'UnusedBitBucket' ,
216
+ owner : 'aws' ,
217
+ repo : 'aws-cdk' ,
218
+ output : unusedSourceOutput ,
219
+ connectionArn : 'arn:aws:codestar-connections:us-east-1:123456789012:connection/12345678-abcd-12ab-34cdef5678gh' ,
220
+ } ) ;
221
+ const unusedBuildAction = new cpactions . CodeBuildAction ( {
222
+ actionName : 'UnusedCodeBuild' ,
223
+ project : new codebuild . PipelineProject ( stack , 'UnusedMyProject' ) ,
224
+ input : unusedSourceOutput ,
225
+ environmentVariables : {
226
+ CommitId : { value : unusedSourceAction . variables . commitId } ,
227
+ } ,
228
+ } ) ;
229
+ pipeline . stage ( 'Build' ) . addAction ( unusedBuildAction ) ;
230
+
231
+ expect ( ( ) => {
232
+ SynthUtils . synthesize ( stack ) ;
233
+ } ) . toThrow ( / C a n n o t r e f e r e n c e v a r i a b l e s o f a c t i o n ' U n u s e d B i t B u c k e t ' , a s t h a t a c t i o n w a s n e v e r a d d e d t o a p i p e l i n e / ) ;
234
+ } ) ;
235
+
236
+ test ( 'fail if variable from unused action with custom namespace is referenced' , ( ) => {
237
+ const stack = new Stack ( ) ;
238
+ const pipeline = createBitBucketAndCodeBuildPipeline ( stack , {
239
+ variablesNamespace : 'kornicameister' ,
240
+ } ) ;
241
+ const unusedSourceOutput = new codepipeline . Artifact ( ) ;
242
+ const unusedSourceAction = new cpactions . CodeStarConnectionsSourceAction ( {
243
+ actionName : 'UnusedBitBucket' ,
244
+ owner : 'aws' ,
245
+ repo : 'aws-cdk' ,
246
+ output : unusedSourceOutput ,
247
+ connectionArn : 'arn:aws:codestar-connections:us-east-1:123456789012:connection/12345678-abcd-12ab-34cdef5678gh' ,
248
+ variablesNamespace : 'kornicameister' ,
249
+ } ) ;
250
+ const unusedBuildAction = new cpactions . CodeBuildAction ( {
251
+ actionName : 'UnusedCodeBuild' ,
252
+ project : new codebuild . PipelineProject ( stack , 'UnusedProject' ) ,
253
+ input : unusedSourceOutput ,
254
+ environmentVariables : {
255
+ CommitId : { value : unusedSourceAction . variables . commitId } ,
256
+ } ,
257
+ } ) ;
258
+ pipeline . stage ( 'Build' ) . addAction ( unusedBuildAction ) ;
259
+
260
+ expect ( ( ) => {
261
+ SynthUtils . synthesize ( stack ) ;
262
+ } ) . toThrow ( / C a n n o t r e f e r e n c e v a r i a b l e s o f a c t i o n ' U n u s e d B i t B u c k e t ' , a s t h a t a c t i o n w a s n e v e r a d d e d t o a p i p e l i n e / ) ;
152
263
} ) ;
153
264
} ) ;
154
265
155
- function createBitBucketAndCodeBuildPipeline ( stack : Stack , props : Partial < cpactions . BitBucketSourceActionProps > ) : void {
266
+ function createBitBucketAndCodeBuildPipeline (
267
+ stack : Stack , props : Partial < cpactions . CodeStarConnectionsSourceActionProps > = { } ,
268
+ ) : codepipeline . Pipeline {
156
269
const sourceOutput = new codepipeline . Artifact ( ) ;
157
- new codepipeline . Pipeline ( stack , 'Pipeline' , {
270
+ const sourceAction = new cpactions . CodeStarConnectionsSourceAction ( {
271
+ actionName : 'BitBucket' ,
272
+ owner : 'aws' ,
273
+ repo : 'aws-cdk' ,
274
+ output : sourceOutput ,
275
+ connectionArn : 'arn:aws:codestar-connections:us-east-1:123456789012:connection/12345678-abcd-12ab-34cdef5678gh' ,
276
+ ...props ,
277
+ } ) ;
278
+
279
+ return new codepipeline . Pipeline ( stack , 'Pipeline' , {
158
280
stages : [
159
281
{
160
282
stageName : 'Source' ,
161
- actions : [
162
- new cpactions . CodeStarConnectionsSourceAction ( {
163
- actionName : 'BitBucket' ,
164
- owner : 'aws' ,
165
- repo : 'aws-cdk' ,
166
- output : sourceOutput ,
167
- connectionArn : 'arn:aws:codestar-connections:us-east-1:123456789012:connection/12345678-abcd-12ab-34cdef5678gh' ,
168
- ...props ,
169
- } ) ,
170
- ] ,
283
+ actions : [ sourceAction ] ,
171
284
} ,
172
285
{
173
286
stageName : 'Build' ,
@@ -177,6 +290,9 @@ function createBitBucketAndCodeBuildPipeline(stack: Stack, props: Partial<cpacti
177
290
project : new codebuild . PipelineProject ( stack , 'MyProject' ) ,
178
291
input : sourceOutput ,
179
292
outputs : [ new codepipeline . Artifact ( ) ] ,
293
+ environmentVariables : {
294
+ CommitId : { value : sourceAction . variables . commitId } ,
295
+ } ,
180
296
} ) ,
181
297
] ,
182
298
} ,
0 commit comments