-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.yml
222 lines (211 loc) · 6.54 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
Parameters:
GithubOwner:
Type: String
Description: Name of the owner (Organization or person) in Github that owns the repo
Default: rupertbg
GithubRepo:
Type: String
Description: Name of the repo in Github
Default: YES3
GithubBranch:
Type: String
Description: Name of the branch to use from the git repo
Default: master
Resources:
PipelineS3Bucket:
Type: AWS::S3::Bucket
LambdaS3Bucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
Pipeline:
DependsOn:
- LambdaS3Bucket
Type: AWS::CodePipeline::Pipeline
Properties:
Name: yes3
RoleArn: !GetAtt AutomationRole.Arn
RestartExecutionOnUpdate: true
ArtifactStore:
Type: S3
Location: !Ref PipelineS3Bucket
Stages:
- Name: Source
Actions:
- Name: Github
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
OutputArtifacts:
- Name: source
Configuration:
Branch: !Ref GithubBranch
OAuthToken: '{{resolve:secretsmanager:Github:SecretString:PersonalAccessToken}}'
Owner: !Ref GithubOwner
PollForSourceChanges: false
Repo: !Ref GithubRepo
RunOrder: 1
- Name: Update
Actions:
- Name: Pipeline
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
RunOrder: 1
InputArtifacts:
- Name: source
Configuration:
ActionMode: REPLACE_ON_FAILURE
Capabilities: CAPABILITY_NAMED_IAM
RoleArn: !GetAtt AutomationRole.Arn
StackName: !Ref AWS::StackName
TemplatePath: source::pipeline.yml
- Name: Build
Actions:
- Name: Lambda
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
RunOrder: 1
InputArtifacts:
- Name: source
OutputArtifacts:
- Name: output
Configuration:
ProjectName: !Ref Deploy
- Name: Deploy
Actions:
- Name: Lambda
InputArtifacts:
- Name: source
- Name: output
ActionTypeId:
Category: Deploy
Owner: AWS
Provider: CloudFormation
Version: '1'
RunOrder: 1
Configuration:
ActionMode: REPLACE_ON_FAILURE
Capabilities: CAPABILITY_NAMED_IAM
RoleArn: !GetAtt AutomationRole.Arn
StackName: !Sub yes3-lambda
TemplatePath: source::lambda.yml
ParameterOverrides: !Sub |
{
"LambdaBucketName": "${LambdaS3Bucket}",
"S3FunctionCodeVersion": { "Fn::GetParam": [ "output", "output.json", "VersionId" ] }
}
GHWebHook:
DependsOn:
- Pipeline
Type: AWS::CodePipeline::Webhook
Properties:
AuthenticationConfiguration:
SecretToken: '{{resolve:secretsmanager:Github:SecretString:WebhookSecret}}'
Filters:
- JsonPath: '$.ref'
MatchEquals: !Sub refs/heads/${GithubBranch}
Authentication: GITHUB_HMAC
TargetPipeline: !Ref Pipeline
TargetAction: GitHub
Name: yes3
TargetPipelineVersion: !GetAtt Pipeline.Version
Deploy:
Type: AWS::CodeBuild::Project
Properties:
Name: yes3
ServiceRole: !GetAtt AutomationRole.Arn
TimeoutInMinutes: 5
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:2.0
Source:
Type: CODEPIPELINE
BuildSpec:
!Sub |
version: 0.2
artifacts:
discard-paths: yes
files:
- output.json
phases:
install:
runtime-versions:
python: 3.7
build:
commands:
- cd src;
- pip install -r requirements.txt -t .;
- zip -r code.zip .;
post_build:
commands:
- du -hs *
- aws s3api put-object --bucket ${LambdaS3Bucket} --key code.zip --body code.zip --output json > ../output.json;
AutomationRole:
Type: AWS::IAM::Role
Properties:
RoleName: yes3
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
- codebuild.amazonaws.com
- cloudformation.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: yes3
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: cloudformation:*
Resource: '*'
- Effect: Allow
Action: codepipeline:*
Resource: '*'
- Effect: Allow
Action: lambda:*
Resource: '*'
- Effect: Allow
Action: codebuild:*
Resource: '*'
- Effect: Allow
Action: secretsmanager:GetSecretValue
Resource: !Sub arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:Github-??????
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action: events:*
Resource: '*'
- Effect: Allow
Action: s3:*
Resource: '*'
- Effect: Allow
Action: iam:*
Resource:
- !Sub arn:aws:iam::${AWS::AccountId}:policy/yes3*
- !Sub arn:aws:iam::${AWS::AccountId}:role/yes3*
Outputs:
WebHookURL:
Value: !GetAtt GHWebHook.Url
Export:
Name: !Sub yes3-webhook-url