Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codepipeline): allow creation of pipelines without source trigger #2332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/app-delivery/test/integ.cicd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const source = new cpactions.GitHubSourceAction({
owner: 'awslabs',
repo: 'aws-cdk',
oauthToken: cdk.SecretValue.plainText('DummyToken'),
pollForSourceChanges: true,
trigger: cpactions.GitHubTrigger.Poll,
output: sourceOutput,
});
pipeline.addStage({
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codepipeline-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const sourceAction = new codepipeline_actions.GitHubSourceAction({
oauthToken: token.value,
output: sourceOutput,
branch: 'develop', // default: 'master'
trigger: codepipeline_actions.GitHubTrigger.Poll // default: 'WebHook', 'None' is also possible for no Source trigger
});
pipeline.addStage({
name: 'Source',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import codepipeline = require('@aws-cdk/aws-codepipeline');
import { SecretValue } from '@aws-cdk/cdk';
import { sourceArtifactBounds } from '../common';

/**
* If and how the GitHub source action should be triggered
*/
export enum GitHubTrigger {
None = 'None',
Poll = 'Poll',
WebHook = 'WebHook',
}

/**
* Construction properties of the {@link GitHubSourceAction GitHub source action}.
*/
Expand Down Expand Up @@ -39,12 +48,15 @@ export interface GitHubSourceActionProps extends codepipeline.CommonActionProps
readonly oauthToken: SecretValue;

/**
* Whether AWS CodePipeline should poll for source changes.
* If this is `false`, the Pipeline will use a webhook to detect source changes instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the webhook comment (of course, adjust it to the current situation).

* How AWS CodePipeline should be triggered
*
* With the default value "WebHook", a webhook is created in GitHub that triggers the action
* With "Poll", CodePipeline periodically checks the source for changes
* With "None", the action is not triggered through changes in the source
*
* @default false
* @default GitHubTrigger.WebHook
*/
readonly pollForSourceChanges?: boolean;
readonly trigger?: GitHubTrigger;
}

/**
Expand All @@ -66,15 +78,15 @@ export class GitHubSourceAction extends codepipeline.Action {
Repo: props.repo,
Branch: props.branch || "master",
OAuthToken: props.oauthToken.toString(),
PollForSourceChanges: props.pollForSourceChanges || false,
PollForSourceChanges: props.trigger === GitHubTrigger.Poll,
},
});

this.props = props;
}

protected bind(info: codepipeline.ActionBind): void {
if (!this.props.pollForSourceChanges) {
if (!this.props.trigger || this.props.trigger === GitHubTrigger.WebHook) {
new codepipeline.CfnWebhook(info.scope, 'WebhookResource', {
authentication: 'GITHUB_HMAC',
authenticationConfiguration: {
Expand Down
122 changes: 121 additions & 1 deletion packages/@aws-cdk/aws-codepipeline-actions/test/test.pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource, haveResourceLike, SynthUtils } from '@aws-cdk/assert';
import { expect, haveResource, haveResourceLike, not, SynthUtils } from '@aws-cdk/assert';
import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');
import codepipeline = require('@aws-cdk/aws-codepipeline');
Expand Down Expand Up @@ -67,6 +67,124 @@ export = {
test.done();
},

'pipeline with GitHub source with poll trigger'(test: Test) {
const stack = new Stack();

const secret = new CfnParameter(stack, 'GitHubToken', { type: 'String', default: 'my-token' });

const p = new codepipeline.Pipeline(stack, 'P');

p.addStage({
name: 'Source',
actions: [
new cpactions.GitHubSourceAction({
actionName: 'GH',
runOrder: 8,
output: new codepipeline.Artifact('A'),
branch: 'branch',
oauthToken: SecretValue.plainText(secret.stringValue),
owner: 'foo',
repo: 'bar',
trigger: cpactions.GitHubTrigger.Poll
}),
],
});

p.addStage({
name: 'Two',
actions: [
new cpactions.ManualApprovalAction({ actionName: 'Boo' }),
],
});

expect(stack).to(not(haveResourceLike('AWS::CodePipeline::Webhook')));

expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
"Stages": [
{
"Actions": [
{
"Configuration": {
"PollForSourceChanges": true
},
"Name": "GH"
}
],
"Name": "Source"
},
{
"Actions": [
{
"Name": "Boo",
}
],
"Name": "Two"
}
]
}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you're using haveResourceLike, you don't need all these extra properties. Focus on just the ones important for your test, something like:

    expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
      "Stages": [
        {
          "Actions": [
            {
              "Name": "GH",
              "Configuration": {
                "PollForSourceChanges": true
              },
            }
          ],
          "Name": "Source"
        },
        {
          "Actions": [
            {
              "Name": "Boo"
            }
          ],
          "Name": "Two"
        }
      ]
    }));


test.done();
},

'pipeline with GitHub source without triggers'(test: Test) {
const stack = new Stack();

const secret = new CfnParameter(stack, 'GitHubToken', { type: 'String', default: 'my-token' });

const p = new codepipeline.Pipeline(stack, 'P');

p.addStage({
name: 'Source',
actions: [
new cpactions.GitHubSourceAction({
actionName: 'GH',
runOrder: 8,
output: new codepipeline.Artifact('A'),
branch: 'branch',
oauthToken: SecretValue.plainText(secret.stringValue),
owner: 'foo',
repo: 'bar',
trigger: cpactions.GitHubTrigger.None
}),
],
});

p.addStage({
name: 'Two',
actions: [
new cpactions.ManualApprovalAction({ actionName: 'Boo' }),
],
});

expect(stack).to(not(haveResourceLike('AWS::CodePipeline::Webhook')));

expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
"Stages": [
{
"Actions": [
{
"Configuration": {
"PollForSourceChanges": false
},
"Name": "GH"
}
],
"Name": "Source"
},
{
"Actions": [
{
"Name": "Boo",
}
],
"Name": "Two"
}
]
}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here as above.


test.done();
},

'github action uses ThirdParty owner'(test: Test) {
const stack = new Stack();

Expand Down Expand Up @@ -96,6 +214,8 @@ export = {
],
});

expect(stack).to(haveResourceLike('AWS::CodePipeline::Webhook'));

expect(stack).to(haveResourceLike('AWS::CodePipeline::Pipeline', {
"ArtifactStore": {
"Location": {
Expand Down