Skip to content

Commit ed39a8c

Browse files
Sander Knapeskinny85
authored andcommitted
feat(codepipeline): allow creation of GitHub Pipelines without source trigger (#2332)
BREAKING CHANGE: the `pollForSourceChanges` property in `GitHubSourceAction` has been renamed to `trigger`, and its type changed from a `boolean` to an enum.
1 parent c2d4358 commit ed39a8c

File tree

4 files changed

+141
-8
lines changed

4 files changed

+141
-8
lines changed

packages/@aws-cdk/app-delivery/test/integ.cicd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const source = new cpactions.GitHubSourceAction({
1919
owner: 'awslabs',
2020
repo: 'aws-cdk',
2121
oauthToken: cdk.SecretValue.plainText('DummyToken'),
22-
pollForSourceChanges: true,
22+
trigger: cpactions.GitHubTrigger.Poll,
2323
output: sourceOutput,
2424
});
2525
pipeline.addStage({

packages/@aws-cdk/aws-codepipeline-actions/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const sourceAction = new codepipeline_actions.GitHubSourceAction({
5050
oauthToken: token.value,
5151
output: sourceOutput,
5252
branch: 'develop', // default: 'master'
53+
trigger: codepipeline_actions.GitHubTrigger.Poll // default: 'WebHook', 'None' is also possible for no Source trigger
5354
});
5455
pipeline.addStage({
5556
name: 'Source',

packages/@aws-cdk/aws-codepipeline-actions/lib/github/source-action.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import codepipeline = require('@aws-cdk/aws-codepipeline');
22
import { SecretValue } from '@aws-cdk/cdk';
33
import { sourceArtifactBounds } from '../common';
44

5+
/**
6+
* If and how the GitHub source action should be triggered
7+
*/
8+
export enum GitHubTrigger {
9+
None = 'None',
10+
Poll = 'Poll',
11+
WebHook = 'WebHook',
12+
}
13+
514
/**
615
* Construction properties of the {@link GitHubSourceAction GitHub source action}.
716
*/
@@ -39,12 +48,15 @@ export interface GitHubSourceActionProps extends codepipeline.CommonActionProps
3948
readonly oauthToken: SecretValue;
4049

4150
/**
42-
* Whether AWS CodePipeline should poll for source changes.
43-
* If this is `false`, the Pipeline will use a webhook to detect source changes instead.
51+
* How AWS CodePipeline should be triggered
52+
*
53+
* With the default value "WebHook", a webhook is created in GitHub that triggers the action
54+
* With "Poll", CodePipeline periodically checks the source for changes
55+
* With "None", the action is not triggered through changes in the source
4456
*
45-
* @default false
57+
* @default GitHubTrigger.WebHook
4658
*/
47-
readonly pollForSourceChanges?: boolean;
59+
readonly trigger?: GitHubTrigger;
4860
}
4961

5062
/**
@@ -66,15 +78,15 @@ export class GitHubSourceAction extends codepipeline.Action {
6678
Repo: props.repo,
6779
Branch: props.branch || "master",
6880
OAuthToken: props.oauthToken.toString(),
69-
PollForSourceChanges: props.pollForSourceChanges || false,
81+
PollForSourceChanges: props.trigger === GitHubTrigger.Poll,
7082
},
7183
});
7284

7385
this.props = props;
7486
}
7587

7688
protected bind(info: codepipeline.ActionBind): void {
77-
if (!this.props.pollForSourceChanges) {
89+
if (!this.props.trigger || this.props.trigger === GitHubTrigger.WebHook) {
7890
new codepipeline.CfnWebhook(info.scope, 'WebhookResource', {
7991
authentication: 'GITHUB_HMAC',
8092
authenticationConfiguration: {

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

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, haveResource, haveResourceLike, SynthUtils } from '@aws-cdk/assert';
1+
import { expect, haveResource, haveResourceLike, not, SynthUtils } from '@aws-cdk/assert';
22
import codebuild = require('@aws-cdk/aws-codebuild');
33
import codecommit = require('@aws-cdk/aws-codecommit');
44
import codepipeline = require('@aws-cdk/aws-codepipeline');
@@ -67,6 +67,124 @@ export = {
6767
test.done();
6868
},
6969

70+
'pipeline with GitHub source with poll trigger'(test: Test) {
71+
const stack = new Stack();
72+
73+
const secret = new CfnParameter(stack, 'GitHubToken', { type: 'String', default: 'my-token' });
74+
75+
const p = new codepipeline.Pipeline(stack, 'P');
76+
77+
p.addStage({
78+
name: 'Source',
79+
actions: [
80+
new cpactions.GitHubSourceAction({
81+
actionName: 'GH',
82+
runOrder: 8,
83+
output: new codepipeline.Artifact('A'),
84+
branch: 'branch',
85+
oauthToken: SecretValue.plainText(secret.stringValue),
86+
owner: 'foo',
87+
repo: 'bar',
88+
trigger: cpactions.GitHubTrigger.Poll
89+
}),
90+
],
91+
});
92+
93+
p.addStage({
94+
name: 'Two',
95+
actions: [
96+
new cpactions.ManualApprovalAction({ actionName: 'Boo' }),
97+
],
98+
});
99+
100+
expect(stack).to(not(haveResourceLike('AWS::CodePipeline::Webhook')));
101+
102+
expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
103+
"Stages": [
104+
{
105+
"Actions": [
106+
{
107+
"Configuration": {
108+
"PollForSourceChanges": true
109+
},
110+
"Name": "GH"
111+
}
112+
],
113+
"Name": "Source"
114+
},
115+
{
116+
"Actions": [
117+
{
118+
"Name": "Boo",
119+
}
120+
],
121+
"Name": "Two"
122+
}
123+
]
124+
}));
125+
126+
test.done();
127+
},
128+
129+
'pipeline with GitHub source without triggers'(test: Test) {
130+
const stack = new Stack();
131+
132+
const secret = new CfnParameter(stack, 'GitHubToken', { type: 'String', default: 'my-token' });
133+
134+
const p = new codepipeline.Pipeline(stack, 'P');
135+
136+
p.addStage({
137+
name: 'Source',
138+
actions: [
139+
new cpactions.GitHubSourceAction({
140+
actionName: 'GH',
141+
runOrder: 8,
142+
output: new codepipeline.Artifact('A'),
143+
branch: 'branch',
144+
oauthToken: SecretValue.plainText(secret.stringValue),
145+
owner: 'foo',
146+
repo: 'bar',
147+
trigger: cpactions.GitHubTrigger.None
148+
}),
149+
],
150+
});
151+
152+
p.addStage({
153+
name: 'Two',
154+
actions: [
155+
new cpactions.ManualApprovalAction({ actionName: 'Boo' }),
156+
],
157+
});
158+
159+
expect(stack).to(not(haveResourceLike('AWS::CodePipeline::Webhook')));
160+
161+
expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
162+
"Stages": [
163+
{
164+
"Actions": [
165+
{
166+
"Configuration": {
167+
"PollForSourceChanges": false
168+
},
169+
"Name": "GH"
170+
}
171+
],
172+
"Name": "Source"
173+
},
174+
{
175+
"Actions": [
176+
{
177+
"Name": "Boo",
178+
}
179+
],
180+
"Name": "Two"
181+
}
182+
]
183+
}));
184+
185+
test.done();
186+
},
187+
70188
'github action uses ThirdParty owner'(test: Test) {
71189
const stack = new Stack();
72190

@@ -96,6 +214,8 @@ export = {
96214
],
97215
});
98216

217+
expect(stack).to(haveResourceLike('AWS::CodePipeline::Webhook'));
218+
99219
expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
100220
"ArtifactStore": {
101221
"Location": {

0 commit comments

Comments
 (0)