-
Notifications
You must be signed in to change notification settings - Fork 7
/
pipeline.yml
361 lines (349 loc) · 12.7 KB
/
pipeline.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
AWSTemplateFormatVersion: "2010-09-09"
Description: >
CodePipline to build and deploy lambda functions. Naming conveention, [repo]--[branch]--[eyecatcher]--cicd
Parameters:
CodeBuildImage:
Description: Image for the CodeBuild container that runs linting, test cases, build. Make sure the version matches your Lambda runtime. Ex, golang:1.10.3
Type: String
Default: aws/codebuild/standard:2.0
S3BucketForLambdaPackages:
Description: Name of bucket to hold lambda deploy packages
Type: String
Default: us-east-1--deploy.example.com
BuildTimeout:
Description: Timeout in minutes for the build
Type: Number
Default: 5
CodeBuildProjectBuildSpecPath:
Description: Path to the buildspec used to build your app image, relative to your codebase
Type: String
Default: aws/codebuild/buildspec.yml
TestCloudFormationTemplateParameters:
Description: Test stack - Relative JSON config file that contains parameters passed CloudFormation
Type: String
Default: aws/cloudformation/parameters/test-pipeline-parameters.json
StagingCloudFormationTemplateParameters:
Description: Staging stack - Relative JSON config file that contains parameters passed CloudFormation
Type: String
Default: aws/cloudformation/parameters/staging-pipeline-parameters.json
ProdCloudFormationTemplateParameters:
Description: Prod stack - Relative JSON config file that contains parameters passed CloudFormation
Type: String
Default: aws/cloudformation/parameters/prod-pipeline-parameters.json
ApprovalNotificationArn:
Description: SNS ARN to receive notifications for approvals
Type: String
Default: arn:aws:sns:us-east-1:1111111:code-promotion-approvals
GithubOrg:
Description: The GitHub organization
Type: String
GithubOAuthToken:
Description: The GitHub Personal Access Token so CodePipeline can get the code. https://github.com/settings/tokens. Needs repo scope.
Type: String
NoEcho: true
Resources:
ArtifactStoreBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [codebuild.amazonaws.com]
Version: '2012-10-17'
Path: /
Policies:
- PolicyName: CodeBuild
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Effect: Allow
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*"
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*:*"
- Action:
- "s3:PutObject"
- "s3:GetObject"
- "s3:GetObjectVersion"
Effect: Allow
Resource:
- "arn:aws:s3:::codepipeline-us-east-1-*"
- Action:
- 's3:*'
Effect: Allow
Resource:
- !Sub
- ${bucket}*
- { bucket: !GetAtt ArtifactStoreBucket.Arn }
- !Sub
- arn:aws:s3:::${bucket}*
- { bucket: !Ref S3BucketForLambdaPackages }
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: !Ref 'AWS::StackName'
Artifacts:
Type: CODEPIPELINE
Environment:
ComputeType: BUILD_GENERAL1_LARGE
PrivilegedMode: false
Type: LINUX_CONTAINER
Image: !Ref CodeBuildImage
EnvironmentVariables:
- Name: DEPLOY_BUCKET
Value: !Ref S3BucketForLambdaPackages
- Name: DEPLOY_S3_PREFIX
Value: !Ref AWS::StackName
ServiceRole: !Ref CodeBuildRole
Source:
Type: CODEPIPELINE
BuildSpec: !Ref CodeBuildProjectBuildSpecPath
TimeoutInMinutes: !Ref BuildTimeout
PipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [codepipeline.amazonaws.com]
Version: '2012-10-17'
Path: /
Policies:
- PolicyName: CodePipelineAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 's3:*'
- 'cloudformation:CreateStack'
- 'cloudformation:DescribeStacks'
- 'cloudformation:DeleteStack'
- 'cloudformation:UpdateStack'
- 'cloudformation:CreateChangeSet'
- 'cloudformation:ExecuteChangeSet'
- 'cloudformation:DeleteChangeSet'
- 'cloudformation:DescribeChangeSet'
- 'cloudformation:SetStackPolicy'
- 'iam:PassRole'
- 'sns:Publish'
- 'codebuild:BatchGetBuilds'
- 'codebuild:StartBuild'
- 'codecommit:*'
Effect: Allow
Resource: '*'
CFNRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [cloudformation.amazonaws.com]
Version: '2012-10-17'
Path: /
Policies:
- PolicyName: CloudFormationRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- '*'
Effect: Allow
Resource: '*'
# 'GithubWebhook' satisfies two requirements:
# -- Means that updates are pushed from GitHub, rather than AWS having to poll
# -- Means we can filter for required changes
GithubWebhook:
Type: AWS::CodePipeline::Webhook
Properties:
Authentication: GITHUB_HMAC
AuthenticationConfiguration:
SecretToken: !Ref GithubOAuthToken
RegisterWithThirdParty: true
Filters:
- JsonPath: "$.ref"
MatchEquals: refs/heads/{Branch}
TargetPipeline: !Ref Pipeline
TargetAction: Source
TargetPipelineVersion: !GetAtt Pipeline.Version
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Location: !Ref 'ArtifactStoreBucket'
Type: S3
DisableInboundStageTransitions: []
Name: !Ref AWS::StackName
RoleArn: !GetAtt PipelineRole.Arn
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: ThirdParty
Provider: GitHub
Version: '1'
OutputArtifacts:
- Name: MyAppCode
Configuration:
Owner: !Ref GithubOrg
Repo: !Select [ 0, !Split [ '--', !Ref 'AWS::StackName' ] ]
PollForSourceChanges: false
Branch: !Select [ 1, !Split [ '--', !Ref 'AWS::StackName' ] ]
OAuthToken: !Ref GithubOAuthToken
RunOrder: 1
- Name: Build
Actions:
- Name: CodeBuild
InputArtifacts:
- Name: MyAppCode
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
OutputArtifacts:
- Name: MyAppBuild
Configuration:
ProjectName: !Ref CodeBuildProject
RunOrder: 1
- Name: Test
Actions:
- Name: CreateChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
InputArtifacts:
- Name: MyAppCode
- Name: MyAppBuild
Configuration:
ActionMode: CHANGE_SET_REPLACE
RoleArn: !GetAtt CFNRole.Arn
Capabilities: CAPABILITY_IAM
StackName: !Sub "${AWS::StackName}--test--gen"
ChangeSetName: !Sub "${AWS::StackName}--test--changeset"
TemplatePath: MyAppBuild::outputtemplate.yaml
TemplateConfiguration: !Sub "MyAppCode::${TestCloudFormationTemplateParameters}"
RunOrder: 1
- Name: ExecuteChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
Configuration:
ActionMode: CHANGE_SET_EXECUTE
RoleArn: !GetAtt CFNRole.Arn
StackName: !Sub "${AWS::StackName}--test--gen"
ChangeSetName: !Sub "${AWS::StackName}--test--changeset"
RunOrder: 2
# Seperate stage so test enviornment re-deployes are not blocked by approvals
- Name: PromoteTest
Actions:
- Name: Approve
ActionTypeId:
Category: Approval
Owner: AWS
Provider: Manual
Version: '1'
Configuration:
NotificationArn: !Ref ApprovalNotificationArn
CustomData: !Sub '${AWS::StackName} test stack ready for promotion?'
RunOrder: 1
- Name: Staging
Actions:
- Name: CreateChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
InputArtifacts:
- Name: MyAppCode
- Name: MyAppBuild
Configuration:
ActionMode: CHANGE_SET_REPLACE
RoleArn: !GetAtt CFNRole.Arn
Capabilities: CAPABILITY_IAM
StackName: !Sub "${AWS::StackName}--staging--gen"
ChangeSetName: !Sub "${AWS::StackName}--staging--changeset"
TemplatePath: MyAppBuild::outputtemplate.yaml
TemplateConfiguration: !Sub "MyAppCode::${StagingCloudFormationTemplateParameters}"
RunOrder: 1
- Name: ExecuteChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
Configuration:
ActionMode: CHANGE_SET_EXECUTE
RoleArn: !GetAtt CFNRole.Arn
StackName: !Sub "${AWS::StackName}--staging--gen"
ChangeSetName: !Sub "${AWS::StackName}--staging--changeset"
RunOrder: 2
- Name: ApproveStaging
ActionTypeId:
Category: Approval
Owner: AWS
Provider: Manual
Version: '1'
Configuration:
NotificationArn: !Ref ApprovalNotificationArn
CustomData: !Sub '${AWS::StackName} ready to deploy to prod?'
RunOrder: 1
- Name: Prod
Actions:
- Name: CreateChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
InputArtifacts:
- Name: MyAppCode
- Name: MyAppBuild
Configuration:
ActionMode: CHANGE_SET_REPLACE
RoleArn: !GetAtt CFNRole.Arn
Capabilities: CAPABILITY_IAM
StackName: !Sub "${AWS::StackName}--prod--gen"
ChangeSetName: !Sub "${AWS::StackName}--prod--changeset"
TemplatePath: MyAppBuild::outputtemplate.yaml
TemplateConfiguration: !Sub "MyAppCode::${ProdCloudFormationTemplateParameters}"
RunOrder: 1
- Name: ExecuteChangeSet
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
Configuration:
ActionMode: CHANGE_SET_EXECUTE
RoleArn: !GetAtt CFNRole.Arn
StackName: !Sub "${AWS::StackName}--prod--gen"
ChangeSetName: !Sub "${AWS::StackName}--prod--changeset"
RunOrder: 2
Outputs:
CodeBuildRole:
Description: CodeBuildRole
Value: !Ref CodeBuildRole
S3ArtifactStore:
Description: Artifact Store Bucket
Value: !Ref ArtifactStoreBucket