· 7 years ago · Oct 09, 2018, 04:58 AM
1---
2AWSTemplateFormatVersion: '2010-09-09'
3Description: Pipeline using CodePipeline and CodeBuild for continuous delivery of a single-page application to S3
4Parameters:
5 SiteBucketName:
6 Type: String
7 Description: Name of bucket to create to host the website
8 GitHubUser:
9 Type: String
10 Description: GitHub User
11 Default: "stelligent"
12 GitHubRepo:
13 Type: String
14 Description: GitHub Repo to pull from. Only the Name. not the URL
15 Default: "devops-essentials"
16 GitHubBranch:
17 Type: String
18 Description: GitHub Branch
19 Default: "master"
20 GitHubToken:
21 NoEcho: true
22 Type: String
23 Description: Secret. It might look something like 9b189a1654643522561f7b3ebd44a1531a4287af OAuthToken with access to Repo. Go to https://github.com/settings/tokens
24 BuildType:
25 Type: String
26 Default: "LINUX_CONTAINER"
27 Description: The build container type to use for building the app
28 BuildComputeType:
29 Type: String
30 Default: "BUILD_GENERAL1_SMALL"
31 Description: The build compute type to use for building the app
32 BuildImage:
33 Type: String
34 Default: "aws/codebuild/ubuntu-base:14.04"
35 Description: The build image to use for building the app
36Metadata:
37 AWS::CloudFormation::Interface:
38 ParameterGroups:
39 - Label:
40 default: "Site Configuration"
41 Parameters:
42 - SiteBucketName
43 - Label:
44 default: "GitHub Configuration"
45 Parameters:
46 - GitHubToken
47 - GitHubUser
48 - GitHubRepo
49 - GitHubBranch
50 - Label:
51 default: "Build Configuration"
52 Parameters:
53 - BuildType
54 - BuildComputeType
55 - BuildImage
56 ParameterLabels:
57 SiteBucketName:
58 default: Name of S3 Bucket to create for website hosting
59 GitHubToken:
60 default: GitHub OAuth2 Token
61 GitHubUser:
62 default: GitHub User/Org Name
63 GitHubRepo:
64 default: GitHub Repository Name
65 GitHubBranch:
66 default: GitHub Branch Name
67 BuildType:
68 default: CodeBuild type
69 BuildComputeType:
70 default: CodeBuild instance type
71 BuildImage:
72 default: CodeBuild image
73Resources:
74 CodeBuildRole:
75 Type: AWS::IAM::Role
76 Properties:
77 AssumeRolePolicyDocument:
78 Statement:
79 - Effect: Allow
80 Principal:
81 Service:
82 - codebuild.amazonaws.com
83 Action:
84 - sts:AssumeRole
85 Path: "/"
86 Policies:
87 - PolicyName: codebuild-service
88 PolicyDocument:
89 Statement:
90 - Effect: Allow
91 Action: "*"
92 Resource: "*"
93 Version: '2012-10-17'
94 CodePipelineRole:
95 Type: AWS::IAM::Role
96 Properties:
97 AssumeRolePolicyDocument:
98 Statement:
99 - Effect: Allow
100 Principal:
101 Service:
102 - codepipeline.amazonaws.com
103 Action:
104 - sts:AssumeRole
105 Path: "/"
106 Policies:
107 - PolicyName: codepipeline-service
108 PolicyDocument:
109 Statement:
110 - Action:
111 - codebuild:*
112 Resource: "*"
113 Effect: Allow
114 - Action:
115 - s3:GetObject
116 - s3:GetObjectVersion
117 - s3:GetBucketVersioning
118 Resource: "*"
119 Effect: Allow
120 - Action:
121 - s3:PutObject
122 Resource:
123 - arn:aws:s3:::codepipeline*
124 Effect: Allow
125 - Action:
126 - s3:*
127 - cloudformation:*
128 - iam:PassRole
129 Resource: "*"
130 Effect: Allow
131 Version: '2012-10-17'
132 SiteBucket:
133 Type: AWS::S3::Bucket
134 DeletionPolicy: Delete
135 Properties:
136 AccessControl: PublicRead
137 BucketName: !Ref SiteBucketName
138 WebsiteConfiguration:
139 IndexDocument: index.html
140 PipelineBucket:
141 Type: AWS::S3::Bucket
142 DeletionPolicy: Delete
143 CodeBuildDeploySite:
144 Type: AWS::CodeBuild::Project
145 DependsOn: CodeBuildRole
146 Properties:
147 Name: !Sub ${AWS::StackName}-DeploySite
148 Description: Deploy site to S3
149 ServiceRole: !GetAtt CodeBuildRole.Arn
150 Artifacts:
151 Type: CODEPIPELINE
152 Environment:
153 Type: !Ref BuildType
154 ComputeType: !Ref BuildComputeType
155 Image: !Sub ${BuildImage}
156 Source:
157 Type: CODEPIPELINE
158 BuildSpec: !Sub |
159 version: 0.1
160 phases:
161 post_build:
162 commands:
163 - aws s3 cp --recursive --acl public-read ./samples s3://${SiteBucketName}/samples
164 - aws s3 cp --recursive --acl public-read ./html s3://${SiteBucketName}/
165 artifacts:
166 type: zip
167 files:
168 - ./html/index.html
169 TimeoutInMinutes: 10
170 Pipeline:
171 Type: AWS::CodePipeline::Pipeline
172 Properties:
173 RoleArn: !GetAtt CodePipelineRole.Arn
174 Stages:
175 - Name: Source
176 Actions:
177 - InputArtifacts: []
178 Name: Source
179 ActionTypeId:
180 Category: Source
181 Owner: ThirdParty
182 Version: '1'
183 Provider: GitHub
184 OutputArtifacts:
185 - Name: SourceArtifacts
186 Configuration:
187 Owner: !Ref GitHubUser
188 Repo: !Ref GitHubRepo
189 Branch: !Ref GitHubBranch
190 OAuthToken: !Ref GitHubToken
191 RunOrder: 1
192 - Name: Deploy
193 Actions:
194 - Name: Artifact
195 ActionTypeId:
196 Category: Build
197 Owner: AWS
198 Version: '1'
199 Provider: CodeBuild
200 InputArtifacts:
201 - Name: SourceArtifacts
202 OutputArtifacts:
203 - Name: DeploymentArtifacts
204 Configuration:
205 ProjectName: !Ref CodeBuildDeploySite
206 RunOrder: 1
207 ArtifactStore:
208 Type: S3
209 Location: !Ref PipelineBucket
210Outputs:
211 PipelineUrl:
212 Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${Pipeline}
213 Description: CodePipeline URL
214 SiteUrl:
215 Value: !GetAtt [SiteBucket, WebsiteURL]
216 Description: S3 Website URL
217
218---
219AWSTemplateFormatVersion: '2010-09-09'
220Description: Pipeline using CodePipeline and CodeBuild for continuous delivery of a single-page application to S3
221Parameters:
222 SiteBucketName:
223 Type: String
224 Description: Name of bucket to create to host the website
225 GitHubUser:
226 Type: String
227 Description: GitHub User
228 Default: "stelligent"
229 GitHubRepo:
230 Type: String
231 Description: GitHub Repo to pull from. Only the Name. not the URL
232 Default: "devops-essentials"
233 GitHubBranch:
234 Type: String
235 Description: GitHub Branch
236 Default: "master"
237 GitHubToken:
238 NoEcho: true
239 Type: String
240 Description: Secret. It might look something like 9b189a1654643522561f7b3ebd44a1531a4287af OAuthToken with access to Repo. Go to https://github.com/settings/tokens
241 BuildType:
242 Type: String
243 Default: "LINUX_CONTAINER"
244 Description: The build container type to use for building the app
245 BuildComputeType:
246 Type: String
247 Default: "BUILD_GENERAL1_SMALL"
248 Description: The build compute type to use for building the app
249 BuildImage:
250 Type: String
251 Default: "aws/codebuild/ubuntu-base:14.04"
252 Description: The build image to use for building the app
253Metadata:
254 AWS::CloudFormation::Interface:
255 ParameterGroups:
256 - Label:
257 default: "Site Configuration"
258 Parameters:
259 - SiteBucketName
260 - Label:
261 default: "GitHub Configuration"
262 Parameters:
263 - GitHubToken
264 - GitHubUser
265 - GitHubRepo
266 - GitHubBranch
267 - Label:
268 default: "Build Configuration"
269 Parameters:
270 - BuildType
271 - BuildComputeType
272 - BuildImage
273 ParameterLabels:
274 SiteBucketName:
275 default: Name of S3 Bucket to create for website hosting
276 GitHubToken:
277 default: GitHub OAuth2 Token
278 GitHubUser:
279 default: GitHub User/Org Name
280 GitHubRepo:
281 default: GitHub Repository Name
282 GitHubBranch:
283 default: GitHub Branch Name
284 BuildType:
285 default: CodeBuild type
286 BuildComputeType:
287 default: CodeBuild instance type
288 BuildImage:
289 default: CodeBuild image
290Resources:
291 CodeBuildRole:
292 Type: AWS::IAM::Role
293 Properties:
294 AssumeRolePolicyDocument:
295 Statement:
296 - Effect: Allow
297 Principal:
298 Service:
299 - codebuild.amazonaws.com
300 Action:
301 - sts:AssumeRole
302 Path: "/"
303 Policies:
304 - PolicyName: codebuild-service
305 PolicyDocument:
306 Statement:
307 - Effect: Allow
308 Action: "*"
309 Resource: "*"
310 Version: '2012-10-17'
311 CodePipelineRole:
312 Type: AWS::IAM::Role
313 Properties:
314 AssumeRolePolicyDocument:
315 Statement:
316 - Effect: Allow
317 Principal:
318 Service:
319 - codepipeline.amazonaws.com
320 Action:
321 - sts:AssumeRole
322 Path: "/"
323 Policies:
324 - PolicyName: codepipeline-service
325 PolicyDocument:
326 Statement:
327 - Action:
328 - codebuild:*
329 Resource: "*"
330 Effect: Allow
331 - Action:
332 - s3:GetObject
333 - s3:GetObjectVersion
334 - s3:GetBucketVersioning
335 Resource: "*"
336 Effect: Allow
337 - Action:
338 - s3:PutObject
339 Resource:
340 - arn:aws:s3:::codepipeline*
341 Effect: Allow
342 - Action:
343 - s3:*
344 - cloudformation:*
345 - iam:PassRole
346 Resource: "*"
347 Effect: Allow
348 Version: '2012-10-17'
349 SiteBucket:
350 Type: AWS::S3::Bucket
351 DeletionPolicy: Delete
352 Properties:
353 AccessControl: PublicRead
354 BucketName: !Ref SiteBucketName
355 WebsiteConfiguration:
356 IndexDocument: index.html
357 PipelineBucket:
358 Type: AWS::S3::Bucket
359 DeletionPolicy: Delete
360 CodeBuildDeploySite:
361 Type: AWS::CodeBuild::Project
362 DependsOn: CodeBuildRole
363 Properties:
364 Name: !Sub ${AWS::StackName}-DeploySite
365 Description: Deploy site to S3
366 ServiceRole: !GetAtt CodeBuildRole.Arn
367 Artifacts:
368 Type: CODEPIPELINE
369 Environment:
370 Type: !Ref BuildType
371 ComputeType: !Ref BuildComputeType
372 Image: !Sub ${BuildImage}
373 Source:
374 Type: CODEPIPELINE
375 BuildSpec: !Sub |
376 version: 0.1
377 environment_variables:
378 plaintext:
379 AWS_DEFAULT_REGION: "US-WEST-2"
380 HUGO_VERSION: "0.49"
381 phases:
382 install:
383 commands:
384 - printenv
385 - echo "Install step..."
386 - curl -Ls https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-64bit.tar.gz -o /tmp/hugo.tar.gz
387 - tar xf /tmp/hugo.tar.gz -C /tmp
388 - mv /tmp/hugo_${HUGO_VERSION}_linux_amd64/hugo_${HUGO_VERSION}_linux_amd64 /usr/bin/hugo
389 - rm -rf /tmp/hugo*
390 build:
391 commands:
392 - hugo
393 post_build:
394 commands:
395 - aws s3 cp --recursive --acl public-read ./public s3://${SiteBucketName}
396 artifacts:
397 type: zip
398 files:
399 - ./html/index.html
400 TimeoutInMinutes: 10
401 Pipeline:
402 Type: AWS::CodePipeline::Pipeline
403 Properties:
404 RoleArn: !GetAtt CodePipelineRole.Arn
405 Stages:
406 - Name: Source
407 Actions:
408 - InputArtifacts: []
409 Name: Source
410 ActionTypeId:
411 Category: Source
412 Owner: ThirdParty
413 Version: '1'
414 Provider: GitHub
415 OutputArtifacts:
416 - Name: SourceArtifacts
417 Configuration:
418 Owner: !Ref GitHubUser
419 Repo: !Ref GitHubRepo
420 Branch: !Ref GitHubBranch
421 OAuthToken: !Ref GitHubToken
422 RunOrder: 1
423 - Name: Deploy
424 Actions:
425 - Name: Artifact
426 ActionTypeId:
427 Category: Build
428 Owner: AWS
429 Version: '1'
430 Provider: CodeBuild
431 InputArtifacts:
432 - Name: SourceArtifacts
433 OutputArtifacts:
434 - Name: DeploymentArtifacts
435 Configuration:
436 ProjectName: !Ref CodeBuildDeploySite
437 RunOrder: 1
438 ArtifactStore:
439 Type: S3
440 Location: !Ref PipelineBucket
441Outputs:
442 PipelineUrl:
443 Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${Pipeline}
444 Description: CodePipeline URL
445 SiteUrl:
446 Value: !GetAtt [SiteBucket, WebsiteURL]
447 Description: S3 Website URL