-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
337 lines (299 loc) · 9.38 KB
/
template.yml
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
Resources:
# Actual pipeline
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
# Artifact store location
ArtifactStore:
Type: S3
Location: !Join
- ""
- - Ref: BucketName
- "-artifacts"
# Add the IAM role to the bucket
RoleArn: !GetAtt [PipelineRole, Arn]
# Stages
Stages:
# Stage for loading code from Github
- Name: Source
Actions:
# Determine the action type. See https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html#actions-valid-providers
- ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeStarSourceConnection
Version: "1"
# Connect the pipeline to the git provider. The CodeStar connection could, as of right now, be either Github or Bitbucket. See documentation for latest info: https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html
Configuration:
ConnectionArn:
Ref: CodeStarConnection
FullRepositoryId: !Join
- ""
- - Ref: RepositoryOwner
- "/"
- Ref: RepositoryName
BranchName:
Ref: BranchName
OutputArtifactFormat: "CODE_ZIP"
# Name the output of this action. Note that this is the name that will be referenced further down the pipeline
OutputArtifacts:
- Name: SourceArtifact
# Action name
Name: Get_source
# Stage for building the code loaded from Github
- Name: Build
Actions:
- ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: 1
# Connect the build project
Configuration:
ProjectName: !Join
- ""
- - Ref: PipelineName
- "-builder"
PrimarySource: SourceArtifact
# Get the source code artifact
InputArtifacts:
- Name: SourceArtifact
# Output the build files
OutputArtifacts:
- Name: BuildFiles
# Action name
Name: Build_code
# Stage for deploying the build
- Name: Deploy
Actions:
- ActionTypeId:
Category: Deploy
Owner: AWS
Provider: S3
Version: 1
# Configure where to deploy
Configuration:
BucketName:
Ref: BucketName
Extract: True
# Get the artifact produced in the above stage
InputArtifacts:
- Name: BuildFiles
# Action name
Name: Deploy_build
# Pipeline name
Name:
Ref: PipelineName
# Tags
Tags:
- Key: Type
Value: Learning
# CodeBuild project
Builder:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: CODEPIPELINE
# Build environment
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0
Type: LINUX_CONTAINER
# Environment variable used for clearing the bucket before deployment
EnvironmentVariables:
- Name: OUTPUT_BUCKET
Value: !Ref OutputBucket
Type: PLAINTEXT
# Name of the CodeBuild project
Name: !Join
- ""
- - Ref: PipelineName
- "-builder"
# Queuing timeout
QueuedTimeoutInMinutes: 20
# IAM role for the builder
ServiceRole: !GetAtt [BuilderRole, Arn]
Source:
Type: CODEPIPELINE
Tags:
- Key: Type
Value: Learning
# Building timeout
TimeoutInMinutes: 5
# S3 bucket where builds will be stored
OutputBucket:
Type: AWS::S3::Bucket
Properties:
BucketName:
Ref: BucketName
WebsiteConfiguration:
IndexDocument:
Ref: IndexDocumentName
ErrorDocument:
Ref: ErrorDocumentName
# S3 bucket where artifacts will be stored
ArtifactBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Join
- ""
- - Ref: BucketName
- "-artifacts"
# Determines how long artifacts are stored
LifecycleConfiguration:
Rules:
- ExpirationInDays:
Ref: ArtifactLifeInDays
Status: Enabled
# IAM role for the pipeline
PipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ["sts:AssumeRole"]
Effect: Allow
Principal:
Service: [codepipeline.amazonaws.com]
# Pipeline permissions
Policies:
- PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
# Codestar permissions
- Effect: "Allow"
Action:
- "codestar-connections:UseConnection"
Resource: !Ref CodeStarConnection
# Bucket permissions
- Effect: "Allow"
Action:
- "s3:*"
Resource:
- !Join
- ""
- - !GetAtt [OutputBucket, Arn]
- /*
- !Join
- ""
- - !GetAtt [ArtifactBucket, Arn]
- /*
# Codebuild permissions
- Effect: "Allow"
Action:
- "codebuild:StartBuild"
- "codebuild:StartBuildBatch"
- "codebuild:BatchGetBuilds"
- "codebuild:BatchGetBuildBatches"
Resource: !GetAtt [Builder, Arn]
# IAM role for the builder
BuilderRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ["sts:AssumeRole"]
Effect: Allow
Principal:
Service: [codebuild.amazonaws.com]
# Builder permissions
Policies:
- PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
# Cloudwatch permissions
- Effect: "Allow"
Action:
- "cloudwatch:*"
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "*"
# Permissions for retrieving the source artifact
- Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:GetObject"
- "s3:GetObjectVersion"
Resource: !Join
- ""
- - !GetAtt [ArtifactBucket, Arn]
- /*
# Permissions for clearing the output bucket
- Effect: "Allow"
Action:
- "s3:ListBucket"
Resource: !GetAtt [OutputBucket, Arn]
- Effect: "Allow"
Action:
- "s3:DeleteObject"
Resource: !Join
- ""
- - !GetAtt [OutputBucket, Arn]
- /*
# S3 Bucket Policy for the output bucket
BucketRole:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: OutputBucket
PolicyDocument:
Id: !Join
- ""
- - Ref: BucketName
- "_policy"
Version: 2012-10-17
Statement:
- Sid: PublicReadForGetBucketObjects
Effect: Allow
Principal: "*"
Action: "s3:GetObject"
Resource: !Join
- ""
- - "arn:aws:s3:::"
- Ref: BucketName
- /*
Parameters:
# How many days before artifacts expire
ArtifactLifeInDays:
Type: Number
Description: Specify how many days you want artifacts to be stored for (1->180 days accepted)
Default: 30
MaxValue: 180
MinValue: 1
# Branch name
BranchName:
Type: String
Description: Enter the branch name that you want to be used for the build
# Name of the (new) output bucket
BucketName:
Type: String
Description: Enter what you want to call your output bucket. Note, the name must be DNS compliant
# CodeStar Connection
CodeStarConnection:
Type: String
Description: Enter the ARN of the CodeStar connection you want to use
# Name of the error file
ErrorDocumentName:
Type: String
Default: error.html
Description: Enter the name (or path) of your error file
# Name of the index file
IndexDocumentName:
Type: String
Default: index.html
Description: Enter the name (or path) of your index file
# Pipeline name
PipelineName:
Type: String
Description: Enter what you want to call this pipeline
# Github username
RepositoryOwner:
Type: String
Description: Enter the Github username of the repository owner
# Github repository name
RepositoryName:
Type: String
Description: Enter the name of the repository you want to build