Skip to content

Commit

Permalink
feat: add experimental cdk pipelines speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
stekern committed Apr 18, 2023
1 parent 2385131 commit 7bf724c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/cdk-pipelines/__tests__/liflig-cdk-pipeline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as assertions from "aws-cdk-lib/assertions"
import { App, CfnOutput, Stack, Stage } from "aws-cdk-lib"
import { LifligCdkPipeline } from "../liflig-cdk-pipeline"
import { FEATURE_FLAG_CDK_PIPELINES_SPEED_UP } from "../../feature-flags"

test("liflig-cdk-pipeline-with-feature-flag", () => {
const app = new App({
context: {
[FEATURE_FLAG_CDK_PIPELINES_SPEED_UP]: true,
},
})

const stage = new Stage(app, "Stage")
const stack = new Stack(stage, "Stack")
new CfnOutput(stack, "ExampleOutput", {
value: "hello world",
})

const pipelineStack = new Stack(app, "PipelineStack")

const pipeline = new LifligCdkPipeline(pipelineStack, "Pipeline", {
pipelineName: "test-pipeline",
sourceType: "cdk-source",
})

pipeline.cdkPipeline.addStage(stage)
const template = assertions.Template.fromStack(pipelineStack)

// Assert that S3 polling is deactivated
template.hasResourceProperties("AWS::CodePipeline::Pipeline", {
Stages: assertions.Match.arrayWith([
assertions.Match.objectLike({
Actions: assertions.Match.arrayWith([
assertions.Match.objectLike({
ActionTypeId: assertions.Match.objectEquals({
Category: "Source",
Owner: "AWS",
Provider: "S3",
Version: "1",
}),
Configuration: assertions.Match.objectLike({
PollForSourceChanges: false,
}),
}),
]),
}),
]),
})

// Assert that there are no actions that creates changesets
template.hasResourceProperties("AWS::CodePipeline::Pipeline", {
Stages: assertions.Match.not(
assertions.Match.arrayWith([
assertions.Match.objectLike({
Actions: assertions.Match.arrayWith([
assertions.Match.objectLike({
ActionTypeId: assertions.Match.objectEquals({
Category: "Deploy",
Owner: "AWS",
Provider: "CloudFormation",
Version: "1",
}),
Configuration: assertions.Match.objectLike({
ActionMode: "CHANGE_SET_REPLACE",
}),
}),
]),
}),
]),
),
})
// Assert that there's an EventBridge rule set up to trigger the pipeline
template.hasResource("AWS::Events::Rule", {})
})
32 changes: 32 additions & 0 deletions src/cdk-pipelines/liflig-cdk-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as codepipeline from "aws-cdk-lib/aws-codepipeline"
import * as codepipelineActions from "aws-cdk-lib/aws-codepipeline-actions"
import * as iam from "aws-cdk-lib/aws-iam"
import * as lambda from "aws-cdk-lib/aws-lambda"
import * as events from "aws-cdk-lib/aws-events"
import * as targets from "aws-cdk-lib/aws-events-targets"
import * as s3 from "aws-cdk-lib/aws-s3"
import * as cdk from "aws-cdk-lib"
import * as pipelines from "aws-cdk-lib/pipelines"
Expand All @@ -14,6 +16,10 @@ import {
CloudAssemblyLookupUserParameters,
} from "./cloud-assembly-lookup-handler"
import { SlackNotification, SlackNotificationProps } from "./slack-notification"
import {
FeatureFlags,
FEATURE_FLAG_CDK_PIPELINES_SPEED_UP,
} from "../feature-flags"

export interface LifligCdkPipelineProps {
/**
Expand Down Expand Up @@ -168,6 +174,11 @@ export class LifligCdkPipeline extends constructs.Construct {
new codepipelineActions.S3SourceAction({
actionName: "source",
bucket: artifactsBucket,
trigger: FeatureFlags.of(scope).isEnabled(
FEATURE_FLAG_CDK_PIPELINES_SPEED_UP,
)
? codepipelineActions.S3Trigger.NONE
: undefined,
bucketKey: LifligCdkPipeline.pipelineS3TriggerKey(
props.pipelineName,
),
Expand All @@ -180,8 +191,29 @@ export class LifligCdkPipeline extends constructs.Construct {
restartExecutionOnUpdate: true,
})

if (FeatureFlags.of(scope).isEnabled(FEATURE_FLAG_CDK_PIPELINES_SPEED_UP)) {
new events.Rule(this, "PipelineTrigger", {
eventPattern: {
source: ["aws.s3"],
detailType: ["Object Created"],
detail: {
bucket: {
name: [artifactsBucket.bucketName],
},
object: {
key: [LifligCdkPipeline.pipelineS3TriggerKey(props.pipelineName)],
},
},
},
targets: [new targets.CodePipeline(this.codePipeline)],
})
}

this.cdkPipeline = new pipelines.CodePipeline(this, "CdkPipeline", {
synth,
useChangeSets: !FeatureFlags.of(scope).isEnabled(
FEATURE_FLAG_CDK_PIPELINES_SPEED_UP,
),
codePipeline: this.codePipeline,
})
}
Expand Down

0 comments on commit 7bf724c

Please sign in to comment.