-
Notifications
You must be signed in to change notification settings - Fork 12
/
code-pipeline-stack.ts
136 lines (128 loc) · 4.28 KB
/
code-pipeline-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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import cdk = require('@aws-cdk/cdk');
import ecr = require('@aws-cdk/aws-ecr');
import codebuild = require('@aws-cdk/aws-codebuild');
// import ecs = require('@aws-cdk/aws-ecs');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import codepipeline_actions = require('@aws-cdk/aws-codepipeline-actions');
import { PipelineContainerImage } from "./pipeline-container-image";
export class CodePipelineStack extends cdk.Stack {
public readonly repository: ecr.Repository;
public readonly builtImage: PipelineContainerImage;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, {
...props,
autoDeploy: false,
});
this.repository = new ecr.Repository(this, 'EcrRepo');
this.builtImage = new PipelineContainerImage(this.repository);
const sourceOutput = new codepipeline.Artifact();
const sourceAction = new codepipeline_actions.GitHubSourceAction({
actionName: 'GitHub',
owner: 'rix0rrr',
repo: 'cdk-ecs-demo',
oauthToken: cdk.SecretValue.secretsManager('my-github-token'),
output: sourceOutput,
trigger: codepipeline_actions.GitHubTrigger.Poll,
});
const cdkBuild = new codebuild.PipelineProject(this, 'CdkBuildProject', {
environment: {
buildImage: codebuild.LinuxBuildImage.UBUNTU_14_04_NODEJS_8_11_0,
},
buildSpec: {
version: '0.2',
phases: {
install: {
commands: [
'npm install',
],
},
build: {
commands: [
'npm run build',
'npm run cdk synth ProdHttpServiceStack -- -o .',
],
},
},
artifacts: {
'base-directory': 'http-service',
files: 'ProdHttpServiceStack.template.yaml',
},
},
});
const dockerBuild = new codebuild.PipelineProject(this, 'DockerCodeBuildProject', {
environment: {
buildImage: codebuild.LinuxBuildImage.UBUNTU_14_04_DOCKER_17_09_0,
privileged: true,
},
buildSpec: {
version: '0.2',
phases: {
pre_build: {
commands: '$(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)',
},
build: {
commands: 'docker build -t $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION demo-http-server',
},
post_build: {
commands: [
'docker push $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION',
`printf '{ "imageTag": "'$CODEBUILD_RESOLVED_SOURCE_VERSION'" }' > imageTag.json`,
],
},
},
artifacts: {
files: 'imageTag.json',
},
},
environmentVariables: {
'REPOSITORY_URI': {
value: this.repository.repositoryUri,
},
},
});
this.repository.grantPullPush(dockerBuild);
const dockerBuildOutput = new codepipeline.Artifact();
const cdkBuildOutput = new codepipeline.Artifact();
new codepipeline.Pipeline(this, 'Pipeline', {
stages: [
{
name: 'Source',
actions: [sourceAction],
},
{
name: 'Build',
actions: [
new codepipeline_actions.CodeBuildAction({
actionName: 'DockerBuild',
project: dockerBuild,
input: sourceOutput,
output: dockerBuildOutput,
}),
new codepipeline_actions.CodeBuildAction({
actionName: 'CdkBuild',
project: cdkBuild,
input: sourceOutput,
output: cdkBuildOutput,
})
],
},
{
name: 'Deploy',
actions: [
new codepipeline_actions.CloudFormationCreateUpdateStackAction({
actionName: 'CFN_Deploy',
stackName: 'ProdHttpServiceStack',
templatePath: cdkBuildOutput.atPath('ProdHttpServiceStack.template.yaml'),
adminPermissions: true,
parameterOverrides: {
[this.builtImage.paramName]: dockerBuildOutput.getParam('imageTag.json', 'imageTag'),
// ...this.builtImage.assing(dockerBuildOutput)
},
extraInputs: [dockerBuildOutput],
}),
],
},
],
});
}
}