Skip to content

Commit 3d91c93

Browse files
authored
feat(aws-codepipeline): make input and output artifact names optional when creating Actions. (#845)
BREAKING CHANGE: this commit contains the following breaking changes: * Rename 'artifactName' in Action construction properties to 'outputArtifactName' * Rename the 'artifact' property of Actions to 'outputArtifact' * No longer allow adding output artifacts to Actions by instantiating the Artifact class * Rename Action#input/outputArtifacts properties to _input/_outputArtifacts Previously, we always required customers to explicitly name the output artifacts the Actions used in the Pipeline, and to explicitly "wire together" the outputs of one Action as inputs to another. With this change, the CodePipeline Construct generates artifact names, if the customer didn't provide one explicitly, and tries to find the first available output artifact to use as input to a newly created Action that needs it, thus turning both the input and output artifacts from required to optional properties.
1 parent 89ba45d commit 3d91c93

36 files changed

+330
-221
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ export interface PipelineCloudFormationActionProps extends codepipeline.CommonAc
3838
/**
3939
* Base class for Actions that execute CloudFormation
4040
*/
41-
export abstract class PipelineCloudFormationAction extends codepipeline.DeployAction {
41+
export abstract class PipelineCloudFormationAction extends codepipeline.Action {
4242
/**
4343
* Output artifact containing the CloudFormation call response
4444
*
4545
* Only present if configured by passing `outputFileName`.
4646
*/
47-
public artifact?: codepipeline.Artifact;
47+
public outputArtifact?: codepipeline.Artifact;
4848

4949
constructor(parent: cdk.Construct, id: string, props: PipelineCloudFormationActionProps, configuration?: any) {
5050
super(parent, id, {
@@ -57,6 +57,7 @@ export abstract class PipelineCloudFormationAction extends codepipeline.DeployAc
5757
maxOutputs: 1,
5858
},
5959
provider: 'CloudFormation',
60+
category: codepipeline.ActionCategory.Deploy,
6061
configuration: {
6162
StackName: props.stackName,
6263
OutputFileName: props.outputFileName,
@@ -65,7 +66,7 @@ export abstract class PipelineCloudFormationAction extends codepipeline.DeployAc
6566
});
6667

6768
if (props.outputFileName) {
68-
this.artifact = this.addOutputArtifact(props.outputArtifactName ||
69+
this.outputArtifact = this.addOutputArtifact(props.outputArtifactName ||
6970
(props.stage.name + this.id + 'Artifact'));
7071
}
7172
}

packages/@aws-cdk/aws-cloudformation/test/test.pipeline-actions.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export = nodeunit.testCase({
3232
_assertPermissionGranted(test, pipelineRole.statements, 'cloudformation:CreateChangeSet', stackArn, changeSetCondition);
3333
_assertPermissionGranted(test, pipelineRole.statements, 'cloudformation:DeleteChangeSet', stackArn, changeSetCondition);
3434

35-
test.deepEqual(action.inputArtifacts, [artifact],
35+
test.deepEqual(action._inputArtifacts, [artifact],
3636
'The inputArtifact was correctly registered');
3737

3838
_assertActionMatches(test, stage.actions, 'AWS', 'CloudFormation', 'Deploy', {
@@ -145,10 +145,11 @@ function _isOrContains(entity: string | string[], value: string): boolean {
145145
return false;
146146
}
147147

148-
class StageDouble implements cpapi.IStage {
148+
class StageDouble implements cpapi.IStage, cpapi.IInternalStage {
149149
public readonly name: string;
150150
public readonly pipelineArn: string;
151151
public readonly pipelineRole: iam.Role;
152+
public readonly _internal = this;
152153

153154
public readonly actions = new Array<cpapi.Action>();
154155

@@ -165,6 +166,14 @@ class StageDouble implements cpapi.IStage {
165166
public _attachAction(action: cpapi.Action) {
166167
this.actions.push(action);
167168
}
169+
170+
public _generateOutputArtifactName(): string {
171+
throw new Error('Unsupported');
172+
}
173+
174+
public _findInputArtifact(): cpapi.Artifact {
175+
throw new Error('Unsupported');
176+
}
168177
}
169178

170179
class RoleDouble extends iam.Role {

packages/@aws-cdk/aws-codebuild/README.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,12 @@ const project = new codebuild.PipelineProject(this, 'MyProject');
5050

5151
const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
5252

53-
const sourceStage = new codepipeline.Stage(this, 'Source', {
54-
pipeline,
55-
});
56-
const sourceAction = new codecommit.PipelineSourceAction(this, 'CodeCommit', {
57-
stage: sourceStage,
58-
artifactName: 'SourceOutput',
59-
repository,
60-
});
53+
const sourceStage = pipeline.addStage('Source');
54+
repository.addToPipeline(sourceStage, 'CodeCommit');
6155

62-
const buildStage = new codepipeline.Stage(this, 'Build', {
63-
pipeline,
64-
});
56+
const buildStage = pipeline.addStage('Build');
6557
new codebuild.PipelineBuildAction(this, 'CodeBuild', {
6658
stage: buildStage,
67-
inputArtifact: sourceAction.artifact,
6859
project,
6960
});
7061
```
@@ -84,9 +75,7 @@ You can also add the Project to the Pipeline directly:
8475
8576
```ts
8677
// equivalent to the code above:
87-
project.addBuildToPipeline(buildStage, 'CodeBuild', {
88-
inputArtifact: sourceAction.artifact,
89-
})
78+
project.addBuildToPipeline(buildStage, 'CodeBuild');
9079
```
9180
9281
### Using Project as an event target

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ import { ProjectRef } from './project';
1010
*/
1111
export interface CommonPipelineBuildActionProps extends codepipeline.CommonActionProps {
1212
/**
13-
* The source to use as input for this build
13+
* The source to use as input for this build.
14+
*
15+
* @default CodePipeline will use the output of the last Action from a previous Stage as input
1416
*/
15-
inputArtifact: codepipeline.Artifact;
17+
inputArtifact?: codepipeline.Artifact;
1618

1719
/**
18-
* The name of the build's output artifact
20+
* The name of the build's output artifact.
21+
*
22+
* @default an auto-generated name will be used
1923
*/
20-
artifactName?: string;
24+
outputArtifactName?: string;
2125
}
2226

2327
/**
@@ -44,7 +48,7 @@ export class PipelineBuildAction extends codepipeline.BuildAction {
4448
runOrder: props.runOrder,
4549
provider: 'CodeBuild',
4650
inputArtifact: props.inputArtifact,
47-
artifactName: props.artifactName,
51+
outputArtifactName: props.outputArtifactName,
4852
configuration: {
4953
ProjectName: props.project.projectName
5054
}

packages/@aws-cdk/aws-codebuild/lib/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ export abstract class ProjectRef extends cdk.Construct implements events.IEventR
8989
* @param props the properties of the new Action
9090
* @returns the newly created {@link PipelineBuildAction} build Action
9191
*/
92-
public addBuildToPipeline(stage: codepipeline.IStage, name: string, props: CommonPipelineBuildActionProps): PipelineBuildAction {
93-
return new PipelineBuildAction(this.parent!, name, {
92+
public addBuildToPipeline(stage: codepipeline.IStage, name: string, props: CommonPipelineBuildActionProps = {}): PipelineBuildAction {
93+
return new PipelineBuildAction(this, name, {
9494
stage,
9595
project: this,
9696
...props,

packages/@aws-cdk/aws-codecommit/README.md

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,41 @@ To add a CodeCommit Repository to your stack:
55
```ts
66
import codecommit = require('@aws-cdk/aws-codecommit');
77

8-
const repository = new codecommit.Repository(this, 'Repository' ,{
9-
repositoryName: 'MyRepositoryName'
8+
const repo = new codecommit.Repository(this, 'Repository' ,{
9+
repositoryName: 'MyRepositoryName',
10+
description: 'Some description.', // optional property
1011
});
1112
```
1213

1314
To add an SNS trigger to your repository:
1415

1516
```ts
16-
import codecommit = require('@aws-cdk/aws-codecommit');
17-
18-
const repository = new codecommit.Repository(this, 'Repository', {
19-
repositoryName: 'MyRepositoryName'
20-
});
21-
2217
// trigger is established for all repository actions on all branches by default.
23-
repository.notify('arn:aws:sns:*:123456789012:my_topic');
18+
repo.notify('arn:aws:sns:*:123456789012:my_topic');
2419
```
2520

2621
### CodePipeline
2722

2823
To use a CodeCommit Repository in a CodePipeline:
2924

3025
```ts
31-
import codecommit = require('@aws-cdk/aws-codecommit');
3226
import codepipeline = require('@aws-cdk/aws-codepipeline');
3327

34-
// see above for the details...
35-
const repository = new codecommit.Repository( // ...
36-
);
37-
3828
const pipeline = new codepipeline.Pipeline(this, 'MyPipeline', {
3929
pipelineName: 'MyPipeline',
4030
});
41-
const sourceStage = new codepipeline.Stage(this, 'Source', {
42-
pipeline,
43-
});
31+
const sourceStage = pipeline.addStage('Source');
4432
const sourceAction = new codecommit.PipelineSourceAction(this, 'CodeCommit', {
4533
stage: sourceStage,
46-
artifactName: 'SourceOutput', // name can be arbitrary
47-
repository,
34+
repository: repo,
4835
});
49-
// use sourceAction.artifact as the inputArtifact to later Actions...
5036
```
5137

5238
You can also add the Repository to the Pipeline directly:
5339

5440
```ts
5541
// equivalent to the code above:
56-
const sourceAction = repository.addToPipeline(sourceStage, 'CodeCommit', {
57-
artifactName: 'SourceOutput',
58-
});
42+
const sourceAction = repo.addToPipeline(sourceStage, 'CodeCommit');
5943
```
6044

6145
### Events

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ export interface CommonPipelineSourceActionProps extends codepipeline.CommonActi
1212
/**
1313
* The name of the source's output artifact.
1414
* Output artifacts are used by CodePipeline as inputs into other actions.
15+
*
16+
* @default a name will be auto-generated
1517
*/
16-
artifactName: string;
18+
outputArtifactName?: string;
1719

1820
/**
1921
* @default 'master'
@@ -54,7 +56,7 @@ export class PipelineSourceAction extends codepipeline.SourceAction {
5456
BranchName: props.branch || 'master',
5557
PollForSourceChanges: props.pollForSourceChanges !== undefined ? props.pollForSourceChanges : true
5658
},
57-
artifactName: props.artifactName
59+
outputArtifactName: props.outputArtifactName
5860
});
5961

6062
// https://docs.aws.amazon.com/codecommit/latest/userguide/auth-and-access-control-permissions-reference.html#aa-acp

packages/@aws-cdk/aws-codecommit/lib/repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export abstract class RepositoryRef extends cdk.Construct {
6868
* @param props the properties of the new Action
6969
* @returns the newly created {@link PipelineSourceAction}
7070
*/
71-
public addToPipeline(stage: actions.IStage, name: string, props: CommonPipelineSourceActionProps): PipelineSourceAction {
72-
return new PipelineSourceAction(this.parent!, name, {
71+
public addToPipeline(stage: actions.IStage, name: string, props: CommonPipelineSourceActionProps = {}): PipelineSourceAction {
72+
return new PipelineSourceAction(this, name, {
7373
stage,
7474
repository: this,
7575
...props,

packages/@aws-cdk/aws-codecommit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"nyc": {
4343
"lines": 30,
44-
"branches": 48
44+
"branches": 40
4545
},
4646
"keywords": [
4747
"aws",

packages/@aws-cdk/aws-codedeploy/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,9 @@ const pipeline = new codepipeline.Pipeline(this, 'MyPipeline', {
137137

138138
// add the source and build Stages to the Pipeline...
139139

140-
const deployStage = new codepipeline.Stage(this, 'Deploy', {
141-
pipeline,
142-
}));
140+
const deployStage = pipeline.addStage('Deploy');
143141
new codedeploy.PipelineDeployAction(this, 'CodeDeploy', {
144142
stage: deployStage,
145-
inputArtifact: buildAction.artifact, // taken from a build Action in a previous Stage
146143
applicationName: 'YourCodeDeployApplicationName',
147144
deploymentGroupName: 'YourCodeDeployDeploymentGroupName',
148145
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ export interface PipelineDeployActionProps extends codepipeline.CommonActionProp
2525

2626
/**
2727
* The source to use as input for deployment.
28+
*
29+
* @default CodePipeline will use the output of the last Action from a previous Stage as input
2830
*/
29-
inputArtifact: codepipeline.Artifact;
31+
inputArtifact?: codepipeline.Artifact;
3032
}
3133

3234
export class PipelineDeployAction extends codepipeline.DeployAction {

0 commit comments

Comments
 (0)