-
Notifications
You must be signed in to change notification settings - Fork 1
/
06-cdk-pipeline-deploy-to-environment-stack.ts
101 lines (92 loc) · 3.26 KB
/
06-cdk-pipeline-deploy-to-environment-stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as codepipeline from "@aws-cdk/aws-codepipeline";
import * as codepipeline_actions from "@aws-cdk/aws-codepipeline-actions";
import { Construct, SecretValue, Stack, StackProps } from "@aws-cdk/core";
import { CdkPipeline, SimpleSynthAction } from "@aws-cdk/pipelines";
import * as codebuild from "@aws-cdk/aws-codebuild";
import * as s3 from "@aws-cdk/aws-s3";
export interface CdkPipelineStackProps extends StackProps {
GitHubOrganization: string;
GitHubRepo: string;
GitHubBranch: string;
GitHubPathToProject: string;
GitHubPathArtifactName: string;
}
/**
* The stack that defines the application pipeline
*/
export class CdkPipelineStack extends Stack {
constructor(scope: Construct, id: string, props: CdkPipelineStackProps) {
super(scope, id, props);
const artifactsBucket = new s3.Bucket(this, "ArtifactsBucket", {
versioned: true, // required for CodePipeline source
});
const build = new codebuild.Project(this, "Build", {
source: codebuild.Source.gitHub({
owner: props.GitHubOrganization,
repo: props.GitHubRepo,
webhook: true,
webhookFilters: [
codebuild.FilterGroup.inEventOf(codebuild.EventAction.PUSH)
.andBranchIs(props.GitHubBranch)
.andFilePathIs(props.GitHubPathToProject),
],
}),
artifacts: codebuild.Artifacts.s3({
bucket: artifactsBucket,
includeBuildId: false,
packageZip: true,
path: "code",
identifier: "RepoArtifact",
}),
environmentVariables: {
GitHubPathToProject: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: props.GitHubPathToProject,
},
ArtifactName: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: props.GitHubPathArtifactName,
},
ArtifactsBucket: {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: artifactsBucket.bucketName,
},
},
buildSpec: codebuild.BuildSpec.fromObject({
version: "0.2",
phases: {
build: {
commands: [
"cd ${CODEBUILD_SRC_DIR}",
"zip -qr ${ArtifactName} ./${GitHubPathToProject}",
"aws s3 cp ${CODEBUILD_SRC_DIR}/${ArtifactName} s3://${ArtifactsBucket}/code/${ArtifactName}",
],
},
},
}),
});
const sourceArtifact = new codepipeline.Artifact();
const cloudAssemblyArtifact = new codepipeline.Artifact();
const pipeline = new CdkPipeline(this, "Pipeline", {
// The pipeline name
pipelineName: "06CDKPipelineToEnvironment",
cloudAssemblyArtifact,
// Where the source can be found
sourceAction: new codepipeline_actions.S3SourceAction({
actionName: "S3Source",
bucket: artifactsBucket,
bucketKey: `code/${props.GitHubPathArtifactName}`,
output: sourceArtifact,
}),
// How it will be built and synthesized
synthAction: SimpleSynthAction.standardNpmSynth({
sourceArtifact,
cloudAssemblyArtifact,
// We need a build step to compile the TypeScript Lambda
buildCommand: "ls -al && npm run build",
}),
});
// This is where we add the application stages
// ...
}
}