-
Notifications
You must be signed in to change notification settings - Fork 24
/
Jenkinsfile-production
302 lines (286 loc) · 13.9 KB
/
Jenkinsfile-production
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env groovy
void setBuildStatus(String message, String state) {
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/paladinarcher/padawan"],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
]);
}
pipeline {
agent {
dockerfile {
filename "Dockerfile-jenkins-build"
args "--entrypoint=''"
}
}
environment {
LC_ALL='en_US.UTF-8'
LANG='en_US.UTF-8'
METEOR_ALLOW_SUPERUSER=true
NODE_ENV='development'
}
stages {
stage('Istanbul') {
steps {
script {
env.JUNIT_REPORT_PATH = 'reports/report.xml'
// env.JUNIT_REPORT_PATH = '/report.xml'
}
//echo "${env.JUNIT_REPORT_PATH}"
// beggining of xml report tags
sh "cd $WORKSPACE"
sh '''
cd $WORKSPACE
rm -rf reports
mkdir reports
touch ./reports/report.xml
echo '<testsuites name="Mocha Tests">' >> ./reports/report.xml
echo ' <testsuite name="For Jenkins" tests="3" errors="0" failures="0" skipped="0" timestamp="2019-11-26T21:32:43" time="0.004">' >> ./reports/report.xml
'''
//sh "cat reports/report.xml"
sh '/usr/local/bin/meteor add lmieulet:meteor-coverage meteortesting:mocha'
sh '/usr/local/bin/meteor npm install --save-dev babel-plugin-istanbul'
echo 'Istanbul installed'
sh 'set +e' // this should help Jenkins not crash
sh '''
if /usr/local/bin/meteor npm run coverage:unit ; then
echo "Initial Istanbul suceeded"
else
echo "Initial Istanbul failed"
usr/local/bin/meteor npm run coverage:unit || usr/local/bin/meteor npm run coverage:unit || true
fi
'''
sh 'set -e' // this should help Jenkins not crash
echo 'coverage:unit script ran'
// middle of xml report tags
sh '''
failingTest='false'
linePrint ()
{
myReg=$(echo $1 | awk 'match($0, /pic high|pic low/) {print substr($0, RSTART, RLENGTH)}')
coverageName=$(echo $2 | awk 'match($0, /data-value="[^"]+/) {print substr($0, RSTART + 12, RLENGTH - 12)}')
percent=$(echo $1 | awk 'match($0, /data-value="[0-9][0-9][.][0-9][0-9]|data-value="[0-9][.][0-9][0-9]|data-value="[0-9][0-9][0-9]|data-value="[0-9][0-9]|data-value="[0-9]/) {print substr($0, RSTART + 12, RLENGTH - 12)}')
myName='name="'$myReg' '$percent'% coverage: '$coverageName'"'
qt='"'
if [ "$myReg" = "pic high" ]
then
echo "MYREG is pic high"
echo " <testcase classname=${qt}Istanbul Coverage$qt $myName time=${qt}0$qt>" >> ./reports/report.xml
echo " <system-out><![CDATA[$1 $2]]></system-out>" >> ./reports/report.xml
echo ' </testcase>' >> ./reports/report.xml
elif [ "$myReg" = "pic low" ]
then
echo "MYREG is pic low"
#failingTest='true'
#sed -i '1s/^/true/' ./failingTestVariableFileIstanbul
#echo "true" | ./failingTestVariableFileIstanbul
echo "true" >> ./failingTestVariableFileIstanbul
echo " <testcase classname=${qt}Istanbul Coverage$qt $myName time=${qt}0$qt status=${qt}Failed$qt>" >> ./reports/report.xml
echo ' <failure message="Coverage Percentage is below 80%"></failure>' >> ./reports/report.xml
echo " <system-out><![CDATA[$1 $2]]></system-out>" >> ./reports/report.xml
echo ' </testcase>' >> ./reports/report.xml
elif [ "$myReg" = "" ];
then
echo "MYREG is empty"
fi
echo "==============================\n"
}
prevLine='none'
echo "false" >> ./failingTestVariableFileIstanbul # if this gets set to true, then the build fails (Istanbul < 80%)
while read -r line; do linePrint "$line" "$prevLine"; prevLine="$line"; done < .coverage/index.html
'''
// closing of xml report tags
sh '''
echo ' </testsuite>' >> ./reports/report.xml
echo '</testsuites>' >> ./reports/report.xml
'''
// If there is an istanbul test below 80%, the folowing code should fail the pipeline
// turn on following code to make failing tests fail the build
sh '''
#failingTest=`cat ./failingTestVariableFileIstanbul`
#failingTest=$(cut -c1-4 < ./failingTestVariableFileIstanbul)
if grep -q "true" "./failingTestVariableFileIstanbul"; then
echo "true" >> ./failingTestVariableFileIstanbulFinal
failingTest="true"
else
echo "false" >> ./failingTestVariableFileIstanbulFinal
failingTest="false"
fi
echo "failingTest: "
echo "$failingTest"
if [ "$failingTest" = "true" ]
then
echo "failing the build because Istanbul is below 80%"
false
else
echo "Istanbul coverage is above 80%"
fi
'''
}
}
stage('Unit Tests') {
steps {
echo 'Testing...'
sh 'echo "LC_ALL=en_US.UTF-8" >> /etc/environment'
sh 'echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen'
sh 'echo "LANG=en_US.UTF-8" > /etc/locale.conf'
sh 'locale-gen en_US.UTF-8'
//sh 'meteor --allow-superuser remove-platform android'
sh 'meteor npm --allow-superuser install --save babel-runtime nightwatch'
sh 'meteor --allow-superuser test --settings settings.local.json --once --driver-package meteortesting:mocha'
}
}
stage('Functional Tests') {
steps {
sh 'java -jar /opt/selenium/selenium-server-standalone.jar > selenium_startup.log 2>&1 &'
sh 'meteor --allow-superuser reset'
sh 'meteor --allow-superuser --settings settings.prod.json > meteor_startup.log 2>&1 &'
sh '''
LOGFILE=meteor_startup.log
STR_SUCCESS="Started your app"
STR_FAILURE="Can't start"
STR_FAILURE2="Your application has errors"
STR_FAILURE3="Waiting for file change"
TIMEOUT=600
RETRY_SEC=10
ELAPSED_SEC=0
until [ "$ELAPSED_SEC" -ge "$TIMEOUT" ]; do
if grep -q "$STR_FAILURE" $LOGFILE; then
echo "failed to start"
cat $LOGFILE
exit 1
fi
if grep -q "$STR_FAILURE2" $LOGFILE; then
echo "failed to start"
cat $LOGFILE
exit 1
fi
if grep -q "$STR_FAILURE3" $LOGFILE; then
echo "failed to start"
cat $LOGFILE
exit 1
fi
if grep -q "$STR_SUCCESS" $LOGFILE; then
echo "started successfully"
exit 0
fi
sleep $RETRY_SEC
echo $((ELAPSED_SEC+=$RETRY_SEC))
done
echo "timed out"
exit 1
'''
//sh 'sleep 8m'
sh 'cat selenium_startup.log'
sh 'cat meteor_startup.log'
sh 'meteor npm --allow-superuser run test-e2e'
}
}
stage('Build') {
steps {
echo "Building... ${env.JOB_NAME} ${env.BUILD_ID}"
sh 'meteor --allow-superuser build /tmp --architecture os.linux.x86_64'
}
}
stage('Deploy') {
steps {
echo 'Deploying... '
sh "ls -ltrh /tmp"
sh "scp -o StrictHostKeyChecking=no -i /home/.ssh/wjInstance.pem settings.prod.json [email protected]:/home/ec2-user/docker/production/settings.prod.json"
sh "scp -o StrictHostKeyChecking=no -i /home/.ssh/wjInstance.pem `ls -1 /tmp/${env.JOB_NAME}*.tar.gz | head -n 1` [email protected]:/home/ec2-user/docker/production/padawan.tar.gz"
sh "ssh -o StrictHostKeyChecking=no -i /home/.ssh/wjInstance.pem [email protected] /home/ec2-user/bin/production-rebuild-up.sh"
}
}
stage('HealthCheck') {
steps {
script {
sh 'sleep 20'
response = sh(returnStdout: true, script: "curl --write-out %{http_code} --silent --output /dev/null http://app.developerlevel.com/healthCheck")
if (response == '209') {
sh "echo 'Health check was successful'"
} else {
sh """
echo 'Health check was not successful'
git checkout ${env.GIT_PREVIOUS_SUCCESSFUL_COMMIT}
echo 'Building... ${env.JOB_NAME} with commit... ${env.GIT_PREVIOUS_SUCCESSFUL_COMMIT}'
rm /tmp/${env.JOB_NAME}*.tar.gz
meteor --allow-superuser build /tmp --architecture os.linux.x86_64
echo 'Deploying... '
ls -ltrh /tmp
scp -o StrictHostKeyChecking=no -i /home/.ssh/wjInstance.pem settings.prod.json [email protected]:/home/ec2-user/docker/production/settings.prod.json"
scp -o StrictHostKeyChecking=no -i /home/.ssh/wjInstance.pem `ls -1 /tmp/${env.JOB_NAME}*.tar.gz | head -n 1` [email protected]:/home/ec2-user/docker/production/padawan.tar.gz"
ssh -o StrictHostKeyChecking=no -i /home/.ssh/wjInstance.pem [email protected] /home/ec2-user/bin/production-rebuild-up.sh"
echo 'Failing the pipeline build'
false
"""
}
}
}
}
}
post {
always {
// sh "cat ${env.JUNIT_REPORT_PATH}"
// echo "${env.JUNIT_REPORT_PATH}"
junit "${env.JUNIT_REPORT_PATH}"
}
success {
setBuildStatus("Build complete.", "SUCCESS")
script {
commitId = sh(returnStdout: true, script: "git rev-parse HEAD")
userEmail = sh(returnStdout: true, script: "git show -s --format='%ae' $commitId")
commitMsg = sh(returnStdout: true, script: "git show -s --format=%B $commitId")
slackMsg = "Build Succeeded - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)\n"
slackMsg += "Built using the Jenkins-production pipeline\n"
slackMsg += "Build User email: $userEmail"
slackMsg += "Commit Id: $commitId".trim() + "\n"
slackMsg += "Commit Message: $commitMsg"
//echo "$slackMsg"
}
slackSend (color: '#00FF00', message: "$slackMsg")
cleanWs()
}
failure {
setBuildStatus("Build failed.", "FAILURE")
script {
commitId = sh(returnStdout: true, script: "git rev-parse HEAD")
userEmail = sh(returnStdout: true, script: "git show -s --format='%ae' $commitId")
commitMsg = sh(returnStdout: true, script: "git show -s --format=%B $commitId")
slackMsg = "Build FAILED! - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)\n"
slackMsg += "Built using the Jenkins-production pipeline\n"
sh "echo 'checking if Istanbul coverage is below 80%'"
failIstanbul = sh(returnStdout: true, script: "awk '{print \$1; exit}' ./failingTestVariableFileIstanbulFinal")
failIstanbul = failIstanbul.substring(0,4);
failIstanbul.trim()
if ( "$failIstanbul".equals("true") ) {
sh "echo 'inside failingTest if'"
slackMsg += "Istanbul coverage below 80%\n"
}
slackMsg += "Build User email: $userEmail"
slackMsg += "Commit Id: $commitId".trim() + "\n"
slackMsg += "Commit Message: $commitMsg"
//echo "$slackMsg"
}
slackSend (color: '#FF0000', message: "$slackMsg")
cleanWs()
}
unstable {
setBuildStatus("Build unstable.", "UNSTABLE")
script {
commitId = sh(returnStdout: true, script: "git rev-parse HEAD")
userEmail = sh(returnStdout: true, script: "git show -s --format='%ae' $commitId")
commitMsg = sh(returnStdout: true, script: "git show -s --format=%B $commitId")
slackMsg = "Build UNSTABLE! - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)\n"
slackMsg += "Built using the Jenkins-production pipeline\n"
slackMsg += "Build User email: $userEmail"
slackMsg += "Commit Id: $commitId".trim() + "\n"
slackMsg += "Commit Message: $commitMsg"
//echo "$slackMsg"
}
slackSend (color: '#FFFF00', message: "$slackMsg")
cleanWs()
}
}
}