This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Jenkinsfile
287 lines (262 loc) · 9.42 KB
/
Jenkinsfile
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
#!groovy
import groovy.json.JsonSlurper
import java.net.URL
pipeline {
agent none
options {
timeout(time: 1, unit: 'DAYS')
disableConcurrentBuilds()
}
stages {
stage("Init") {
agent any
steps { initialize() }
}
stage("Build App") {
agent any
steps { buildApp() }
}
stage("Run App Security Scan") {
agent any
steps { runSecurityTest() }
}
stage("Build and Register Image") {
agent any
steps { buildAndRegisterDockerImage() }
}
stage("Deploy Image to Dev") {
agent any
steps { deployImage(env.ENVIRONMENT) }
}
stage("Test App in Dev") {
agent any
steps { runBrowserTest(env.ENVIRONMENT) }
}
stage("Deploy Image to Test") {
agent any
when { branch 'master' }
steps { deployImage('test') }
}
stage("Test App in Test") {
agent any
when { branch 'master' }
steps { runBrowserTest('test') }
}
stage("Proceed to prod?") {
agent none
when { branch 'master' }
steps { proceedTo('prod') }
}
stage("Deploy Image to Prod") {
agent any
when { branch 'master' }
steps { deployImage('prod') }
}
}
}
// ================================================================================================
// Initialization steps
// ================================================================================================
def initialize() {
env.SYSTEM_NAME = "DSO"
env.AWS_REGION = "us-east-1"
env.REGISTRY_URL = "https://912661153448.dkr.ecr.us-east-1.amazonaws.com"
env.MAX_ENVIRONMENTNAME_LENGTH = 32
setEnvironment()
env.IMAGE_NAME = "hello-world:" +
((env.BRANCH_NAME == "master") ? "" : "${env.ENVIRONMENT}-") +
env.BUILD_ID
showEnvironmentVariables()
}
def setEnvironment() {
def branchName = env.BRANCH_NAME.toLowerCase()
def environment = 'dev'
echo "branchName = ${branchName}"
if (branchName == "") {
showEnvironmentVariables()
throw "BRANCH_NAME is not an environment variable or is empty"
} else if (branchName != "master") {
//echo "split"
if (branchName.contains("/")) {
// ignore branch type
branchName = branchName.split("/")[1]
}
//echo "remove '-' characters'"
branchName = branchName.replace("-", "")
//echo "remove JIRA project name"
if (env.JIRA_PROJECT_NAME) {
branchName = branchName.replace(env.JIRA_PROJECT_NAME, "")
}
// echo "limit length"
branchName = branchName.take(env.MAX_ENVIRONMENTNAME_LENGTH as Integer)
environment += "-" + branchName
}
echo "Using environment: ${environment}"
env.ENVIRONMENT = environment
}
def showEnvironmentVariables() {
sh 'env | sort > env.txt'
sh 'cat env.txt'
}
// ================================================================================================
// Build steps
// ================================================================================================
def buildApp() {
dir("webapp") {
withDockerContainer("maven:3.5.0-jdk-8-alpine") { sh "mvn clean install"}
archiveArtifacts '**/target/spring-boot-web-jsp-1.0.war'
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'] )
}
}
def buildAndRegisterDockerImage() {
def buildResult
docker.withRegistry(env.REGISTRY_URL) {
echo "Connect to registry at ${env.REGISTRY_URL}"
dockerRegistryLogin()
echo "Build ${env.IMAGE_NAME}"
buildResult = docker.build(env.IMAGE_NAME)
echo "Register ${env.IMAGE_NAME} at ${env.REGISTRY_URL}"
buildResult.push()
echo "Disconnect from registry at ${env.REGISTRY_URL}"
sh "docker logout ${env.REGISTRY_URL}"
}
}
def dockerRegistryLogin() {
def login_command = ""
withDockerContainer("garland/aws-cli-docker") {
login_command = sh(returnStdout: true,
script: "aws ecr get-login --region ${AWS_REGION} | sed -e 's|-e none||g'"
)
}
sh "${login_command}"
}
// ================================================================================================
// Deploy steps
// ================================================================================================
def deployImage(environment) {
def context = getContext(environment)
def ip = findIp(environment)
echo "Deploy ${env.IMAGE_NAME} to '${environment}' environment (in context: ${context})"
sshagent (credentials: ["${env.SYSTEM_NAME}-${context}-helloworld"]) {
sh """ssh -o StrictHostKeyChecking=no -tt \"ec2-user@${ip}\" \
sudo /opt/dso/deploy-app \"${env.IMAGE_NAME}\" \"${env.REGISTRY_URL}\"
"""
}
echo "Ensure site is up"
// TODO: Replace with wait loop that tests if the siste is responsive
sleep time: 10, unit: 'SECONDS'
}
def getContext(environment) {
return (env.BRANCH_NAME == 'master') ? environment : 'dev'
}
// ================================================================================================
// Test steps
// ================================================================================================
def runSecurityTest() {
def sonarReportDir = "target/sonar"
def jenkinsIP = findJenkinsIp()
dir("webapp") {
withDockerContainer("maven:3.5.0-jdk-8-alpine") {
sh "mvn sonar:sonar -Dsonar.host.url=http://${jenkinsIP}:9000"
}
sh "ls -al ${sonarReportDir}"
//archiveArtifacts "**/${sonarReportDir}/*.txt"
}
}
def runBrowserTest(environment) {
def ip = findIp(environment)
def workDir = "src/test"
def testsDir="${workDir}/python"
def resultsDir = "target/browser-test-results/${environment}"
def resultsPrefix = "${resultsDir}/results-${env.BUILD_ID}"
def sitePackagesDir="${workDir}/resources/lib/python2.6/site-packages"
def script = """
export PYTHONPATH="${sitePackagesDir}:${testsDir}"
mkdir -p ${resultsDir}
/usr/bin/python -B -u ./${testsDir}/helloworld/test_suite.py \
"--base-url=http://${ip}" \
--webdriver-class=PhantomJS\
--reuse-driver \
--environment ${environment} \
--default-wait=15 \
--verbose \
--default-window-width=800 \
--test-reports-dir=${resultsDir} \
--results-file=${resultsPrefix}.csv
"""
dir("webapp") {
withDockerContainer("killercentury/python-phantomjs") { sh "${script}" }
step([$class: 'JUnitResultArchiver', testResults: "**/${resultsDir}/TEST-*.xml"])
sh "ls -lhr ${resultsDir}"
archiveArtifacts "**/${resultsPrefix}.*"
}
}
// ================================================================================================
// Utility steps
// ================================================================================================
def proceedTo(environment) {
def description = "Choose 'yes' if you want to deploy to this build to " +
"the ${environment} environment"
def proceed = 'no'
timeout(time: 4, unit: 'HOURS') {
proceed = input message: "Do you want to deploy the changes to ${environment}?",
parameters: [choice(name: "Deploy to ${environment}", choices: "no\nyes",
description: description)]
if (proceed == 'no') {
error("User stopped pipeline execution")
}
}
}
def findIp(environment) {
def ip = ""
withDockerContainer("garland/aws-cli-docker") {
ip = sh(returnStdout: true,
script: """aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
"Name=tag:Name,Values=${env.SYSTEM_NAME}-${environment}-helloworld" \
--query "Reservations[].Instances[].{Ip:PrivateIpAddress}" \
--output text --region ${env.AWS_REGION} | tr -d '\n'
"""
)
}
echo "ip=[${ip}]"
return ip
}
def findJenkinsIp() {
def ip = ""
withDockerContainer("garland/aws-cli-docker") {
ip = sh(returnStdout: true,
script: """aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
"Name=tag:Name,Values=${env.SYSTEM_NAME}-shared-jenkins" \
--query "Reservations[].Instances[].{Ip:PrivateIpAddress}" \
--output text --region ${env.AWS_REGION} | tr -d '\n'
"""
)
}
echo "ip=[${ip}]"
return ip
}
// def startSonarQube() {
// if (!isSonarQubeRunning()) {
// echo "Restarting SonarQube"
// stopSonarQube()
// sh "docker run -d --name ${env.SONARQUBE_IMAGE_NAME} -p 9000:9000 -p 9092:9092 sonarqube"
// } else {
// echo "SonarQube already running"
// }
// }
// def stopSonarQube() {
// if (isSonarQubeRunning()) {
// sh "docker stop ${env.SONARQUBE_IMAGE_NAME}"
// sh "docker rm ${env.SONARQUBE_IMAGE_NAME}"
// }
// }
// def isSonarQubeRunning() {
// def imageID = sh(returnStdout: true, script: """
// docker ps | grep '${env.SONARQUBE_IMAGE_NAME}' | awk -F" " '{print \$1;}' | tr -d '\n'
// """
// )
// echo "SonarQube ImageID=${imageID}"
// return (imageID?.trim())
// }