-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-image.ts
159 lines (147 loc) · 4.34 KB
/
build-image.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
aws_codebuild as codebuild,
aws_codecommit as codecommit,
aws_codepipeline as codepipeline,
aws_codepipeline_actions as actions,
aws_ecr as ecr,
aws_iam as iam,
aws_logs as logs,
aws_s3 as s3,
Duration,
RemovalPolicy,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
export interface BuildImageProps {
projectBaseName: string;
artifactBucket: s3.IBucket;
repository: codecommit.IRepository;
ecrRepository: ecr.IRepository;
sourceOutput: codepipeline.Artifact;
buildAppOutput: codepipeline.Artifact;
}
export class BuildImage {
public readonly output: codepipeline.Artifact;
public readonly action: actions.CodeBuildAction;
constructor (scope: Construct, props: BuildImageProps){
const PROJECT_BASE_NAME = props.projectBaseName;
const artifactBucket = props.artifactBucket;
const repository = props.repository;
const ecrRepository = props.ecrRepository;
const sourceOutput = props.sourceOutput;
const buildAppOutput = props.buildAppOutput;
// -----------------------------
// CodeBuild (Image)
// -----------------------------
const projectName = `${PROJECT_BASE_NAME}-Image`;
// LogGroup for CodeBuild
const logGroup = new logs.LogGroup(scope, 'LogsBuildImage', {
logGroupName: `/aws/codebuild/${projectName}`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS
});
// IAM Policy for CodeBuild
const policyDocument = new iam.PolicyDocument({
statements:[
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
's3:PutObject',
's3:GetBucketAcl',
's3:GetBucketLocation'
],
resources: [
artifactBucket.bucketArn,
`${artifactBucket.bucketArn}/*`
]
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'logs:CreateLogGroup',
'logs:CreateLogStream',
'logs:PutLogEvents'
],
resources: [
logGroup.logGroupArn,
`${logGroup.logGroupArn}:*`
]
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'codecommit:GitPull'
],
resources: [
repository.repositoryArn
]
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'ecr:CompleteLayerUpload',
'ecr:UploadLayerPart',
'ecr:InitiateLayerUpload',
'ecr:BatchCheckLayerAvailability',
'ecr:PutImage'
],
resources: [
ecrRepository.repositoryArn
]
}),
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'ecr:GetAuthorizationToken'
],
resources: [
'*'
]
}),
]
});
// IAM Role for CodeBuild
const role = new iam.Role(scope, 'RoleBuildImage', {
assumedBy: new iam.ServicePrincipal('codebuild.amazonaws.com'),
description: `Role for CodeBuild of ${PROJECT_BASE_NAME}`,
inlinePolicies: {
'policy': policyDocument
},
});
// BuildProject
const project = new codebuild.PipelineProject(scope, 'ProjectBuildImage', {
projectName: projectName,
buildSpec: codebuild.BuildSpec.fromSourceFilename('buildspec-image.yml'),
environment: {
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_3,
computeType: codebuild.ComputeType.SMALL,
privileged: true,
environmentVariables: {
'IMAGE_REPO_NAME': {
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
value: ecrRepository.repositoryName,
},
}
},
logging: {
cloudWatch: {
logGroup: logGroup
}
},
role: role,
timeout: Duration.minutes(30),
queuedTimeout: Duration.hours(8)
});
// Artifact
const output = new codepipeline.Artifact('BuildImage');
// Action
const action = new actions.CodeBuildAction({
actionName: 'CodeBuild-Image',
project: project,
input: sourceOutput,
extraInputs: [ buildAppOutput ],
outputs: [ output ],
});
this.output = output;
this.action = action;
}
}