Skip to content

Commit bba3602

Browse files
authored
feat(aws-codepipeline): allow specifying the runOrder property when creating Actions. (#776)
Also create a new addToPipeline method on LambdaRef, which was missed when doing these for Code* services.
1 parent 3cedc0c commit bba3602

File tree

16 files changed

+103
-47
lines changed

16 files changed

+103
-47
lines changed

Diff for: packages/@aws-cdk/aws-cloudformation/lib/pipeline-actions.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import cdk = require('@aws-cdk/cdk');
55
/**
66
* Properties common to all CloudFormation actions
77
*/
8-
export interface PipelineCloudFormationActionProps extends codepipeline.CommonActionProps {
8+
export interface PipelineCloudFormationActionProps extends codepipeline.CommonActionProps,
9+
codepipeline.CommonActionConstructProps {
910
/**
1011
* The name of the stack to apply this action to
1112
*/
@@ -48,6 +49,7 @@ export abstract class PipelineCloudFormationAction extends codepipeline.DeployAc
4849
constructor(parent: cdk.Construct, id: string, props: PipelineCloudFormationActionProps, configuration?: any) {
4950
super(parent, id, {
5051
stage: props.stage,
52+
runOrder: props.runOrder,
5153
artifactBounds: {
5254
minInputs: 0,
5355
maxInputs: 10,

Diff for: packages/@aws-cdk/aws-codebuild/lib/pipeline-actions.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ProjectRef } from './project';
88
* either directly, through its constructor,
99
* or through {@link ProjectRef#addBuildToPipeline}.
1010
*/
11-
export interface CommonPipelineBuildActionProps {
11+
export interface CommonPipelineBuildActionProps extends codepipeline.CommonActionProps {
1212
/**
1313
* The source to use as input for this build
1414
*/
@@ -23,7 +23,8 @@ export interface CommonPipelineBuildActionProps {
2323
/**
2424
* Construction properties of the {@link PipelineBuildAction CodeBuild build CodePipeline Action}.
2525
*/
26-
export interface PipelineBuildActionProps extends CommonPipelineBuildActionProps, codepipeline.CommonActionProps {
26+
export interface PipelineBuildActionProps extends CommonPipelineBuildActionProps,
27+
codepipeline.CommonActionConstructProps {
2728
/**
2829
* The build project
2930
*/
@@ -40,6 +41,7 @@ export class PipelineBuildAction extends codepipeline.BuildAction {
4041

4142
super(parent, name, {
4243
stage: props.stage,
44+
runOrder: props.runOrder,
4345
provider: 'CodeBuild',
4446
inputArtifact: props.inputArtifact,
4547
artifactName: props.artifactName,

Diff for: packages/@aws-cdk/aws-codecommit/lib/pipeline-action.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { RepositoryRef } from './repository';
88
* either directly, through its constructor,
99
* or through {@link RepositoryRef#addToPipeline}.
1010
*/
11-
export interface CommonPipelineSourceActionProps {
11+
export interface CommonPipelineSourceActionProps extends codepipeline.CommonActionProps {
1212
/**
1313
* The name of the source's output artifact.
1414
* Output artifacts are used by CodePipeline as inputs into other actions.
@@ -32,7 +32,8 @@ export interface CommonPipelineSourceActionProps {
3232
/**
3333
* Construction properties of the {@link PipelineSourceAction CodeCommit source CodePipeline Action}.
3434
*/
35-
export interface PipelineSourceActionProps extends CommonPipelineSourceActionProps, codepipeline.CommonActionProps {
35+
export interface PipelineSourceActionProps extends CommonPipelineSourceActionProps,
36+
codepipeline.CommonActionConstructProps {
3637
/**
3738
* The CodeCommit repository.
3839
*/
@@ -46,6 +47,7 @@ export class PipelineSourceAction extends codepipeline.SourceAction {
4647
constructor(parent: cdk.Construct, name: string, props: PipelineSourceActionProps) {
4748
super(parent, name, {
4849
stage: props.stage,
50+
runOrder: props.runOrder,
4951
provider: 'CodeCommit',
5052
configuration: {
5153
RepositoryName: props.repository.repositoryName,

Diff for: packages/@aws-cdk/aws-codedeploy/lib/pipeline-action.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import actions = require('@aws-cdk/aws-codepipeline-api');
1+
import codepipeline = require('@aws-cdk/aws-codepipeline-api');
22
import iam = require('@aws-cdk/aws-iam');
33
import cdk = require('@aws-cdk/cdk');
44

55
/**
66
* Construction properties of the {@link PipelineDeployAction CodeDeploy deploy CodePipeline Action}.
77
*/
8-
export interface PipelineDeployActionProps extends actions.CommonActionProps {
8+
export interface PipelineDeployActionProps extends codepipeline.CommonActionProps,
9+
codepipeline.CommonActionConstructProps {
910
/**
1011
* The name of the CodeDeploy application to deploy to.
1112
*
@@ -25,13 +26,14 @@ export interface PipelineDeployActionProps extends actions.CommonActionProps {
2526
/**
2627
* The source to use as input for deployment.
2728
*/
28-
inputArtifact: actions.Artifact;
29+
inputArtifact: codepipeline.Artifact;
2930
}
3031

31-
export class PipelineDeployAction extends actions.DeployAction {
32+
export class PipelineDeployAction extends codepipeline.DeployAction {
3233
constructor(parent: cdk.Construct, id: string, props: PipelineDeployActionProps) {
3334
super(parent, id, {
3435
stage: props.stage,
36+
runOrder: props.runOrder,
3537
artifactBounds: { minInputs: 1, maxInputs: 1, minOutputs: 0, maxOutputs: 0 },
3638
provider: 'CodeDeploy',
3739
inputArtifact: props.inputArtifact,

Diff for: packages/@aws-cdk/aws-codepipeline-api/lib/action.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ export interface IStage {
7777
* Common properties shared by all Actions.
7878
*/
7979
export interface CommonActionProps {
80+
/**
81+
* The runOrder property for this Action.
82+
* RunOrder determines the relative order in which multiple Actions in the same Stage execute.
83+
*
84+
* @default 1
85+
* @see https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html
86+
*/
87+
runOrder?: number;
88+
}
89+
90+
/**
91+
* Common properties shared by all Action Constructs.
92+
*/
93+
export interface CommonActionConstructProps {
8094
/**
8195
* The Pipeline Stage to add this Action to.
8296
*/
@@ -86,7 +100,7 @@ export interface CommonActionProps {
86100
/**
87101
* Construction properties of the low-level {@link Action Action class}.
88102
*/
89-
export interface ActionProps extends CommonActionProps {
103+
export interface ActionProps extends CommonActionProps, CommonActionConstructProps {
90104
category: ActionCategory;
91105
provider: string;
92106
artifactBounds: ActionArtifactBounds;
@@ -127,7 +141,7 @@ export abstract class Action extends cdk.Construct {
127141
*
128142
* https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html#action-requirements
129143
*/
130-
public runOrder: number;
144+
public readonly runOrder: number;
131145

132146
public readonly owner: string;
133147
public readonly version: string;
@@ -148,7 +162,7 @@ export abstract class Action extends cdk.Construct {
148162
this.provider = props.provider;
149163
this.configuration = props.configuration;
150164
this.artifactBounds = props.artifactBounds;
151-
this.runOrder = 1;
165+
this.runOrder = props.runOrder === undefined ? 1 : props.runOrder;
152166
this.stage = props.stage;
153167

154168
this.stage._attachAction(this);

Diff for: packages/@aws-cdk/aws-codepipeline-api/lib/build-action.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import cdk = require("@aws-cdk/cdk");
2-
import { Action, ActionCategory, CommonActionProps } from "./action";
2+
import { Action, ActionCategory, CommonActionConstructProps, CommonActionProps } from "./action";
33
import { Artifact } from "./artifact";
44

55
/**
66
* Construction properties of the low level {@link BuildAction build action}.
77
*/
8-
export interface BuildActionProps extends CommonActionProps {
8+
export interface BuildActionProps extends CommonActionProps, CommonActionConstructProps {
99
/**
1010
* The source to use as input for this build.
1111
*/
@@ -41,6 +41,7 @@ export abstract class BuildAction extends Action {
4141
constructor(parent: cdk.Construct, name: string, props: BuildActionProps) {
4242
super(parent, name, {
4343
stage: props.stage,
44+
runOrder: props.runOrder,
4445
artifactBounds: { minInputs: 1, maxInputs: 1, minOutputs: 0, maxOutputs: 1 },
4546
category: ActionCategory.Build,
4647
provider: props.provider,

Diff for: packages/@aws-cdk/aws-codepipeline-api/lib/deploy-action.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import cdk = require('@aws-cdk/cdk');
2-
import { Action, ActionArtifactBounds, ActionCategory, CommonActionProps } from "./action";
2+
import { Action, ActionArtifactBounds, ActionCategory, CommonActionConstructProps, CommonActionProps } from "./action";
33
import { Artifact } from './artifact';
44

5-
export interface DeployActionProps extends CommonActionProps {
5+
export interface DeployActionProps extends CommonActionProps, CommonActionConstructProps {
66
provider: string;
77

88
artifactBounds: ActionArtifactBounds;
@@ -16,6 +16,7 @@ export abstract class DeployAction extends Action {
1616
constructor(parent: cdk.Construct, name: string, props: DeployActionProps) {
1717
super(parent, name, {
1818
stage: props.stage,
19+
runOrder: props.runOrder,
1920
category: ActionCategory.Deploy,
2021
provider: props.provider,
2122
artifactBounds: props.artifactBounds,

Diff for: packages/@aws-cdk/aws-codepipeline-api/lib/source-action.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import cdk = require("@aws-cdk/cdk");
2-
import { Action, ActionCategory, CommonActionProps } from "./action";
2+
import { Action, ActionCategory, CommonActionConstructProps, CommonActionProps } from "./action";
33
import { Artifact } from "./artifact";
44

55
/**
66
* Construction properties of the low-level {@link SourceAction source Action}.
77
*/
8-
export interface SourceActionProps extends CommonActionProps {
8+
export interface SourceActionProps extends CommonActionProps, CommonActionConstructProps {
99
/**
1010
* The source action owner (could be "AWS", "ThirdParty" or "Custom").
1111
*
@@ -53,6 +53,7 @@ export abstract class SourceAction extends Action {
5353
constructor(parent: cdk.Construct, name: string, props: SourceActionProps) {
5454
super(parent, name, {
5555
stage: props.stage,
56+
runOrder: props.runOrder,
5657
category: ActionCategory.Source,
5758
owner: props.owner,
5859
provider: props.provider,

Diff for: packages/@aws-cdk/aws-codepipeline/lib/github-source-action.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import cdk = require('@aws-cdk/cdk');
44
/**
55
* Construction properties of the {@link GitHubSourceAction GitHub source action}.
66
*/
7-
export interface GitHubSourceActionProps extends actions.CommonActionProps {
7+
export interface GitHubSourceActionProps extends actions.CommonActionProps,
8+
actions.CommonActionConstructProps {
89
/**
910
* The name of the source's output artifact. Output artifacts are used by CodePipeline as
1011
* inputs into other actions.
@@ -55,6 +56,7 @@ export class GitHubSourceAction extends actions.SourceAction {
5556
constructor(parent: cdk.Construct, name: string, props: GitHubSourceActionProps) {
5657
super(parent, name, {
5758
stage: props.stage,
59+
runOrder: props.runOrder,
5860
owner: 'ThirdParty',
5961
provider: 'GitHub',
6062
configuration: {

Diff for: packages/@aws-cdk/aws-codepipeline/lib/manual-approval-action.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import actions = require('@aws-cdk/aws-codepipeline-api');
22
import cdk = require('@aws-cdk/cdk');
33

44
// tslint:disable-next-line:no-empty-interface
5-
export interface ManualApprovalActionProps extends actions.CommonActionProps {
5+
export interface ManualApprovalActionProps extends actions.CommonActionProps,
6+
actions.CommonActionConstructProps {
67
}
78

89
/**
@@ -11,10 +12,10 @@ export interface ManualApprovalActionProps extends actions.CommonActionProps {
1112
export class ManualApprovalAction extends actions.Action {
1213
constructor(parent: cdk.Construct, name: string, props: ManualApprovalActionProps) {
1314
super(parent, name, {
14-
stage: props.stage,
1515
category: actions.ActionCategory.Approval,
1616
provider: 'Manual',
17-
artifactBounds: { minInputs: 0, maxInputs: 0, minOutputs: 0, maxOutputs: 0 }
17+
artifactBounds: { minInputs: 0, maxInputs: 0, minOutputs: 0, maxOutputs: 0 },
18+
...props,
1819
});
1920
}
2021
}

Diff for: packages/@aws-cdk/aws-codepipeline/test/integ.lambda-pipeline.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ const lambdaFun = new lambda.Function(stack, 'LambdaFun', {
3030
runtime: lambda.Runtime.NodeJS610,
3131
});
3232
const lambdaStage = new codepipeline.Stage(pipeline, 'Lambda', { pipeline });
33-
new lambda.PipelineInvokeAction(stack, 'Lambda', {
34-
stage: lambdaStage,
35-
lambda: lambdaFun,
36-
});
33+
lambdaFun.addToPipeline(lambdaStage, 'Lambda');
3734

3835
app.run();

Diff for: packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export = {
5151
const s1 = new codepipeline.Stage(stack, 'Source', { pipeline: p });
5252
new codepipeline.GitHubSourceAction(stack, 'GH', {
5353
stage: s1,
54+
runOrder: 8,
5455
artifactName: 'A',
5556
branch: 'branch',
5657
oauthToken: secret.value,
@@ -100,7 +101,7 @@ export = {
100101
"Name": "A"
101102
}
102103
],
103-
"RunOrder": 1
104+
"RunOrder": 8
104105
}
105106
],
106107
"Name": "Source"

Diff for: packages/@aws-cdk/aws-lambda/README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ This module also contains an Action that allows you to invoke a Lambda function
4242

4343
```ts
4444
import codepipeline = require('@aws-cdk/aws-codepipeline');
45-
import lambda = require('@aws-cdk/aws-lambda');
46-
47-
const lambdaFun = new lambda.Function(this, 'MyLambda', {
48-
// some lambda parameters here...
49-
});
5045

5146
const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
52-
const lambdaStage = new codepipeline.Stage(this, 'Lambda', {
53-
pipeline,
54-
});
47+
const lambdaStage = pipeline.addStage('Lambda');
5548
new lambda.PipelineInvokeAction(this, 'Lambda', {
5649
stage: lambdaStage,
57-
lambda: lambdaFun,
50+
lambda: fn,
5851
});
5952
```
6053

54+
You can also add the Lambda to the Pipeline directly:
55+
56+
```ts
57+
// equivalent to the code above:
58+
fn.addToPipeline(lambdaStage, 'Lambda');
59+
```
60+
6161
See [the AWS documentation](https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html)
6262
on how to write a Lambda function invoked from CodePipeline.
6363

Diff for: packages/@aws-cdk/aws-lambda/lib/lambda-ref.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
2+
import codepipeline = require('@aws-cdk/aws-codepipeline-api');
23
import ec2 = require('@aws-cdk/aws-ec2');
34
import events = require('@aws-cdk/aws-events');
45
import iam = require('@aws-cdk/aws-iam');
@@ -7,6 +8,7 @@ import s3n = require('@aws-cdk/aws-s3-notifications');
78
import cdk = require('@aws-cdk/cdk');
89
import { cloudformation } from './lambda.generated';
910
import { Permission } from './permission';
11+
import { CommonPipelineInvokeActionProps, PipelineInvokeAction } from './pipeline-action';
1012

1113
/**
1214
* Represents a Lambda function defined outside of this stack.
@@ -181,6 +183,23 @@ export abstract class FunctionRef extends cdk.Construct
181183
});
182184
}
183185

186+
/**
187+
* Convenience method for creating a new {@link PipelineInvokeAction},
188+
* and adding it to the given Stage.
189+
*
190+
* @param stage the Pipeline Stage to add the new Action to
191+
* @param name the name of the newly created Action
192+
* @param props the properties of the new Action
193+
* @returns the newly created {@link PipelineInvokeAction}
194+
*/
195+
public addToPipeline(stage: codepipeline.IStage, name: string, props: CommonPipelineInvokeActionProps = {}): PipelineInvokeAction {
196+
return new PipelineInvokeAction(this, name, {
197+
stage,
198+
lambda: this,
199+
...props,
200+
});
201+
}
202+
184203
public addToRolePolicy(statement: iam.PolicyStatement) {
185204
if (!this.role) {
186205
return;

Diff for: packages/@aws-cdk/aws-lambda/lib/pipeline-action.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import cdk = require('@aws-cdk/cdk');
44
import { FunctionRef } from './lambda-ref';
55

66
/**
7-
* Construction properties of the {@link PipelineInvokeAction Lambda invoke CodePipeline Action}.
7+
* Common properties for creating a {@link PipelineInvokeAction} -
8+
* either directly, through its constructor,
9+
* or through {@link FunctionRef#addToPipeline}.
810
*/
9-
export interface PipelineInvokeActionProps extends codepipeline.CommonActionProps {
10-
/**
11-
* The lambda function to invoke.
12-
*/
13-
lambda: FunctionRef;
14-
11+
export interface CommonPipelineInvokeActionProps extends codepipeline.CommonActionProps {
1512
/**
1613
* String to be used in the event data parameter passed to the Lambda
1714
* function
@@ -40,6 +37,17 @@ export interface PipelineInvokeActionProps extends codepipeline.CommonActionProp
4037
addPutJobResultPolicy?: boolean;
4138
}
4239

40+
/**
41+
* Construction properties of the {@link PipelineInvokeAction Lambda invoke CodePipeline Action}.
42+
*/
43+
export interface PipelineInvokeActionProps extends CommonPipelineInvokeActionProps,
44+
codepipeline.CommonActionConstructProps {
45+
/**
46+
* The lambda function to invoke.
47+
*/
48+
lambda: FunctionRef;
49+
}
50+
4351
/**
4452
* CodePipeline invoke Action that is provided by an AWS Lambda function.
4553
*
@@ -49,6 +57,7 @@ export class PipelineInvokeAction extends codepipeline.Action {
4957
constructor(parent: cdk.Construct, name: string, props: PipelineInvokeActionProps) {
5058
super(parent, name, {
5159
stage: props.stage,
60+
runOrder: props.runOrder,
5261
category: codepipeline.ActionCategory.Invoke,
5362
provider: 'Lambda',
5463
artifactBounds: codepipeline.defaultBounds(),

0 commit comments

Comments
 (0)