forked from mozilla/kitsune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
175 lines (167 loc) · 4.12 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
import groovy.json.JsonOutput
/** Map of Tox environments and associated capabilities */
def environments = [
desktop: [
browserName: 'Firefox',
version: '47.0',
platform: 'Windows 7'
],
mobile: [
browserName: 'Browser',
platformName: 'Android',
platformVersion: '5.1',
deviceName: 'Android Emulator',
appiumVersion: '1.5.3'
]
]
/** Transform a map into a list of maps
*
* @param aMap map to transform
* @return a list of maps
*/
@NonCPS
def entrySet(aMap) {
aMap.collect {
k, v -> [key: k, value: v]
}
}
/** Write capabilities to JSON file
*
* @param desiredCapabilities capabilities to include in the file
*/
def writeCapabilities(desiredCapabilities) {
def defaultCapabilities = [
build: env.BUILD_TAG,
public: 'public restricted'
]
def capabilities = defaultCapabilities.clone()
capabilities.putAll(desiredCapabilities)
def json = JsonOutput.toJson([capabilities: capabilities])
writeFile file: 'capabilities.json', text: json
}
/** Run Tox
*
* @param environment test environment to run
*/
def runTox(environment) {
def processes = env.PYTEST_PROCESSES ?: 'auto'
try {
wrap([$class: 'AnsiColorBuildWrapper']) {
withCredentials([[
$class: 'StringBinding',
credentialsId: 'SAUCELABS_API_KEY',
variable: 'SAUCELABS_API_KEY']]) {
withEnv(["PYTEST_ADDOPTS=${PYTEST_ADDOPTS} " +
"-n=${processes} " +
"--base-url=${PYTEST_BASE_URL} " +
"--driver=SauceLabs " +
"--variables=capabilities.json " +
"--color=yes"]) {
sh "tox -e ${environment}"
}
}
}
} catch(err) {
currentBuild.result = 'FAILURE'
throw err
} finally {
dir('results') {
stash environment
}
}
}
/** Send a notice to #fxtest-alerts on irc.mozilla.org with the build result
*
* @param result outcome of build
*/
def ircNotification(result) {
def nick = "fxtest${BUILD_NUMBER}"
def channel = '#fx-test-alerts'
result = result.toUpperCase()
def message = "Project ${JOB_NAME} build #${BUILD_NUMBER}: ${result}: ${BUILD_URL}"
node {
sh """
(
echo NICK ${nick}
echo USER ${nick} 8 * : ${nick}
sleep 5
echo "JOIN ${channel}"
echo "NOTICE ${channel} :${message}"
echo QUIT
) | openssl s_client -connect irc.mozilla.org:6697
"""
}
}
stage('Checkout') {
node {
timestamps {
deleteDir()
checkout scm
stash 'workspace'
}
}
}
def builders = [:]
for (entry in entrySet(environments)) {
def environment = entry.key
def capabilities = entry.value
builders[(environment)] = {
node {
timeout(time: 1, unit: 'HOURS') {
timestamps {
deleteDir()
unstash 'workspace'
writeCapabilities(capabilities)
withCredentials([[
$class: 'FileBinding',
credentialsId: 'SUMO_VARIABLES',
variable: 'VARIABLES']]) {
withEnv(["PYTEST_ADDOPTS=--variables=${VARIABLES}"]) {
runTox(environment)
}
}
}
}
}
}
}
try {
stage('Test') {
parallel builders
}
} catch(err) {
currentBuild.result = 'FAILURE'
ircNotification(currentBuild.result)
mail(
body: "${BUILD_URL}",
from: "[email protected]",
replyTo: "[email protected]",
subject: "Build failed in Jenkins: ${JOB_NAME} #${BUILD_NUMBER}",
to: "[email protected]")
throw err
} finally {
stage('Results') {
def keys = environments.keySet() as String[]
def htmlFiles = []
node {
deleteDir()
sh 'mkdir results'
dir('results') {
for (int i = 0; i < keys.size(); i++) {
// Unstash results from each environment
unstash keys[i]
htmlFiles.add("${keys[i]}.html")
}
}
publishHTML(target: [
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'results',
reportFiles: htmlFiles.join(','),
reportName: 'HTML Report'])
junit 'results/*.xml'
archiveArtifacts 'results/*'
}
}
}