-
Notifications
You must be signed in to change notification settings - Fork 9
/
buildspecs.ts
144 lines (128 loc) · 5.57 KB
/
buildspecs.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
137
138
139
140
141
142
143
144
import codebuild = require('@aws-cdk/aws-codebuild');
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { PipelineProject } from '@aws-cdk/aws-codebuild';
import * as ecr from '@aws-cdk/aws-ecr';
import * as eks from '@aws-cdk/aws-eks';
export function codeToECRspec (scope: cdk.Construct, apprepo: string) :PipelineProject {
const buildForECR = new codebuild.PipelineProject(scope, `build-to-ecr`, {
projectName: `build-to-ecr`,
environment: {
buildImage: codebuild.LinuxBuildImage.UBUNTU_14_04_DOCKER_18_09_0,
privileged: true
},
environmentVariables: { 'ECR_REPO_URI': {
value: apprepo
} },
buildSpec: codebuild.BuildSpec.fromObject({
version: "0.2",
phases: {
pre_build: {
commands: [
'env', `$(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)`,
'IMAGE_TAG=$CODEBUILD_RESOLVED_SOURCE_VERSION'
]
},
build: {
commands: [
'docker build -t $ECR_REPO_URI:latest .',
'docker tag $ECR_REPO_URI:latest $ECR_REPO_URI:$IMAGE_TAG'
]
},
post_build: {
commands: [
'docker push $ECR_REPO_URI:latest',
'docker push $ECR_REPO_URI:$IMAGE_TAG'
]
}
}
})
});
return buildForECR;
}
export function deployToEKSspec (scope: cdk.Construct, region: string, cluster: eks.Cluster, apprepo: ecr.IRepository, roleToAssume: iam.Role) :PipelineProject {
const deployBuildSpec = new codebuild.PipelineProject(scope, `deploy-to-eks-${region}`, {
environment: {
buildImage: codebuild.LinuxBuildImage.fromAsset(scope, `custom-image-for-eks-${region}`, {
directory: './utils/buildimage'
})
},
environmentVariables: {
'REGION': { value: region },
'CLUSTER_NAME': { value: cluster.clusterName },
'ECR_REPO_URI': { value: apprepo.repositoryUri } ,
},
buildSpec: codebuild.BuildSpec.fromObject({
version: "0.2",
phases: {
install: {
commands: [
'env',
'export TAG=${CODEBUILD_RESOLVED_SOURCE_VERSION}',
'/usr/local/bin/entrypoint.sh']
},
build: {
commands: [
`CREDENTIALS=$(aws sts assume-role --role-arn "${roleToAssume.roleArn}" --role-session-name codebuild-cdk)`,
`export AWS_ACCESS_KEY_ID="$(echo \${CREDENTIALS} | jq -r '.Credentials.AccessKeyId')"`,
`export AWS_SECRET_ACCESS_KEY="$(echo \${CREDENTIALS} | jq -r '.Credentials.SecretAccessKey')"`,
`export AWS_SESSION_TOKEN="$(echo \${CREDENTIALS} | jq -r '.Credentials.SessionToken')"`,
`export AWS_EXPIRATION=$(echo \${CREDENTIALS} | jq -r '.Credentials.Expiration')`,
`sed -i 's@CONTAINER_IMAGE@'"$ECR_REPO_URI:$TAG"'@' app-deployment.yaml`,
'kubectl apply -f app-deployment.yaml'
]
}
}})
});
deployBuildSpec.addToRolePolicy(new iam.PolicyStatement({
actions: ['eks:DescribeCluster'],
resources: [`*`],
}));
deployBuildSpec.addToRolePolicy(new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
resources: [roleToAssume.roleArn]
}))
return deployBuildSpec;
}
export function deployAllPoliciesSpec (scope: cdk.Construct, region: string, cluster: eks.Cluster, roleToAssume: iam.Role) :PipelineProject {
const deployBuildSpec = new codebuild.PipelineProject(scope, `deploy-to-eks-${region}`, {
environment: {
buildImage: codebuild.LinuxBuildImage.fromAsset(scope, `custom-image-for-eks-${region}`, {
directory: './utils/buildimage'
})
},
environmentVariables: {
'REGION': { value: region },
'CLUSTER_NAME': { value: cluster.clusterName }
},
buildSpec: codebuild.BuildSpec.fromObject({
version: "0.2",
phases: {
install: {
commands: [
'env',
'export TAG=${CODEBUILD_RESOLVED_SOURCE_VERSION}',
'/usr/local/bin/entrypoint.sh']
},
build: {
commands: [
`CREDENTIALS=$(aws sts assume-role --role-arn "${roleToAssume.roleArn}" --role-session-name codebuild-cdk)`,
`export AWS_ACCESS_KEY_ID="$(echo \${CREDENTIALS} | jq -r '.Credentials.AccessKeyId')"`,
`export AWS_SECRET_ACCESS_KEY="$(echo \${CREDENTIALS} | jq -r '.Credentials.SecretAccessKey')"`,
`export AWS_SESSION_TOKEN="$(echo \${CREDENTIALS} | jq -r '.Credentials.SessionToken')"`,
`export AWS_EXPIRATION=$(echo \${CREDENTIALS} | jq -r '.Credentials.Expiration')`,
'kubectl apply -f ./'
]
}
}})
});
deployBuildSpec.addToRolePolicy(new iam.PolicyStatement({
actions: ['eks:DescribeCluster'],
resources: [`*`],
}));
deployBuildSpec.addToRolePolicy(new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
resources: [roleToAssume.roleArn]
}))
return deployBuildSpec;
}