This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 868
/
Jenkinsfile
250 lines (249 loc) · 13.7 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
pipeline {
agent any
options {
// Running builds concurrently could cause a race condition with
// building the Docker image.
disableConcurrentBuilds()
// Only keep the last 5 builds.
buildDiscarder(logRotator(numToKeepStr: '5'))
}
stages {
stage('Run Integration Tests') {
when {
anyOf {
changeRequest()
}
}
options {
lock(resource: "lock_${env.JOB_NAME}")
}
environment {
//spawns GITHUB_USR and GITHUB_PSW environment variables
GITHUB=credentials('devops-mycroft_mycroft-skills-vk-tests')
// Some branches have a "/" in their name (e.g. feature/new-and-cool)
// Some commands, such as those tha deal with directories, don't
// play nice with this naming convention. Define an alias for the
// branch name that can be used in these scenarios.
BRANCH_ALIAS = sh(
script: 'echo $BRANCH_NAME | sed -e "s#/#_#g"',
returnStdout: true
).trim()
}
steps {
echo "Pushing to temporary test repo..."
sh 'git remote add mine git+ssh://[email protected]/forslund/mycroft-skills.git || true'
sh 'git push -uf mine ${BRANCH_NAME}'
sh 'docker build \
--build-arg major_release=21.2.1 \
--build-arg platform=mycroft_mark_1 \
--build-arg pull_request=$BRANCH_NAME \
--build-arg branch_name=$BRANCH_NAME \
--build-arg repo_url=https://github.com/forslund/mycroft-skills \
--build-arg github_api_key=$GITHUB_PSW \
--no-cache \
--label build=${JOB_NAME} \
-t voight-kampff-skill:${BRANCH_ALIAS} .'
echo 'Running Tests'
timeout(time: 100, unit: 'MINUTES')
{
sh 'mkdir -p $HOME/skills/$BRANCH_ALIAS/allure'
sh 'mkdir -p $HOME/skills/$BRANCH_ALIAS/mycroft-logs'
sh 'docker run \
--volume "$HOME/voight-kampff/identity:/root/.config/mycroft/identity" \
--volume "$HOME/skills/$BRANCH_ALIAS/allure:/root/allure" \
--volume "$HOME/skills/$BRANCH_ALIAS/mycroft-logs:/var/log/mycroft" \
--label build=${JOB_NAME} \
voight-kampff-skill:${BRANCH_ALIAS} \
-f allure_behave.formatter:AllureFormatter \
-o /root/allure/allure-result --tags ~@xfail'
}
}
post {
always {
echo 'Report Test Results'
echo 'Changing ownership of allure results...'
sh 'docker run \
--volume "$HOME/skills/$BRANCH_ALIAS/allure:/root/allure" \
--entrypoint=/bin/bash \
--label build=${JOB_NAME} \
voight-kampff-skill:${BRANCH_ALIAS} \
-x -c "chown $(id -u $USER):$(id -g $USER) \
-R /root/allure/"'
echo 'Changing ownership of Mycroft logs...'
sh 'docker run \
--volume "$HOME/skills/$BRANCH_ALIAS/mycroft-logs:/var/log/mycroft" \
--entrypoint=/bin/bash \
--label build=${JOB_NAME} \
voight-kampff-skill:${BRANCH_ALIAS} \
-x -c "chown $(id -u $USER):$(id -g $USER) \
-R /var/log/mycroft/"'
echo 'Transferring...'
sh 'rm -rf allure-result/*'
sh 'mv $HOME/skills/$BRANCH_ALIAS/allure/allure-result allure-result'
// This directory should now be empty, rmdir will intentionally fail if not.
sh 'rmdir $HOME/skills/$BRANCH_ALIAS/allure'
script {
allure([
includeProperties: false,
jdk: '',
properties: [],
reportBuildPolicy: 'ALWAYS',
results: [[path: 'allure-result']]
])
}
unarchive mapping:['allure-report.zip': 'allure-report.zip']
sh 'zip mycroft-logs.zip -r $HOME/skills/$BRANCH_ALIAS/mycroft-logs'
sh 'rm -rf $HOME/skills/$BRANCH_ALIAS/mycroft-logs'
// This directory should now be empty, rmdir will intentionally fail if not.
sh 'rmdir $HOME/skills/$BRANCH_ALIAS'
sh (
label: 'Publish Report to Web Server',
script: '''
ssh [email protected] "mkdir -p ~/allure-reports/skills/${BRANCH_ALIAS}";
scp allure-report.zip [email protected]:~/allure-reports/skills/${BRANCH_ALIAS}/;
ssh [email protected] "unzip -o ~/allure-reports/skills/${BRANCH_ALIAS}/allure-report.zip -d ~/allure-reports/skills/${BRANCH_ALIAS}/";
ssh [email protected] "rm -rf /var/www/voight-kampff/skills/${BRANCH_ALIAS}";
ssh [email protected] "mv ~/allure-reports/skills/${BRANCH_ALIAS}/allure-report /var/www/voight-kampff/skills/${BRANCH_ALIAS}"
ssh [email protected] "rm ~/allure-reports/skills/${BRANCH_ALIAS}/allure-report.zip";
ssh [email protected] "rmdir ~/allure-reports/skills/${BRANCH_ALIAS}";
ssh [email protected] "mkdir -p ~/mycroft-logs/skills/${BRANCH_ALIAS}";
scp mycroft-logs.zip [email protected]:~/mycroft-logs/skills/${BRANCH_ALIAS}/;
ssh [email protected] "mkdir -p /var/www/voight-kampff/skills/${BRANCH_ALIAS}/logs"
ssh [email protected] "unzip -oj ~/mycroft-logs/skills/${BRANCH_ALIAS}/mycroft-logs.zip -d /var/www/voight-kampff/skills/${BRANCH_ALIAS}/logs/";
ssh [email protected] "rm ~/mycroft-logs/skills/${BRANCH_ALIAS}/mycroft-logs.zip";
ssh [email protected] "rmdir ~/mycroft-logs/skills/${BRANCH_ALIAS}";
'''
)
echo 'Report Published'
}
failure {
// Send allure report to Pull request
script {
// Create comment for Pull Requests
if (env.CHANGE_ID) {
echo 'Sending PR comment'
pullRequest.comment('Voight Kampff Integration Test Failed ([Results](https://reports.mycroft.ai/skills/' + env.BRANCH_ALIAS + ')). ' +
'\nMycroft logs are also available: ' +
'[skills.log](https://reports.mycroft.ai/skills/' + env.BRANCH_ALIAS + '/logs/skills.log), ' +
'[audio.log](https://reports.mycroft.ai/skills/' + env.BRANCH_ALIAS + '/logs/audio.log), ' +
'[voice.log](https://reports.mycroft.ai/skills/' + env.BRANCH_ALIAS + '/logs/voice.log), ' +
'[bus.log](https://reports.mycroft.ai/skills/' + env.BRANCH_ALIAS + '/logs/bus.log), ' +
'[enclosure.log](https://reports.mycroft.ai/skills/' + env.BRANCH_ALIAS + '/logs/enclosure.log)')
}
}
// Send failure email containing a link to the Jenkins build
// the results report and the console log messages to Mycroft
// developers, the developers of the pull request and the
// developers that caused the build to fail.
echo 'Sending Failure Email'
emailext (
attachLog: true,
subject: "FAILURE - Skills Integration Tests - Build ${BRANCH_NAME} #${BUILD_NUMBER}",
body: """
<p>
One or more integration tests failed. Use the
resources below to identify the issue and fix
the failing tests.
</p>
<br>
<p>
<a href='${BUILD_URL}'>
Jenkins Build Details
</a>
 (Requires account on Mycroft's Jenkins instance)
</p>
<br>
<p>
<a href='https://reports.mycroft.ai/skills/${BRANCH_ALIAS}'>
Report of Test Results
</a>
</p>
<br>
<p>
Mycroft logs are also available:
<ul>
<li><a href='https://reports.mycroft.ai/skills/${BRANCH_ALIAS}/logs/skills.log'>skills.log</a></li>
<li><a href='https://reports.mycroft.ai/skills/${BRANCH_ALIAS}/logs/audio.log'>audio.log</a></li>
<li><a href='https://reports.mycroft.ai/skills/${BRANCH_ALIAS}/logs/voice.log'>voice.log</a></li>
<li><a href='https://reports.mycroft.ai/skills/${BRANCH_ALIAS}/logs/bus.log'>bus.log</a></li>
<li><a href='https://reports.mycroft.ai/skills/${BRANCH_ALIAS}/logs/enclosure.log'>enclosure.log</a></li>
</ul>
</p>
<br>
<p>Jenkins console log is attached.</p>""",
replyTo: '[email protected]',
to: '[email protected]',
recipientProviders: [
[$class: 'RequesterRecipientProvider'],
[$class:'CulpritsRecipientProvider'],
[$class:'DevelopersRecipientProvider']
]
)
}
success {
// Send report to Pull request
script {
if (env.CHANGE_ID) {
echo 'Sending PR comment'
pullRequest.comment('Voight Kampff Integration Test Succeeded ([Results](https://reports.mycroft.ai/skills/' + env.BRANCH_ALIAS + '))')
}
}
// Send success email containing a link to the Jenkins build
// and the results report to Mycroft developers, the developers
// of the pull request and the developers that caused the
// last failed build.
echo 'Sending Success Email'
emailext (
subject: "SUCCESS - Skills Integration Tests - Build ${BRANCH_NAME} #${BUILD_NUMBER}",
body: """
<p>
All integration tests passed. No further action required.
</p>
<br>
<p>
<a href='${BUILD_URL}'>
Jenkins Build Details
</a>
 (Requires account on Mycroft's Jenkins instance)
</p>
<br>
<p>
<a href='https://reports.mycroft.ai/skills/${BRANCH_ALIAS}'>
Report of Test Results
</a>
</p>""",
replyTo: '[email protected]',
to: '[email protected]',
recipientProviders: [
[$class: 'RequesterRecipientProvider'],
[$class:'CulpritsRecipientProvider'],
[$class:'DevelopersRecipientProvider']
]
)
}
}
}
}
post {
success {
// Docker images should remain upon failure for troubleshooting purposes. However,
// if the stage is successful, there is no reason to look back at the Docker image. In theory
// broken builds will eventually be fixed so this step should run eventually for every PR
sh(
label: 'Delete Docker Image on Success',
script: '''
docker image prune --all --force --filter label=build=${JOB_NAME};
'''
)
}
cleanup {
sh(
label: 'Docker Container and Image Cleanup',
script: '''
docker container prune --force;
docker image prune --force;
'''
)
}
}
}