From 9454490a094c2aad60081478115d5b61e2e6d652 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Fri, 2 Nov 2018 17:47:02 -0700 Subject: [PATCH] feat(aws-codepipeline): switch to webhooks instead of polling by default for the GitHub source Action. --- .../@aws-cdk/app-delivery/test/integ.cicd.ts | 1 + .../test/test.pipeline-actions.ts | 4 ++- .../aws-codepipeline-api/lib/action.ts | 5 ++++ .../lib/github-source-action.ts | 27 ++++++++++++++++--- .../aws-codepipeline/test/test.pipeline.ts | 2 +- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/app-delivery/test/integ.cicd.ts b/packages/@aws-cdk/app-delivery/test/integ.cicd.ts index bf6948588f17e..3f7f81636f052 100644 --- a/packages/@aws-cdk/app-delivery/test/integ.cicd.ts +++ b/packages/@aws-cdk/app-delivery/test/integ.cicd.ts @@ -14,6 +14,7 @@ const source = new code.GitHubSourceAction(stack, 'GitHub', { owner: 'awslabs', repo: 'aws-cdk', oauthToken: new cdk.Secret('DummyToken'), + pollForSourceChanges: true, }); new cicd.PipelineDeployStackAction(stack, 'DeployStack', { stage: pipeline.addStage('Deploy'), diff --git a/packages/@aws-cdk/aws-cloudformation/test/test.pipeline-actions.ts b/packages/@aws-cdk/aws-cloudformation/test/test.pipeline-actions.ts index 978303b4f308f..63677172d192a 100644 --- a/packages/@aws-cdk/aws-cloudformation/test/test.pipeline-actions.ts +++ b/packages/@aws-cdk/aws-cloudformation/test/test.pipeline-actions.ts @@ -275,11 +275,13 @@ function _stackArn(stackName: string): string { } class PipelineDouble implements cpapi.IPipeline { + public readonly pipelineName: string; public readonly pipelineArn: string; public readonly role: iam.Role; constructor({ pipelineName, role }: { pipelineName?: string, role: iam.Role }) { - this.pipelineArn = cdk.ArnUtils.fromComponents({ service: 'codepipeline', resource: 'pipeline', resourceName: pipelineName || 'TestPipeline' }); + this.pipelineName = pipelineName || 'TestPipeline'; + this.pipelineArn = cdk.ArnUtils.fromComponents({ service: 'codepipeline', resource: 'pipeline', resourceName: this.pipelineName }); this.role = role; } diff --git a/packages/@aws-cdk/aws-codepipeline-api/lib/action.ts b/packages/@aws-cdk/aws-codepipeline-api/lib/action.ts index 5b27dda440972..c3ac1528f9448 100644 --- a/packages/@aws-cdk/aws-codepipeline-api/lib/action.ts +++ b/packages/@aws-cdk/aws-codepipeline-api/lib/action.ts @@ -73,6 +73,11 @@ export interface IInternalStage { * so this interface can be used as a Target for CloudWatch Events. */ export interface IPipeline extends events.IEventRuleTarget { + /** + * The name of the Pipeline. + */ + readonly pipelineName: string; + /** * The ARN of the Pipeline. */ diff --git a/packages/@aws-cdk/aws-codepipeline/lib/github-source-action.ts b/packages/@aws-cdk/aws-codepipeline/lib/github-source-action.ts index 953c497d7008e..49b77f3e98c84 100644 --- a/packages/@aws-cdk/aws-codepipeline/lib/github-source-action.ts +++ b/packages/@aws-cdk/aws-codepipeline/lib/github-source-action.ts @@ -1,5 +1,6 @@ import actions = require('@aws-cdk/aws-codepipeline-api'); import cdk = require('@aws-cdk/cdk'); +import { cloudformation } from './codepipeline.generated'; /** * Construction properties of the {@link GitHubSourceAction GitHub source action}. @@ -44,9 +45,10 @@ export interface GitHubSourceActionProps extends actions.CommonActionProps, oauthToken: cdk.Secret; /** - * Whether or not AWS CodePipeline should poll for source changes + * Whether AWS CodePipeline should poll for source changes. + * If this is `false`, the Pipeline will use a webhook to detect source changes instead. * - * @default true + * @default false */ pollForSourceChanges?: boolean; } @@ -66,9 +68,28 @@ export class GitHubSourceAction extends actions.SourceAction { Repo: props.repo, Branch: props.branch || "master", OAuthToken: props.oauthToken, - PollForSourceChanges: props.pollForSourceChanges || true + PollForSourceChanges: props.pollForSourceChanges || false, }, outputArtifactName: props.outputArtifactName }); + + if (!props.pollForSourceChanges) { + new cloudformation.WebhookResource(this, 'WebhookResource', { + authentication: 'GITHUB_HMAC', + authenticationConfiguration: { + secretToken: props.oauthToken, + }, + filters: [ + { + jsonPath: '$.ref', + matchEquals: 'refs/heads/{Branch}', + }, + ], + targetAction: this.id, + targetPipeline: props.stage.pipeline.pipelineName, + targetPipelineVersion: 1, + registerWithThirdParty: true, + }); + } } } diff --git a/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts b/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts index c5ad9900fd361..301995f1dcd5b 100644 --- a/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts +++ b/packages/@aws-cdk/aws-codepipeline/test/test.pipeline.ts @@ -93,7 +93,7 @@ export = { "OAuthToken": { "Ref": "GitHubTokenParameterBB166B9D" }, - "PollForSourceChanges": true + "PollForSourceChanges": false }, "InputArtifacts": [], "Name": "GH",