-
Notifications
You must be signed in to change notification settings - Fork 44
/
template.yaml
194 lines (186 loc) · 6.12 KB
/
template.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
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Resource template for integrating SonarQube with AWS CodeCommit approval workflow'
Parameters:
CodeCommitRepositoryName:
Type: String
Default: PullRequestApproverBlogDemo
CodeCommitRepositoryDescription:
Type: String
Default: 'A Maven Java project with a SonarQube analysis integration'
SonarQubeUserName:
Type: String
Description: 'Username of SonarQube identity'
MinLength: 3
NoEcho: true
SonarQubePassword:
Type: String
Description: 'Password for SonarQube user identity'
MinLength: 8
NoEcho: true
Resources:
CodeCommitRepository:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryName: !Ref CodeCommitRepositoryName
RepositoryDescription: !Ref CodeCommitRepositoryDescription
Code:
S3:
Bucket: codecommit-sonarqube-integration-blog-dec-2019
Key: PullRequestApproverBlogDemo.zip
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: NO_ARTIFACTS
BadgeEnabled: true
Description: !Sub 'SonarQube analysis for repository ${CodeCommitRepositoryName}'
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM
Type: LINUX_CONTAINER
Image: 'aws/codebuild/standard:2.0'
EnvironmentVariables:
- Name: SONARQUBE_USER_CREDENTIAL_SECRET
Value: !Ref SonarQubeUserSecret
QueuedTimeoutInMinutes: 10
ServiceRole: !GetAtt CodeBuildRole.Arn
Source:
Type: CODECOMMIT
Location: !GetAtt CodeCommitRepository.CloneUrlHttp
TimeoutInMinutes: 10
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Path: /
Policies:
- PolicyName: CodeBuildAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- logs:*
- codecommit:PostCommentForPullRequest
- codecommit:UpdatePullRequestApprovalState
Effect: Allow
Resource: '*'
- Action:
- codecommit:GitPull
Effect: Allow
Resource: !GetAtt CodeCommitRepository.Arn
- Action:
- secretsmanager:GetSecretValue
Effect: Allow
Resource: !Ref SonarQubeUserSecret
PullRequestTriggerCodeBuildRule:
Type: AWS::Events::Rule
Properties:
Description: 'Rule to trigger build from CodeCommit pull request'
EventPattern:
source:
- aws.codecommit
detail-type:
- 'CodeCommit Pull Request State Change'
detail:
event:
- pullRequestCreated
- pullRequestSourceBranchUpdated
resources:
- !GetAtt CodeCommitRepository.Arn
State: ENABLED
Targets:
- Arn: !GetAtt CodeBuildProject.Arn
Id: codebuild
RoleArn: !GetAtt CloudWatchEventsCodeBuildRole.Arn
InputTransformer:
InputTemplate: |
{
"sourceVersion": <sourceVersion>,
"artifactsOverride": {"type": "NO_ARTIFACTS"},
"environmentVariablesOverride": [
{
"name": "PULL_REQUEST_ID",
"value": <pullRequestId>,
"type": "PLAINTEXT"
},
{
"name": "REPOSITORY_NAME",
"value": <repositoryName>,
"type": "PLAINTEXT"
},
{
"name": "SOURCE_COMMIT",
"value": <sourceCommit>,
"type": "PLAINTEXT"
},
{
"name": "DESTINATION_COMMIT",
"value": <destinationCommit>,
"type": "PLAINTEXT"
},
{
"name" : "REVISION_ID",
"value": <revisionId>,
"type": "PLAINTEXT"
}
]
}
InputPathsMap:
sourceVersion: "$.detail.sourceCommit"
pullRequestId: "$.detail.pullRequestId"
repositoryName: "$.detail.repositoryNames[0]"
sourceCommit: "$.detail.sourceCommit"
destinationCommit: "$.detail.destinationCommit"
revisionId: "$.detail.revisionId"
CloudWatchEventsCodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: events-invoke-codebuild
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Resource: !GetAtt CodeBuildProject.Arn
Action:
- codebuild:StartBuild
SonarQubeUserSecret:
Type: AWS::SecretsManager::Secret
Properties:
Description: 'SonarQube user credentials'
SecretString: !Sub '{"username":"${SonarQubeUserName}","password":"${SonarQubePassword}"}'
SonarQubeUserSecretResourcePolicy:
Type: AWS::SecretsManager::ResourcePolicy
Properties:
SecretId: !Ref SonarQubeUserSecret
ResourcePolicy:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: !GetAtt CodeBuildRole.Arn
Action:
- secretsmanager:GetSecretValue
Resource: '*'
Condition:
'ForAnyValue:StringEquals':
'secretsmanager:VersionStage': AWSCURRENT