-
Notifications
You must be signed in to change notification settings - Fork 425
/
Copy pathpipeline_cfn.yml
316 lines (315 loc) · 11.6 KB
/
pipeline_cfn.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
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: '2010-09-09'
Description: CodePipeline for {{$.AppName}}
Metadata:
Version: {{ .Version }}
Resources:
{{- if isCodeStarConnection .Source}}
{{if eq .Source.ConnectionARN ""}}
SourceConnection:
Type: AWS::CodeStarConnections::Connection
Properties:
ConnectionName: {{.Source.ConnectionName}}
ProviderType: {{.Source.ProviderName}}
{{- end}}
{{- end}}
{{ include "build-action" . | indent 2}}
{{ include "test" . | indent 2 }}
{{ include "actions" . | indent 2}}
PipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
Action:
- sts:AssumeRole
Path: /
{{- if .PermissionsBoundary}}
PermissionsBoundary: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/{{.PermissionsBoundary}}'
{{- end}}
PipelineRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Sub ${AWS::StackName}-CodepipelinePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- codepipeline:*
- codecommit:GetBranch
- codecommit:GetCommit
- codecommit:UploadArchive
- codecommit:GetUploadArchiveStatus
- codecommit:CancelUploadArchive
{{- if eq .Source.ProviderName "CodeCommit" }}
{{- if ne .Source.OutputArtifactFormat "" }}
- codecommit:GetRepository
{{- end }}
{{- end }}
- iam:ListRoles
- cloudformation:Describe*
- cloudFormation:List*
- codebuild:BatchGetBuilds
- codebuild:StartBuild
- cloudformation:CreateStack
- cloudformation:DeleteStack
- cloudformation:DescribeStacks
- cloudformation:UpdateStack
- cloudformation:CreateChangeSet
- cloudformation:DeleteChangeSet
- cloudformation:DescribeChangeSet
- cloudformation:ExecuteChangeSet
- cloudformation:SetStackPolicy
- cloudformation:ValidateTemplate
- iam:PassRole
- s3:ListAllMyBuckets
- s3:GetBucketLocation
Resource:
- "*"
{{- if isCodeStarConnection .Source}}
- Effect: Allow
Action:
- codestar-connections:CreateConnection
- codestar-connections:DeleteConnection
- codestar-connections:GetConnection
- codestar-connections:ListConnections
- codestar-connections:GetIndividualAccessToken
- codestar-connections:GetInstallationUrl
- codestar-connections:ListInstallationTargets
- codestar-connections:StartOAuthHandshake
- codestar-connections:UpdateConnectionInstallation
- codestar-connections:UseConnection
- codestar-connections:RegisterAppCode
- codestar-connections:StartAppRegistrationHandshake
- codestar-connections:StartUploadArchiveToS3
- codestar-connections:GetUploadArchiveToS3Status
- codestar-connections:PassConnection
- codestar-connections:PassedToService
Resource:
{{- if eq .Source.ConnectionARN ""}}
- !Ref SourceConnection
{{- else}}
- {{$.Source.Connection}}
{{- end}}
{{- end}}
- Effect: Allow
Action:
- kms:Decrypt
- kms:Encrypt
- kms:GenerateDataKey
Resource:{{range .ArtifactBuckets}}
- {{.KeyArn}}{{end}}
- Effect: Allow
Action:
- s3:PutObject
- s3:GetBucketPolicy
- s3:GetObject
- s3:ListBucket
{{- if isCodeStarConnection .Source}}
- s3:PutObjectAcl
- s3:GetObjectAcl
{{- end}}
Resource:{{range .ArtifactBuckets}}
- !Join ['', ['arn:aws:s3:::', '{{.BucketName}}']]
- !Join ['', ['arn:aws:s3:::', '{{.BucketName}}', '/*']]{{end}}
- Effect: Allow
Action:
- sts:AssumeRole
Resource:{{range $stage := .Stages}}
- arn:aws:iam::{{$stage.AccountID}}:role/{{$.AppName}}-{{$stage.Name}}-EnvManagerRole{{end}}
Roles:
- !Ref PipelineRole
Pipeline:
Type: AWS::CodePipeline::Pipeline
DependsOn:
- PipelineRole
- PipelineRolePolicy
Properties:
ArtifactStores:{{range .ArtifactBuckets}}
- Region: {{.Region}}
ArtifactStore:
Type: S3
Location: {{.BucketName}}
EncryptionKey:
Id: {{.KeyArn}}
Type: KMS{{end}}
RoleArn: !GetAtt PipelineRole.Arn
{{- if .IsLegacy }}
Name: !Ref AWS::StackName
{{- end }}
Stages:
{{- if eq .Source.ProviderName "GitHubV1"}}
- Name: Source
Actions:
- Name: SourceCodeFor-{{$.AppName}}
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
Configuration:
Owner: {{$.Source.Owner}}
Repo: {{$.Source.Repository}}
Branch: {{$.Source.Branch}}
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html#dynamic-references-secretsmanager
# Use the *entire* SecretString with version AWSCURRENT
OAuthToken: !Sub
- '{{"{{"}}resolve:secretsmanager:${SecretId}{{"}}"}}'
- SecretId: {{$.Source.GitHubPersonalAccessTokenSecretID}}
OutputArtifacts:
- Name: SCCheckoutArtifact
RunOrder: 1
{{- else if isCodeStarConnection .Source}}
- Name: Source
Actions:
- Name: SourceCodeFor-{{$.AppName}}
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: CodeStarSourceConnection
Configuration:
ConnectionArn:
{{- if eq .Source.ConnectionARN ""}}
!Ref SourceConnection
{{- else}}
{{$.Source.Connection}}
{{- end}}
FullRepositoryId: {{$.Source.Repository}}
BranchName: {{$.Source.Branch}}
{{- if ne .Source.OutputArtifactFormat "" }}
OutputArtifactFormat: {{$.Source.OutputArtifactFormat}}
{{- end}}
OutputArtifacts:
- Name: SCCheckoutArtifact
RunOrder: 1
{{- else if eq .Source.ProviderName "CodeCommit"}}
- Name: Source
Actions:
- Name: SourceCodeFor-{{$.AppName}}
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: CodeCommit
Configuration:
RepositoryName: {{$.Source.Repository}}
BranchName: {{$.Source.Branch}}
{{- if ne .Source.OutputArtifactFormat "" }}
OutputArtifactFormat: {{$.Source.OutputArtifactFormat}}
{{- end}}
OutputArtifacts:
- Name: SCCheckoutArtifact
RunOrder: 1
{{- end }}
- Name: Build
Actions:
- Name: Build
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref BuildProject
RunOrder: 1
InputArtifacts:
- Name: SCCheckoutArtifact
OutputArtifacts:
- Name: BuildOutput
{{- range $stage := .Stages}}
{{- $numDeployments := len $stage.Deployments}}{{- if gt $numDeployments 0}}
- Name: {{$stage.FullName}}
Actions:
{{- if $stage.Approval }}
- Name: {{$stage.Approval.Name}}
ActionTypeId:
Category: Approval
Owner: AWS
Version: 1
Provider: Manual
RunOrder: {{$stage.Approval.RunOrder}}
{{- end}}
{{- range $action := $stage.PreDeployments }}
- Name: {{ $action.Name }}
RunOrder: {{ $action.RunOrder}}
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref Pre{{alphanumeric $stage.Name}}DeploymentAction{{alphanumeric $action.Name}}
InputArtifacts:
- Name: SCCheckoutArtifact
OutputArtifacts:
- Name: Pre{{alphanumeric $stage.Name}}DeploymentAction{{alphanumeric $action.Name}}Output
{{- end}}
{{- range $action := $stage.PostDeployments }}
- Name: {{ $action.Name }}
RunOrder: {{ $action.RunOrder}}
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref Post{{alphanumeric $stage.Name}}DeploymentAction{{alphanumeric $action.Name}}
InputArtifacts:
- Name: SCCheckoutArtifact
OutputArtifacts:
- Name: Post{{alphanumeric $stage.Name}}DeploymentAction{{alphanumeric $action.Name}}Output
{{- end}}
{{- range $deployment := $stage.Deployments}}
- Name: {{$deployment.Name}}
Region: {{$stage.Region}}
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
ActionMode: CREATE_UPDATE
StackName: {{$deployment.StackName}}
Capabilities: CAPABILITY_IAM,CAPABILITY_NAMED_IAM,CAPABILITY_AUTO_EXPAND
TemplatePath: BuildOutput::{{$deployment.TemplatePath}}
TemplateConfiguration: BuildOutput::{{$deployment.TemplateConfigPath}}
RoleArn: {{$stage.ExecRoleARN}}
InputArtifacts:
- Name: BuildOutput
RunOrder: {{$deployment.RunOrder}}
RoleArn: {{$stage.EnvManagerRoleARN}}
{{- end}}
{{- if $stage.Test }}
- Name: {{$stage.Test.Name}}
ActionTypeId:
Category: Test
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref BuildTestCommands{{logicalIDSafe $stage.Name}}
RunOrder: {{$stage.Test.RunOrder}}
InputArtifacts:
- Name: SCCheckoutArtifact
{{- end}}
{{- end}} {{/* if gt $numDeployments 0 */}}
{{- end}} {{/* range $stage := .Stages */}}
{{- if isCodeStarConnection .Source}}
Outputs:
PipelineConnectionARN:
Description: "ARN of CodeStar Connections connection"
Value:
{{- if eq .Source.ConnectionARN ""}}
!Ref SourceConnection
{{- else}}
{{$.Source.Connection}}
{{- end}}
{{- end}}