-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTest_CodeBuild_PR_Merge.yaml
232 lines (215 loc) · 7.47 KB
/
Test_CodeBuild_PR_Merge.yaml
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
AWSTemplateFormatVersion: "2010-09-09"
Description: Template creates Codebuild resources for a simple SAM Application
Parameters:
RepoParam:
Type: String
Description: Enter the GitHub URL for the repo
Default: https://GitHub.com/InSciCo/PetStore.git
Resources:
S3:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub "${AWS::StackName}-${AWS::AccountId}"
CodeBuild:
Type: AWS::CodeBuild::Project
Properties:
Name: !Sub "${AWS::StackName}"
Description: Build process for .NET Serverless Application
ServiceRole: !GetAtt CodeBuildRole.Arn
Artifacts:
Type: S3
Location: !Ref S3
Name: !Sub "${AWS::StackName}-build-artifacts"
Path: ''
NamespaceType: NONE
Packaging: NONE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:5.0
Source:
Location: !Ref RepoParam
Type: GITHUB
GitCloneDepth: 1
BuildSpec: !Sub |
version: 0.2
env:
git-credential-helper: yes
phases:
build:
commands:
# ex for GitHub refs/heads/F1
- echo $CODEBUILD_WEBHOOK_HEAD_REF
- FeatureBranch="$(basename $CODEBUILD_WEBHOOK_HEAD_REF)"
- stack=$FeatureBranch
- eval "aws cloudformation delete-stack --stack-name $stack"
- git config user.name "CodeBuild"
- git config user.email [email protected]
- git checkout $FeatureBranch
- git rm Stacks/Test/AwsSettings.json
- git commit -m "Deleted AwsSettings.json for PR Stack"
- git push --set-upstream origin HEAD:$FeatureBranch
Triggers:
Webhook: true
FilterGroups:
- - Type: EVENT
Pattern: PULL_REQUEST_MERGED
ExcludeMatchedPattern: false
BadgeEnabled: false
LogsConfig:
CloudWatchLogs:
Status: ENABLED
S3Logs:
Status: DISABLED
EncryptionDisabled: false
TimeoutInMinutes: 10
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub "CodeBuildRole-test-pr-merge-${AWS::StackName}"
Description: Provides Codebuild permission to access API GW, Lambda and Cloudformation
#Provide Codebuild permission to assume this role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service: "codebuild.amazonaws.com"
Action: "sts:AssumeRole"
# This can be restricted more
CodeBuildPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: !Sub "CodeBuildPolicy-test-pr-merge-${AWS::StackName}"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 'cognito-idp:*'
Resource:
- "*"
- Effect: Allow
Action:
- 'cognito-identity:*'
Resource:
- "*"
- Effect: Allow
Action:
- 'dynamodb:UpdateTable'
- 'dynamodb:DescribeTable'
- 'dynamodb:DeleteTable'
- 'dynamodb:CreateTable'
Resource:
- !Sub 'arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/*'
- Effect: Allow
Action:
- 'cloudformation:CreateChangeSet'
Resource:
- 'arn:aws:cloudformation:*:aws:transform/Serverless-2016-10-31'
- Effect: Allow
Action:
- 'cloudformation:CreateChangeSet'
- 'cloudformation:DeleteStack'
- 'cloudformation:DescribeChangeSet'
- 'cloudformation:DescribeStackEvents'
- 'cloudformation:DescribeStacks'
- 'cloudformation:ExecuteChangeSet'
- 'cloudformation:GetTemplateSummary'
- 'cloudformation:DescribeStackResources'
- 'cloudformation:GetTemplate'
Resource:
- !Sub 'arn:aws:cloudformation:*:${AWS::AccountId}:stack/*'
- Effect: Allow
Action:
- 's3:CreateBucket'
- 's3:GetObject'
- 's3:PutObject'
Resource:
- 'arn:aws:s3:::*/*'
- Effect: Allow
Action:
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:BatchGetImage'
- 'ecr:CompleteLayerUpload'
- 'ecr:DescribeImages'
- 'ecr:DescribeRepositories'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:GetRepositoryPolicy'
- 'ecr:InitiateLayerUpload'
- 'ecr:ListImages'
- 'ecr:PutImage'
- 'ecr:SetRepositoryPolicy'
- 'ecr:UploadLayerPart'
Resource:
- !Sub 'arn:aws:ecr:*:${AWS::AccountId}:repository/*'
- Effect: Allow
Action:
- 'ecr:GetAuthorizationToken'
Resource:
- '*'
- Effect: Allow
Action:
- 'lambda:AddPermission'
- 'lambda:CreateFunction'
- 'lambda:DeleteFunction'
- 'lambda:GetFunction'
- 'lambda:GetFunctionConfiguration'
- 'lambda:ListTags'
- 'lambda:RemovePermission'
- 'lambda:TagResource'
- 'lambda:UntagResource'
- 'lambda:UpdateFunctionCode'
- 'lambda:UpdateFunctionConfiguration'
Resource:
- !Sub 'arn:aws:lambda:*:${AWS::AccountId}:function:*'
- Effect: Allow
Action:
- 'iam:AttachRolePolicy'
- 'iam:CreateRole'
- 'iam:DeleteRole'
- 'iam:DeleteRolePolicy'
- 'iam:DetachRolePolicy'
- 'iam:GetRole'
- 'iam:GetRolePolicy'
- 'iam:PutRolePolicy'
- 'iam:PassRole'
- 'iam:TagRole'
- 'iam:CreateServiceLinkedRole'
Resource:
- !Sub 'arn:aws:iam::${AWS::AccountId}:role/*'
- Effect: Allow
Action:
- 'apigateway:*'
Resource:
- !Sub 'arn:aws:apigateway:*:*:/*'
- Effect: Allow
Action:
- 'logs:DescribeQueries'
- 'logs:GetLogRecord'
- 'logs:PutDestinationPolicy'
- 'logs:StopQuery'
- 'logs:TestMetricFilter'
- 'logs:DeleteDestination'
- 'logs:GetLogDelivery'
- 'logs:ListLogDeliveries'
- 'logs:CreateLogDelivery'
- 'logs:DeleteResourcePolicy'
- 'logs:PutResourcePolicy'
- 'logs:DescribeExportTasks'
- 'logs:GetQueryResults'
- 'logs:UpdateLogDelivery'
- 'logs:CancelExportTask'
- 'logs:DeleteLogDelivery'
- 'logs:PutDestination'
- 'logs:DescribeResourcePolicies'
- 'logs:DescribeDestinations'
Resource: '*'
- Effect: Allow
Action: 'logs:*'
Resource: !Sub 'arn:aws:logs:*:${AWS::AccountId}:log-group:*'
- Effect: Allow
Action: 'logs:*'
Resource: !Sub 'arn:aws:logs:*:${AWS::AccountId}:log-group:*:log-stream:*'
Roles:
- !Ref CodeBuildRole