-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjenkinsfiles_serverless.txt
231 lines (199 loc) · 8.73 KB
/
jenkinsfiles_serverless.txt
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
#!/usr/bin/env groovy
// Import Shared libraries
@Library('P-Funk')
// Import needed classes
import org.daas_watchguard.basefunctions.*
import org.daas_watchguard.notifications.*
import org.daas_watchguard.unittest.*
import org.daas_watchguard.validators.*
import org.wgc.deployment.accountgroup.*
import org.wgc.deployment.environment.*
import org.wgc.deployment.region.*
import org.wgc.jenkins.parameter.*
import org.wgc.jenkins.property.*
import org.wgc.jenkins.config.*
import org.wgc.deployment.overrides.*
import org.wgc.sonarqube.*
import org.wgc.newrelic.*
// Initialize all objects
def microserviceBuild = new Microservices() // org.daas_watchguard.basefunctions.Microservices
def notifications = new Teams() // org.daas_watchguard.notifications.Teams
def pylintTests = new pylint()
def pythonTests = new Python()
def sqTesting = new sonarqube()
def githubUser = "jenkins-gh"
def githubEnterprise = "github.infra.int.daas-watchguard.com"
def teamName = "Platform"
def repoName = "inventory-svc"
def githubUrl = "https://${githubEnterprise}/${teamName}/${repoName}.git"
def deployGithubUrl = "https://${githubEnterprise}/${teamName}/${repoName}-aws.git"
def infraGithubUrl = "https://${githubEnterprise}/microservice-build-and-security-files/${repoName}-infra.git"
def pythonLambdaLayerGithubUrl = "https://${githubEnterprise}/wgc-common/wg-python-lambda-layer"
String defaultAccountGroup = "WGCloud"
AccountGroups accountGroups = new AccountGroups(this, defaultAccountGroup)
accountGroups.loadConfig()
JobProperties jobProps = new JobProperties(this)
jobProps.addStandard([JobProperties.DISABLE_CONCURRENT_BUILDS, JobProperties.DISCARD_OLD_BUILDS])
JobParameters jobParams = new JobParameters(this)
jobParams.addStandard([JobParameters.ACCOUNT_GROUP, JobParameters.REGION])
jobParams.addStandard([JobParameters.DEPLOY_CODE_BRANCH_OVERRIDE, JobParameters.SECURITY_CODE_BRANCH_OVERRIDE])
String accountGroup = accountGroups.getAccountGroupForSelected(params.get(JobParameters.ACCOUNT_GROUP))
String branch = env.BRANCH_NAME
String environment = accountGroups.getEnvironmentForSelected(accountGroup, null, branch)
String region = accountGroups.getRegionForSelected(accountGroup, environment, params.get(JobParameters.REGION))
String deployCodeBranch = BranchOverrides.getDeployCodeBranch(params.get(JobParameters.DEPLOY_CODE_BRANCH_OVERRIDE), environment)
String securityCodeBranch = BranchOverrides.getSecurityCodeBranch(params.get(JobParameters.SECURITY_CODE_BRANCH_OVERRIDE), environment)
def unitTestsPythonPath = "."
SonarQubeAPI sonarQubeAPI = new SonarQubeAPI(this, environment, teamName, repoName, branch)
NewRelic newRelic = new NewRelic(this, environment)
newRelic.loadConfig()
boolean isNRAgentInstallAndTag = true
JobConfig jobConfig = new JobConfig(this, jobParams, jobProps, environment)
jobConfig.build()
echo("AccountGroup: ${accountGroup}")
echo("Branch: ${branch}")
echo("Environment: ${environment}")
echo("Region: ${region}")
echo("Deploy Branch: ${deployCodeBranch}")
echo("Security Branch: ${securityCodeBranch}")
node("cje_sam_cli_base") {
// Send starting notification
notifications.microserviceTeamsNotification(teamName, "started", branch)
// Initial cleanup
deleteDir()
// Main functions
try {
stage('Checkout') {
dir('files') {
println("Service code branch: ${branch}")
microserviceBuild.checkOut(githubUrl, branch, githubUser)
}
dir('deploy') {
println("Deploy code branch: ${deployCodeBranch}")
microserviceBuild.checkOut(deployGithubUrl, deployCodeBranch, githubUser)
}
dir('infrafiles') {
println("Security code branch: ${securityCodeBranch}")
microserviceBuild.checkOut(infraGithubUrl, securityCodeBranch, githubUser)
}
dir('wg-python-lambda-layer') {
println("Checking out wg-python-lambda-layer")
microserviceBuild.checkOut(pythonLambdaLayerGithubUrl, securityCodeBranch, githubUser)
}
}
stage('Install Requirements') {
dir('deploy') {
script {
if (fileExists('requirements.txt')) {
sh('python3 -m pip install -r requirements.txt')
}
}
}
dir('files') {
script {
if (fileExists('requirements.txt')) {
sh('python3 -m pip install -r requirements.txt')
}
}
}
}
stage("Code Style Tests") {
dir("files"){
try {
println('Flake8 Testing')
sh('python -m flake8 --tee --output-file flake8.out --format junit-xml --exit-zero .')
pylintTests.pylintTestResults()
} catch(e) {
println(e)
}
}
}
stage("Unit Tests"){
dir("files"){
try {
println("Unit Tests")
sh(pythonTests.pythonUnitTestWithCoverage(unitTestsPythonPath))
pythonTests.pythonUnitTestResults()
pythonTests.pythonCoverageResults()
} catch(e) {
println(e)
println("No Unit Tests to run.")
}
}
}
stage("Static Analysis") {
dir("files"){
try {
steps.withCredentials([string(credentialsId: 'sonarqube-token', variable: 'AUTH_TOKEN')]) {
sonarQubeAPI.setSonarQubeTags("wgcloudplatform", AUTH_TOKEN)
sonarQubeAPI.setQualityGate("Mid-QualityGate", AUTH_TOKEN)
}
} catch (e) {
println(e)
println("Error in setting up quality gate name")
}
sqTesting.sqScannerWithProperties(teamName, repoName, branch)
sqTesting.sqQuality()
}
}
stage("SAM CLI Deploy") {
dir('deploy') {
if ((environment == 'dev') || (environment == 'qa')) {
deployTag = 'latest'
} else {
deployTag = environment + '-latest'
}
sh("python3 run.py --environment ${environment} --branch ${branch} --region ${region} --build ${deployTag}")
}
}
stage("Get Lambda Names") {
println ('Get lambda names for installing new relic agent and tagging')
def sts = accountGroups.getStsCreds(accountGroup, environment)
String awsAccessKeyId = "AWS_ACCESS_KEY_ID=${sts[0]}"
String awsSecretAccessKey = "AWS_SECRET_ACCESS_KEY=${sts[1]}"
String awsSessionToken = "AWS_SESSION_TOKEN=${sts[2]}"
if (environment == 'dev') {
prefix = branch
} else {
prefix = environment
}
withEnv([awsAccessKeyId, awsSecretAccessKey, awsSessionToken]) {
cmd = "aws cloudformation describe-stack-resources --region ${region} --stack-name ${prefix}-inventory-svc --query \"StackResources[?ResourceType=='AWS::Lambda::Function'].PhysicalResourceId\" --output json > function_names.json"
sh(cmd)
lambdaNames = sh (
script: 'cat function_names.json | xargs | tr -d "[:space:]" | sed "s:^.\\(.*\\).$:\\1:"',
returnStdout: true
).trim()
println("lambdaNames: ${lambdaNames}")
}
}
stage('Install New Relic agent') {
if (isNRAgentInstallAndTag) {
newRelic.installNRAgentForLambda(environment, accountGroups, accountGroup, region, lambdaNames)
} else {
println('Skipping new Relic tagging.')
}
}
stage('Add New Relic Tag') {
if (isNRAgentInstallAndTag) {
tagsMap= [
'wg_purpose_product':'wgcplatform',
'wg_purpose_serviceid': repoName,
'wg_purpose_environment': environment,
'wg_purpose_region': region
]
newRelic.applyNRTag(lambdaNames, tagsMap)
} else {
println('Skipping new Relic tagging.')
}
}
notifications.microserviceTeamsNotification(teamName, "success", branch)
} catch(e) {
notifications.microserviceTeamsNotification(teamName, "failed", branch)
throw e
} finally {
// This final cleanup prevents successful and
// failed build files from sitting on the volume
deleteDir()
}
}