-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathaction.ts
562 lines (494 loc) · 18.8 KB
/
action.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import { Construct } from 'constructs';
import { Artifact } from './artifact';
import * as notifications from '../../aws-codestarnotifications';
import * as events from '../../aws-events';
import * as iam from '../../aws-iam';
import * as s3 from '../../aws-s3';
import { IResource, Lazy } from '../../core';
export enum ActionCategory {
SOURCE = 'Source',
BUILD = 'Build',
TEST = 'Test',
APPROVAL = 'Approval',
DEPLOY = 'Deploy',
INVOKE = 'Invoke',
}
/**
* Specifies the constraints on the number of input and output
* artifacts an action can have.
*
* The constraints for each action type are documented on the
* [Pipeline Structure Reference](https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html) page.
*/
export interface ActionArtifactBounds {
readonly minInputs: number;
readonly maxInputs: number;
readonly minOutputs: number;
readonly maxOutputs: number;
}
/**
* The CodePipeline variables that are global,
* not bound to a specific action.
* This class defines a bunch of static fields that represent the different variables.
* These can be used can be used in any action configuration.
*/
export class GlobalVariables {
/** The identifier of the current pipeline execution. */
public static readonly executionId = '#{codepipeline.PipelineExecutionId}';
}
export interface ActionProperties {
readonly actionName: string;
readonly role?: iam.IRole;
/**
* The AWS region the given Action resides in.
* Note that a cross-region Pipeline requires replication buckets to function correctly.
* You can provide their names with the `PipelineProps#crossRegionReplicationBuckets` property.
* If you don't, the CodePipeline Construct will create new Stacks in your CDK app containing those buckets,
* that you will need to `cdk deploy` before deploying the main, Pipeline-containing Stack.
*
* @default the Action resides in the same region as the Pipeline
*/
readonly region?: string;
/**
* The account the Action is supposed to live in.
* For Actions backed by resources,
* this is inferred from the Stack `resource` is part of.
* However, some Actions, like the CloudFormation ones,
* are not backed by any resource, and they still might want to be cross-account.
* In general, a concrete Action class should specify either `resource`,
* or `account` - but not both.
*/
readonly account?: string;
/**
* The optional resource that is backing this Action.
* This is used for automatically handling Actions backed by
* resources from a different account and/or region.
*/
readonly resource?: IResource;
/**
* The category of the action.
* The category defines which action type the owner
* (the entity that performs the action) performs.
*/
readonly category: ActionCategory;
/**
* The service provider that the action calls.
*/
readonly provider: string;
readonly owner?: string;
readonly version?: string;
/**
* The order in which AWS CodePipeline runs this action.
* For more information, see the AWS CodePipeline User Guide.
*
* https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html#action-requirements
*/
readonly runOrder?: number;
readonly artifactBounds: ActionArtifactBounds;
readonly inputs?: Artifact[];
readonly outputs?: Artifact[];
/**
* The name of the namespace to use for variables emitted by this action.
*
* @default - a name will be generated, based on the stage and action names
*/
readonly variablesNamespace?: string;
}
export interface ActionBindOptions {
readonly role: iam.IRole;
readonly bucket: s3.IBucket;
}
export interface ActionConfig {
readonly configuration?: any;
}
/**
* Additional options to pass to the notification rule.
*/
export interface PipelineNotifyOnOptions extends notifications.NotificationRuleOptions {
/**
* A list of event types associated with this notification rule for CodePipeline Pipeline.
* For a complete list of event types and IDs, see Notification concepts in the Developer Tools Console User Guide.
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#concepts-api
*/
readonly events: PipelineNotificationEvents[];
}
/**
* A Pipeline Action.
* If you want to implement this interface,
* consider extending the `Action` class,
* which contains some common logic.
*/
export interface IAction {
/**
* The simple properties of the Action,
* like its Owner, name, etc.
* Note that this accessor will be called before the `bind` callback.
*/
readonly actionProperties: ActionProperties;
/**
* The callback invoked when this Action is added to a Pipeline.
*
* @param scope the Construct tree scope the Action can use if it needs to create any resources
* @param stage the `IStage` this Action is being added to
* @param options additional options the Action can use,
* like the artifact Bucket of the pipeline it's being added to
*/
bind(scope: Construct, stage: IStage, options: ActionBindOptions): ActionConfig;
/**
* Creates an Event that will be triggered whenever the state of this Action changes.
*
* @param name the name to use for the new Event
* @param target the optional target for the Event
* @param options additional options that can be used to customize the created Event
*/
onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;
}
/**
* The abstract view of an AWS CodePipeline as required and used by Actions.
* It extends `events.IRuleTarget`,
* so this interface can be used as a Target for CloudWatch Events.
*/
export interface IPipeline extends IResource, notifications.INotificationRuleSource {
/**
* The name of the Pipeline.
*
* @attribute
*/
readonly pipelineName: string;
/**
* The ARN of the Pipeline.
*
* @attribute
*/
readonly pipelineArn: string;
/**
* Define an event rule triggered by this CodePipeline.
*
* @param id Identifier for this event handler.
* @param options Additional options to pass to the event rule.
*/
onEvent(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Define an event rule triggered by the "CodePipeline Pipeline Execution
* State Change" event emitted from this pipeline.
*
* @param id Identifier for this event handler.
* @param options Additional options to pass to the event rule.
*/
onStateChange(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Defines a CodeStar notification rule triggered when the pipeline
* events emitted by you specified, it very similar to `onEvent` API.
*
* You can also use the methods `notifyOnExecutionStateChange`, `notifyOnAnyStageStateChange`,
* `notifyOnAnyActionStateChange` and `notifyOnAnyManualApprovalStateChange`
* to define rules for these specific event emitted.
*
* @param id The id of the CodeStar notification rule
* @param target The target to register for the CodeStar Notifications destination.
* @param options Customization options for CodeStar notification rule
* @returns CodeStar notification rule associated with this build project.
*/
notifyOn(
id: string,
target: notifications.INotificationRuleTarget,
options: PipelineNotifyOnOptions,
): notifications.INotificationRule;
/**
* Define an notification rule triggered by the set of the "Pipeline execution" events emitted from this pipeline.
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-pipeline
*
* @param id Identifier for this notification handler.
* @param target The target to register for the CodeStar Notifications destination.
* @param options Additional options to pass to the notification rule.
*/
notifyOnExecutionStateChange(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;
/**
* Define an notification rule triggered by the set of the "Stage execution" events emitted from this pipeline.
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-pipeline
*
* @param id Identifier for this notification handler.
* @param target The target to register for the CodeStar Notifications destination.
* @param options Additional options to pass to the notification rule.
*/
notifyOnAnyStageStateChange(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;
/**
* Define an notification rule triggered by the set of the "Action execution" events emitted from this pipeline.
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-pipeline
*
* @param id Identifier for this notification handler.
* @param target The target to register for the CodeStar Notifications destination.
* @param options Additional options to pass to the notification rule.
*/
notifyOnAnyActionStateChange(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;
/**
* Define an notification rule triggered by the set of the "Manual approval" events emitted from this pipeline.
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-pipeline
*
* @param id Identifier for this notification handler.
* @param target The target to register for the CodeStar Notifications destination.
* @param options Additional options to pass to the notification rule.
*/
notifyOnAnyManualApprovalStateChange(
id: string,
target: notifications.INotificationRuleTarget,
options?: notifications.NotificationRuleOptions,
): notifications.INotificationRule;
}
/**
* The abstract interface of a Pipeline Stage that is used by Actions.
*/
export interface IStage {
/**
* The physical, human-readable name of this Pipeline Stage.
*/
readonly stageName: string;
readonly pipeline: IPipeline;
/**
* The actions belonging to this stage.
*/
readonly actions: IAction[];
addAction(action: IAction): void;
onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;
}
/**
* Common properties shared by all Actions.
*/
export interface CommonActionProps {
/**
* The physical, human-readable name of the Action.
* Note that Action names must be unique within a single Stage.
*/
readonly actionName: string;
/**
* The runOrder property for this Action.
* RunOrder determines the relative order in which multiple Actions in the same Stage execute.
*
* @default 1
* @see https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html
*/
readonly runOrder?: number;
/**
* The name of the namespace to use for variables emitted by this action.
*
* @default - a name will be generated, based on the stage and action names,
* if any of the action's variables were referenced - otherwise,
* no namespace will be set
*/
readonly variablesNamespace?: string;
}
/**
* Common properties shared by all Actions whose `ActionProperties.owner` field is 'AWS'
* (or unset, as 'AWS' is the default).
*/
export interface CommonAwsActionProps extends CommonActionProps {
/**
* The Role in which context's this Action will be executing in.
* The Pipeline's Role will assume this Role
* (the required permissions for that will be granted automatically)
* right before executing this Action.
* This Action will be passed into your `IAction.bind`
* method in the `ActionBindOptions.role` property.
*
* @default a new Role will be generated
*/
readonly role?: iam.IRole;
}
/**
* Low-level class for generic CodePipeline Actions implementing the `IAction` interface.
* Contains some common logic that can be re-used by all `IAction` implementations.
* If you're writing your own Action class,
* feel free to extend this class.
*/
export abstract class Action implements IAction {
/**
* This is a renamed version of the `IAction.actionProperties` property.
*/
protected abstract readonly providedActionProperties: ActionProperties;
private __actionProperties?: ActionProperties;
private __pipeline?: IPipeline;
private __stage?: IStage;
private __scope?: Construct;
private readonly _namespaceToken: string;
private _customerProvidedNamespace?: string;
private _actualNamespace?: string;
private _variableReferenced = false;
protected constructor() {
this._namespaceToken = Lazy.string({
produce: () => {
// make sure the action was bound (= added to a pipeline)
if (this._actualNamespace === undefined) {
throw new Error(`Cannot reference variables of action '${this.actionProperties.actionName}', ` +
'as that action was never added to a pipeline');
} else {
return this._customerProvidedNamespace !== undefined
// if a customer passed a namespace explicitly, always use that
? this._customerProvidedNamespace
// otherwise, only return a namespace if any variable was referenced
: (this._variableReferenced ? this._actualNamespace : undefined);
}
},
});
}
public get actionProperties(): ActionProperties {
if (this.__actionProperties === undefined) {
const actionProperties = this.providedActionProperties;
this._customerProvidedNamespace = actionProperties.variablesNamespace;
this.__actionProperties = {
...actionProperties,
variablesNamespace: this._customerProvidedNamespace === undefined
? this._namespaceToken
: this._customerProvidedNamespace,
};
}
return this.__actionProperties;
}
public bind(scope: Construct, stage: IStage, options: ActionBindOptions): ActionConfig {
this.__pipeline = stage.pipeline;
this.__stage = stage;
this.__scope = scope;
this._actualNamespace = this._customerProvidedNamespace === undefined
// default a namespace name, based on the stage and action names
? `${stage.stageName}_${this.actionProperties.actionName}_NS`
: this._customerProvidedNamespace;
return this.bound(scope, stage, options);
}
public onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
const rule = new events.Rule(this._scope, name, options);
rule.addTarget(target);
rule.addEventPattern({
detailType: ['CodePipeline Action Execution State Change'],
source: ['aws.codepipeline'],
resources: [this._pipeline.pipelineArn],
detail: {
stage: [this._stage.stageName],
action: [this.actionProperties.actionName],
},
});
return rule;
}
protected variableExpression(variableName: string): string {
this._variableReferenced = true;
return `#{${this._namespaceToken}.${variableName}}`;
}
/**
* This is a renamed version of the `IAction.bind` method.
*/
protected abstract bound(scope: Construct, stage: IStage, options: ActionBindOptions): ActionConfig;
private get _pipeline(): IPipeline {
if (this.__pipeline) {
return this.__pipeline;
} else {
throw new Error('Action must be added to a stage that is part of a pipeline before using onStateChange');
}
}
private get _stage(): IStage {
if (this.__stage) {
return this.__stage;
} else {
throw new Error('Action must be added to a stage that is part of a pipeline before using onStateChange');
}
}
/**
* Retrieves the Construct scope of this Action.
* Only available after the Action has been added to a Stage,
* and that Stage to a Pipeline.
*/
private get _scope(): Construct {
if (this.__scope) {
return this.__scope;
} else {
throw new Error('Action must be added to a stage that is part of a pipeline first');
}
}
}
/**
* The list of event types for AWS Codepipeline Pipeline
* @see https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-pipeline
*/
export enum PipelineNotificationEvents {
/**
* Trigger notification when pipeline execution failed
*/
PIPELINE_EXECUTION_FAILED = 'codepipeline-pipeline-pipeline-execution-failed',
/**
* Trigger notification when pipeline execution canceled
*/
PIPELINE_EXECUTION_CANCELED = 'codepipeline-pipeline-pipeline-execution-canceled',
/**
* Trigger notification when pipeline execution started
*/
PIPELINE_EXECUTION_STARTED = 'codepipeline-pipeline-pipeline-execution-started',
/**
* Trigger notification when pipeline execution resumed
*/
PIPELINE_EXECUTION_RESUMED = 'codepipeline-pipeline-pipeline-execution-resumed',
/**
* Trigger notification when pipeline execution succeeded
*/
PIPELINE_EXECUTION_SUCCEEDED = 'codepipeline-pipeline-pipeline-execution-succeeded',
/**
* Trigger notification when pipeline execution superseded
*/
PIPELINE_EXECUTION_SUPERSEDED = 'codepipeline-pipeline-pipeline-execution-superseded',
/**
* Trigger notification when pipeline stage execution started
*/
STAGE_EXECUTION_STARTED = 'codepipeline-pipeline-stage-execution-started',
/**
* Trigger notification when pipeline stage execution succeeded
*/
STAGE_EXECUTION_SUCCEEDED = 'codepipeline-pipeline-stage-execution-succeeded',
/**
* Trigger notification when pipeline stage execution resumed
*/
STAGE_EXECUTION_RESUMED = 'codepipeline-pipeline-stage-execution-resumed',
/**
* Trigger notification when pipeline stage execution canceled
*/
STAGE_EXECUTION_CANCELED = 'codepipeline-pipeline-stage-execution-canceled',
/**
* Trigger notification when pipeline stage execution failed
*/
STAGE_EXECUTION_FAILED = 'codepipeline-pipeline-stage-execution-failed',
/**
* Trigger notification when pipeline action execution succeeded
*/
ACTION_EXECUTION_SUCCEEDED = 'codepipeline-pipeline-action-execution-succeeded',
/**
* Trigger notification when pipeline action execution failed
*/
ACTION_EXECUTION_FAILED = 'codepipeline-pipeline-action-execution-failed',
/**
* Trigger notification when pipeline action execution canceled
*/
ACTION_EXECUTION_CANCELED = 'codepipeline-pipeline-action-execution-canceled',
/**
* Trigger notification when pipeline action execution started
*/
ACTION_EXECUTION_STARTED = 'codepipeline-pipeline-action-execution-started',
/**
* Trigger notification when pipeline manual approval failed
*/
MANUAL_APPROVAL_FAILED = 'codepipeline-pipeline-manual-approval-failed',
/**
* Trigger notification when pipeline manual approval needed
*/
MANUAL_APPROVAL_NEEDED = 'codepipeline-pipeline-manual-approval-needed',
/**
* Trigger notification when pipeline manual approval succeeded
*/
MANUAL_APPROVAL_SUCCEEDED = 'codepipeline-pipeline-manual-approval-succeeded',
}