-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathopenstack-ardana-gerrit.Jenkinsfile
221 lines (194 loc) · 7.53 KB
/
openstack-ardana-gerrit.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
/**
* The openstack-ardana-gerrit Jenkins Pipeline
*/
def ardana_lib = null
pipeline {
// skip the default checkout, because we want to use a custom path
options {
skipDefaultCheckout()
timestamps()
}
agent {
node {
label 'cloud-ci-trigger'
customWorkspace "${JOB_NAME}-${BUILD_NUMBER}"
}
}
stages {
stage('Setup workspace') {
steps {
script {
sh('''
git clone $git_automation_repo --branch $git_automation_branch automation-git
''')
cloud_lib = load "$WORKSPACE/automation-git/jenkins/ci.suse.de/pipelines/openstack-cloud.groovy"
cloud_lib.load_extra_params_as_vars(extra_params)
if (GERRIT_CHANGE_NUMBER == '') {
error("Empty 'GERRIT_CHANGE_NUMBER' parameter value.")
}
if (env.GERRIT_PATCHSET_NUMBER == null) {
// Extract the current patchset number from the Gerrit change, if missing
env.GERRIT_PATCHSET_NUMBER = sh (
returnStdout: true,
script: '''
source automation-git/scripts/jenkins/cloud/jenkins-helper.sh
run_python_script automation-git/scripts/jenkins/cloud/gerrit/gerrit_get.py \
--attr patchset \
${GERRIT_CHANGE_NUMBER}
'''
).trim()
}
currentBuild.displayName = "#${BUILD_NUMBER}: $GERRIT_CHANGE_NUMBER/$GERRIT_PATCHSET_NUMBER ($gerrit_context)"
}
}
}
stage('supersede running jobs') {
steps {
script {
sh('''
source automation-git/scripts/jenkins/cloud/jenkins-helper.sh
# If this is a job triggered by Gerrit because a new patchset has
# been published, abort all other older running builds that
# target the same change number
if [[ $GERRIT_EVENT_TYPE == 'patchset-created' ]]; then
run_python_script -u automation-git/scripts/jenkins/jenkins-job-cancel \
--older-than ${BUILD_NUMBER} \
--with-param GERRIT_CHANGE_NUMBER=${GERRIT_CHANGE_NUMBER} \
--wait 600 \
${JOB_NAME} || :
else
if $voting; then
# If this is a voting job, abort other older running builds that
# target the same change number and are also voting.
run_python_script -u automation-git/scripts/jenkins/jenkins-job-cancel \
--older-than ${BUILD_NUMBER} \
--with-param GERRIT_CHANGE_NUMBER=${GERRIT_CHANGE_NUMBER} \
--with-param voting=True \
--wait 600 \
${JOB_NAME} || :
fi
# Also abort other older running builds that target the same change
# number and gerrit_context value, voting or otherwise.
run_python_script -u automation-git/scripts/jenkins/jenkins-job-cancel \
--older-than ${BUILD_NUMBER} \
--with-param GERRIT_CHANGE_NUMBER=${GERRIT_CHANGE_NUMBER} \
--with-param gerrit_context=${gerrit_context} \
--wait 600 \
${JOB_NAME} || :
fi
''')
}
}
}
stage('notify Gerrit') {
steps {
script {
sh('''
source automation-git/scripts/jenkins/cloud/jenkins-helper.sh
build_str="Build"
$voting || build_str="(Non-voting) build"
message="
${build_str} started (${gerrit_context}): ${BUILD_URL}
The following links can also be used to track the results:
- live console output: ${BUILD_URL}console
- live pipeline job view: ${RUN_DISPLAY_URL}
"
$voting && gerrit_voting_params="--vote 0 --label Verified"
run_python_script automation-git/scripts/jenkins/cloud/gerrit/gerrit_review.py \
--message "$message" \
$gerrit_voting_params \
--patch ${GERRIT_PATCHSET_NUMBER} \
${GERRIT_CHANGE_NUMBER}
''')
}
}
}
stage('validate commit message') {
steps {
sh '''
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
source automation-git/scripts/jenkins/cloud/jenkins-helper.sh
if [[ -n $GERRIT_CHANGE_COMMIT_MESSAGE ]]; then
commit_message=$(echo $GERRIT_CHANGE_COMMIT_MESSAGE | base64 --decode)
else
commit_message=$(run_python_script automation-git/scripts/jenkins/cloud/gerrit/gerrit_get.py \
--attr commit_message \
${GERRIT_CHANGE_NUMBER}/${GERRIT_PATCHSET_NUMBER})
fi
echo "$commit_message" | gitlint -C automation-git/scripts/jenkins/gitlint.ini
'''
}
}
stage('integration test') {
steps {
script {
// reserve a resource here for the integration job, to avoid
// keeping a cloud-ci worker busy while waiting for a
// resource to become available.
cloud_lib.run_with_reserved_env(reserve_env.toBoolean(), cloud_env, cloud_env) {
reserved_env ->
cloud_lib.trigger_build(integration_test_job, [
string(name: 'cloud_env', value: reserved_env),
string(name: 'reserve_env', value: "false"),
string(name: 'os_cloud', value: os_cloud),
string(name: 'gerrit_change_ids', value: "$GERRIT_CHANGE_NUMBER/$GERRIT_PATCHSET_NUMBER"),
string(name: 'cloudsource', value: "develcloud$version"),
string(name: 'extra_repos', value: "$extra_repos"),
string(name: 'git_automation_repo', value: "$git_automation_repo"),
string(name: 'git_automation_branch', value: "$git_automation_branch"),
string(name: 'os_project_name', value: "$os_project_name"),
text(name: 'extra_params', value: extra_params)
])
}
}
}
}
}
post {
always {
script{
env.BUILD_RESULT = currentBuild.currentResult
sh('''
source automation-git/scripts/jenkins/cloud/jenkins-helper.sh
run_python_script automation-git/scripts/jenkins/jenkins-job-pipeline-report.py \
--recursive \
--filter 'Declarative: Post Actions' \
--filter 'Setup workspace' \
--filter 'supersede running jobs' \
--filter 'notify Gerrit' > pipeline-report.txt || :
build_str="Build"
$voting || build_str="(Non-voting) build"
if [[ $BUILD_RESULT == SUCCESS ]]; then
vote=+2
message_result="succeeded"
elif [[ $BUILD_RESULT == ABORTED ]]; then
vote=
message_result="aborted"
else
vote=-2
message_result="failed"
fi
message="
${build_str} ${message_result} ($gerrit_context): ${BUILD_URL}
"
$voting && [[ -n $vote ]] && gerrit_voting_params="--vote $vote --label Verified"
run_python_script automation-git/scripts/jenkins/cloud/gerrit/gerrit_review.py \
--message "$message" \
--message-file pipeline-report.txt \
$gerrit_voting_params \
--patch $GERRIT_PATCHSET_NUMBER \
$GERRIT_CHANGE_NUMBER
if $voting && [[ $BUILD_RESULT == SUCCESS ]]; then
run_python_script automation-git/scripts/jenkins/cloud/gerrit/gerrit_merge.py \
--patch ${GERRIT_PATCHSET_NUMBER} \
${GERRIT_CHANGE_NUMBER}
fi
''')
}
}
cleanup {
cleanWs()
}
}
}