This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
forked from polycube-network/polycube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JenkinsfileTestSameInstance
224 lines (180 loc) · 7.21 KB
/
JenkinsfileTestSameInstance
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
#!/bin/groovy
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
def getGitAuthor() {
def commit = sh(returnStdout: true, script: 'git rev-parse HEAD')
def author = sh(returnStdout: true, script: "git --no-pager show -s --format='%an' ${commit}").trim()
return author
}
def getLastCommitMessage() {
def message = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
return message
}
def notifySlack(String buildStatus = 'STARTED', attachments = null) {
// build status of null means SUCCESS
buildStatus = buildStatus ?: 'SUCCESS'
// Default values
def colorName = 'RED'
def colorCode = '#FF0000'
def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def summary = "${subject} (${env.BUILD_URL})"
// Override default values based on build status
if (buildStatus == 'STARTED') {
color = 'GREY'
colorCode = '#D4DADF'
} else if (buildStatus == 'SUCCESS') {
color = 'GREEN'
colorCode = "good"
} else if (buildStatus == 'UNSTABLE') {
color = 'YELLOW'
colorCode = "warning"
} else {
color = 'RED'
colorCode = "danger"
}
if (attachments != null) {
// Send notifications
slackSend (color: colorCode, message: summary, channel: 'jenkins', attachments: attachments.toString())
} else {
slackSend (color: colorCode, message: summary)
}
}
String getBuildStatus(String buildStatus = 'STARTED') {
// build status of null means SUCCESS
buildStatus = buildStatus ?: 'SUCCESS'
def status = buildStatus
// Override default values based on build status
if (buildStatus == 'STARTED') {
status = 'STARTED'
} else if (buildStatus == 'SUCCESS') {
status = 'SUCCESS'
} else if (buildStatus == 'UNSTABLE') {
status = 'UNSTABLE'
} else {
status = 'FAILED'
}
return status
}
String getBuildColor(String buildStatus = 'STARTED') {
// build status of null means SUCCESS
buildStatus = buildStatus ?: 'SUCCESS'
def color = "danger"
// Override default values based on build status
if (buildStatus == 'STARTED') {
status = '#D4DADF'
} else if (buildStatus == 'SUCCESS') {
status = 'good'
} else if (buildStatus == 'UNSTABLE') {
status = 'warning'
} else {
status = 'danger'
}
return status
}
def getTestSummary() {
def props = readJSON file: 'tests/result.json'
tests_summary = ""
if (props != null) {
int total = String.valueOf(props.total)
int passed = String.valueOf(props.passed)
int failed = total - passed
tests_summary = "Passed: " + (passed)
tests_summary = tests_summary + (", Failed: " + (failed))
tests_summary = tests_summary + (", Total: " + total)
} else {
tests_summary = "No tests found"
}
return tests_summary
}
def getFailedTestsString(String failedTestsFile) {
def version = readFile "tests/${failedTestsFile}"
def failedTestsString = "```"
failedTestsString = failedTestsString + version + "```"
return failedTestsString
}
pipeline {
agent none
stages {
stage('Testing Same Instance') {
agent {
label "testing-2"
}
steps {
echo 'Start building polycube on testing-2 machine'
sh 'export INSTALL_CLEAN_POLYCUBE=true && ./scripts/install.sh'
echo 'Start testing polycube services'
sh 'cd tests/ && ./run-tests.sh false'
}
post {
always {
script {
def props = readJSON file: 'tests/result.json'
def testsSuccededParameter = new StringParameterValue("NUM_TESTS_SUCCEDED", props.passed)
def testsTotalParameter = new StringParameterValue("TOT_TESTS", props.total)
def testsStageParameter = new StringParameterValue("TEST_STAGE_NAME", "TESTING_SAME_INSTANCE")
currentBuild.getRawBuild().actions.add(new ParametersAction(testsSuccededParameter))
currentBuild.getRawBuild().actions.add(new ParametersAction(testsTotalParameter))
currentBuild.getRawBuild().actions.add(new ParametersAction(testsStageParameter))
int total = String.valueOf(props.total)
int passed = String.valueOf(props.passed)
int failed = total - passed
if(failed > 0) {
def attachments = getSlackAttachment(currentBuild, new String("Testing Same Instance"), props.test_log)
def final_string = attachments.toString()
echo "${final_string}"
notifySlack(currentBuild.result, attachments)
}
sh 'rm -rf ${WORKSPACE}/*'
}
}
}
}
}
}
def getSlackAttachment(currentBuild, stageName, String failedTestsString = null) {
JSONArray attachments = new JSONArray();
JSONObject attachment = new JSONObject();
JSONArray fields = new JSONArray();
JSONArray mrkdwn_in = new JSONArray();
JSONObject branch_json = new JSONObject();
JSONObject stage_json = new JSONObject();
JSONObject tests_result_json = new JSONObject();
JSONObject last_commit_json = new JSONObject();
JSONObject failed_tests_json = new JSONObject();
attachment.put('pretext', new String("Build status: *${getBuildStatus(currentBuild.result)}*"));
attachment.put('color', getBuildColor(currentBuild.result));
attachment.put('author_name', getGitAuthor());
attachment.put('title', new String("${env.JOB_NAME}, build #${env.BUILD_NUMBER}"));
attachment.put('title_link', env.BUILD_URL);
mrkdwn_in.add("fields");
mrkdwn_in.add("pretext");
mrkdwn_in.add("text");
attachment.put('mrkdwn_in', mrkdwn_in);
stage_json.put('title', "Stage");
stage_json.put('value', stageName)
stage_json.put('short', true)
branch_json.put('title', "Branch");
branch_json.put('value', env.GIT_BRANCH)
branch_json.put('short', true)
tests_result_json.put('title', "Test Results");
tests_result_json.put('value', getTestSummary())
tests_result_json.put('short', true)
last_commit_json.put('title', "Last Commit");
last_commit_json.put('value', getLastCommitMessage());
last_commit_json.put('short', true);
fields.add(stage_json);
fields.add(branch_json);
fields.add(tests_result_json);
fields.add(last_commit_json);
attachment.put('fields', fields);
failed_tests_json.put('title', "Failed Tests");
failed_tests_json.put('color', getBuildColor(currentBuild.result));
failed_tests_json.put('text', getFailedTestsString(failedTestsString));
failed_tests_json.put('mrkdwn_in', mrkdwn_in);
failed_tests_json.put('footer', "Jenkins Polycube API");
failed_tests_json.put('footer_icon', "https://jenkins.io/images/logos/jenkins/jenkins.png");
failed_tests_json.put('ts', (int)(new Date().getTime()/1000));
attachments.add(attachment);
attachments.add(failed_tests_json);
return attachments
}