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
|
// Pipeline
import groovy.json.JsonSlurper
import hudson.Util
import java.util.concurrent.TimeUnit
// Constants
String PENDING_MSG = "Jenkins started the build."
String SUCCESS_MSG = "The Jenkins build completed successfully."
String FAILURE_MSG = "The Jenkins build failed."
// Parsing data
def data = new JsonSlurper().parseText(env.payload)
// Detecting Git URL
String USERNAME = data.repository.owner.login
String REPOSITORY = data.repository.name
String GITHUB_URL = data.repository.ssh_url
// Determining current pipeline
int exit_status = 0
String BUILD_TYPE = ""
String COMMIT_HASH = ""
String SHORT_HASH = ""
String PR_BRANCH = ""
String COMMIT_BRANCH = ""
String COMMIT_URL = ""
String BASE_HASH = "0000000"
String SENDER = ""
int PR_NUMBER = 0
String CHECKOUT_BRANCH = ""
String CHECKOUT_REF = ""
String ROOT_CONTEXT = "jenkins/diplomacy.ai"
String CONTEXT = ""
String MESSAGE = ""
// ---- Push ----
if (data.ref != null && data.after.substring(0, 7) != "0000000") {
BUILD_TYPE = "push"
COMMIT_HASH = data.after
SHORT_HASH = COMMIT_HASH.substring(0, 7)
COMMIT_BRANCH = data.ref.replaceAll("refs/heads/", "")
COMMIT_URL = data.compare
SENDER = data.pusher.name
CHECKOUT_BRANCH = COMMIT_HASH
CHECKOUT_REF = ""
CONTEXT = ROOT_CONTEXT + "/push"
MESSAGE = data.head_commit.message
currentBuild.description = "${SHORT_HASH} - ${COMMIT_BRANCH} - ${MESSAGE}"
// ---- Pull Request ----
} else if (data.pull_request != null \
&& data.number > 0 \
&& (data.action == "opened" || data.action == "reopened" || data.action == "synchronize")) {
BUILD_TYPE = "pr"
COMMIT_HASH = data.pull_request.head.sha
SHORT_HASH = COMMIT_HASH.substring(0, 7)
PR_BRANCH = data.pull_request.head.label
COMMIT_BRANCH = data.pull_request.base.ref
COMMIT_URL = data.pull_request.html_url
BASE_HASH = data.pull_request.base.sha
SENDER = data.pull_request.user.login
PR_NUMBER = data.pull_request.number
CHECKOUT_BRANCH = COMMIT_HASH
CHECKOUT_REF = "+refs/pull/*:refs/remotes/origin/pr/* +refs/heads/*:refs/remotes/origin/*"
CONTEXT = ROOT_CONTEXT + "/pr"
MESSAGE = data.pull_request.title
currentBuild.description = "${SHORT_HASH} - PR #${PR_NUMBER} - ${MESSAGE}"
// ---- No need to build ----
} else {
currentBuild.result = "SUCCESS"
currentBuild.description = "---"
return
}
// ====== STAGES =======
data = null
String STRIPPED_BRANCH = COMMIT_BRANCH.replaceAll("/", "_")
// -------------------------------
// 1. Setting Status
try {
githubNotify account: USERNAME, context: ROOT_CONTEXT, credentialsId: 'ppaquette-jenkins-api-as-password', description: PENDING_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'PENDING', targetUrl: ''
} catch(err) {
echo "Error setting status on GitHub commit: ${err}"
exit_status = 1
currentBuild.result = "FAILURE"
}
// -------------------------------
// 2. Running tests
parallel_test = [:]
// Running Python 3.6 everytime, except for "web", "build", "build_rw" branch
parallel_test['python_3.6'] = {
node('ubuntu-1604-small') {
stage('Python 3.6') {
try {
timeout(time: 20, activity: true, unit: 'MINUTES') {
githubNotify account: USERNAME, context: CONTEXT + "/python3.6", credentialsId: 'ppaquette-jenkins-api-as-password', description: PENDING_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'PENDING', targetUrl: ''
withCredentials([sshUserPrivateKey(credentialsId: 'jenkins-ssh-key', keyFileVariable: 'SSH_KEY_FILE', passphraseVariable: '', usernameVariable: '')]) {
checkout([$class: 'GitSCM', branches: [[name: CHECKOUT_BRANCH]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[name: 'origin', refspec: CHECKOUT_REF, credentialsId: 'jenkins-ssh-key', url: GITHUB_URL]]])
sh ".jenkins/merge_pr.sh $PR_NUMBER $COMMIT_HASH $BASE_HASH $SSH_KEY_FILE \
&& wget -nv https://storage.googleapis.com/ppaquette-diplomacy/files/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh \
&& chmod +x ./miniconda.sh \
&& rm -Rf $HOME/miniconda \
&& ./miniconda.sh -b -p $HOME/miniconda \
&& export PATH=$HOME/miniconda/bin:$PATH \
&& conda create -y -q -n py36 python=3.6 \
&& export PATH=$HOME/miniconda/envs/py36/bin:$PATH \
&& mkdir -p $HOME/.cache/diplomacy \
&& wget -nv https://storage.googleapis.com/ppaquette-diplomacy/cache-jenkins/convoy_paths_cache.pkl -O $HOME/.cache/diplomacy/convoy_paths_cache.pkl \
&& .jenkins/get_cache.sh $PR_NUMBER $STRIPPED_BRANCH \
&& pip install -r requirements.txt \
&& pip install -r requirements_dev.txt \
&& touch run_install_nvm.sh \
&& chmod +x run_install_nvm.sh \
&& ./run_install_nvm.sh \
&& echo '--------------------------------------------------' \
&& pip --version \
&& python --version \
&& which python \
&& git rev-parse HEAD \
&& git log -n 1 \
&& ./run_tests.sh \
&& .jenkins/set_cache.sh $PR_NUMBER $STRIPPED_BRANCH"
}
githubNotify account: USERNAME, context: CONTEXT + "/python3.6", credentialsId: 'ppaquette-jenkins-api-as-password', description: SUCCESS_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'SUCCESS', targetUrl: ''
}
} catch(err) {
echo "Error encountered in parallel (Python 3.6): ${err}"
exit_status = 1
currentBuild.result = "FAILURE"
githubNotify account: USERNAME, context: CONTEXT + "/python3.6", credentialsId: 'ppaquette-jenkins-api-as-password', description: FAILURE_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'FAILURE', targetUrl: ''
}
}
}
}
// Only running 3.4 and 3.5 for Pull Requests (and for "jenkins" branch)
if (BUILD_TYPE == 'pr' || (BUILD_TYPE == 'push' && COMMIT_BRANCH == 'jenkins')) {
parallel_test['python_3.5'] = {
node('ubuntu-1604-small') {
stage('Python 3.5') {
try {
timeout(time: 20, activity: true, unit: 'MINUTES') {
githubNotify account: USERNAME, context: CONTEXT + "/python3.5", credentialsId: 'ppaquette-jenkins-api-as-password', description: PENDING_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'PENDING', targetUrl: ''
withCredentials([sshUserPrivateKey(credentialsId: 'jenkins-ssh-key', keyFileVariable: 'SSH_KEY_FILE', passphraseVariable: '', usernameVariable: '')]) {
checkout([$class: 'GitSCM', branches: [[name: CHECKOUT_BRANCH]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[name: 'origin', refspec: CHECKOUT_REF, credentialsId: 'jenkins-ssh-key', url: GITHUB_URL]]])
sh ".jenkins/merge_pr.sh $PR_NUMBER $COMMIT_HASH $BASE_HASH $SSH_KEY_FILE \
&& wget -nv https://storage.googleapis.com/ppaquette-diplomacy/files/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh \
&& chmod +x ./miniconda.sh \
&& rm -Rf $HOME/miniconda \
&& ./miniconda.sh -b -p $HOME/miniconda \
&& export PATH=$HOME/miniconda/bin:$PATH \
&& conda create -y -q -n py35 python=3.5 \
&& export PATH=$HOME/miniconda/envs/py35/bin:$PATH \
&& mkdir -p $HOME/.cache/diplomacy \
&& wget -nv https://storage.googleapis.com/ppaquette-diplomacy/cache-jenkins/convoy_paths_cache.pkl -O $HOME/.cache/diplomacy/convoy_paths_cache.pkl \
&& .jenkins/get_cache.sh $PR_NUMBER $STRIPPED_BRANCH \
&& pip install -r requirements.txt \
&& pip install -r requirements_dev.txt \
&& touch run_install_nvm.sh \
&& chmod +x run_install_nvm.sh \
&& ./run_install_nvm.sh \
&& echo '--------------------------------------------------' \
&& pip --version \
&& python --version \
&& which python \
&& git rev-parse HEAD \
&& git log -n 1 \
&& ./run_tests.sh \
&& .jenkins/set_cache.sh $PR_NUMBER $STRIPPED_BRANCH"
}
githubNotify account: USERNAME, context: CONTEXT + "/python3.5", credentialsId: 'ppaquette-jenkins-api-as-password', description: SUCCESS_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'SUCCESS', targetUrl: ''
}
} catch(err) {
echo "Error encountered in parallel (Python 3.5): ${err}"
exit_status = 1
currentBuild.result = "FAILURE"
githubNotify account: USERNAME, context: CONTEXT + "/python3.5", credentialsId: 'ppaquette-jenkins-api-as-password', description: FAILURE_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'FAILURE', targetUrl: ''
}
}
}
}
parallel_test['python_3.4'] = {
node('ubuntu-1604-small') {
stage('Python 3.4') {
try {
timeout(time: 20, activity: true, unit: 'MINUTES') {
githubNotify account: USERNAME, context: CONTEXT + "/python3.4", credentialsId: 'ppaquette-jenkins-api-as-password', description: PENDING_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'PENDING', targetUrl: ''
withCredentials([sshUserPrivateKey(credentialsId: 'jenkins-ssh-key', keyFileVariable: 'SSH_KEY_FILE', passphraseVariable: '', usernameVariable: '')]) {
checkout([$class: 'GitSCM', branches: [[name: CHECKOUT_BRANCH]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']], submoduleCfg: [], userRemoteConfigs: [[name: 'origin', refspec: CHECKOUT_REF, credentialsId: 'jenkins-ssh-key', url: GITHUB_URL]]])
sh ".jenkins/merge_pr.sh $PR_NUMBER $COMMIT_HASH $BASE_HASH $SSH_KEY_FILE \
&& wget -nv https://storage.googleapis.com/ppaquette-diplomacy/files/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh \
&& chmod +x ./miniconda.sh \
&& rm -Rf $HOME/miniconda \
&& ./miniconda.sh -b -p $HOME/miniconda \
&& export PATH=$HOME/miniconda/bin:$PATH \
&& conda create -y -q -n py34 python=3.4 \
&& export PATH=$HOME/miniconda/envs/py34/bin:$PATH \
&& mkdir -p $HOME/.cache/diplomacy \
&& wget -nv https://storage.googleapis.com/ppaquette-diplomacy/cache-jenkins/convoy_paths_cache.pkl -O $HOME/.cache/diplomacy/convoy_paths_cache.pkl \
&& .jenkins/get_cache.sh $PR_NUMBER $STRIPPED_BRANCH \
&& pip install -r requirements.txt \
&& pip install -r requirements_dev.txt \
&& touch run_install_nvm.sh \
&& chmod +x run_install_nvm.sh \
&& ./run_install_nvm.sh \
&& echo '--------------------------------------------------' \
&& pip --version \
&& python --version \
&& which python \
&& git rev-parse HEAD \
&& git log -n 1 \
&& ./run_tests.sh \
&& .jenkins/set_cache.sh $PR_NUMBER $STRIPPED_BRANCH"
}
githubNotify account: USERNAME, context: CONTEXT + "/python3.4", credentialsId: 'ppaquette-jenkins-api-as-password', description: SUCCESS_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'SUCCESS', targetUrl: ''
}
} catch(err) {
echo "Error encountered in parallel (Python 3.4): ${err}"
exit_status = 1
currentBuild.result = "FAILURE"
githubNotify account: USERNAME, context: CONTEXT + "/python3.4", credentialsId: 'ppaquette-jenkins-api-as-password', description: FAILURE_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'FAILURE', targetUrl: ''
}
}
}
}
}
parallel parallel_test
// -------------------------------
// 3. Updating git status
if (exit_status == 1) {
currentBuild.result = "FAILURE"
githubNotify account: USERNAME, context: ROOT_CONTEXT, credentialsId: 'ppaquette-jenkins-api-as-password', description: FAILURE_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'FAILURE', targetUrl: ''
} else {
currentBuild.result = "SUCCESS"
githubNotify account: USERNAME, context: ROOT_CONTEXT, credentialsId: 'ppaquette-jenkins-api-as-password', description: SUCCESS_MSG, gitApiUrl: '', repo: REPOSITORY, sha: COMMIT_HASH, status: 'SUCCESS', targetUrl: ''
}
|