-
Notifications
You must be signed in to change notification settings - Fork 35
/
JenkinsfileTrigger
244 lines (220 loc) · 12.1 KB
/
JenkinsfileTrigger
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
import groovy.json.JsonOutput
properties(
[
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '50', daysToKeepStr: '', numToKeepStr: '100')),
parameters(
[
string(name: 'CI_MESSAGE',
description: 'fedmsg msg'),
string(name: 'TARGETS',
defaultValue: '^(f26|f27)$',
description: 'fedora branch targets'),
string(name: 'PROJECT_REPO',
defaultValue: 'ci-pipeline',
description: 'Main project repo'),
string(name: 'MAIN_TOPIC',
defaultValue: 'org.centos.prod',
description: 'main topic to publish on'),
]
),
pipelineTriggers(
[[$class: 'CIBuildTrigger',
noSquash: true,
providerData: [
$class: 'FedMsgSubscriberProviderData',
name: 'fedora-fedmsg',
overrides: [
topic: 'org.fedoraproject.prod.git.receive'
],
checks: []
]
]]
)
]
)
def fedmsg_properties = []
def job_properties = []
def messageProperties = ""
def messageContent = ""
def TRIGGER_RETRY_COUNT = 3
node('master') {
ansiColor('xterm') {
timestamps {
//def current_stage = "ci-pipeline-rpmbuild-trigger"
try {
deleteDir()
stage('ci-pipeline-rpmbuild-trigger') {
env.basearch = "x86_64"
// Set default main topic for messaging
if ((env.MAIN_TOPIC == null) || ("${env.MAIN_TOPIC}" == "")) {
env.MAIN_TOPIC = "org.centos.prod"
}
// SCM
dir('ci-pipeline') {
git 'https://github.com/CentOS-PaaS-SIG/ci-pipeline'
}
// Python script to parse the ${CI_MESSAGE}
writeFile file: "${env.WORKSPACE}/parse_fedmsg.py",
text: "#!/bin/env python\n" +
"import json\n" +
"import sys\n\n" +
"reload(sys)\n" +
"sys.setdefaultencoding('utf-8')\n" +
"message = json.load(sys.stdin)\n" +
"if 'commit' in message:\n" +
" msg = message['commit']\n\n" +
" for key in msg:\n" +
" print \"fed_%s=%s\" % (key, msg[key])\n"
// Parse the ${CI_MESSAGE}
sh '''
#!/bin/bash
set -xuo pipefail
chmod +x ${WORKSPACE}/parse_fedmsg.py
# Write fedmsg fields to a file to inject them
if [ -n "${CI_MESSAGE}" ]; then
echo ${CI_MESSAGE} | ${WORKSPACE}/parse_fedmsg.py > fedmsg_fields.txt
sed -i '/^\\\\s*$/d' ${WORKSPACE}/fedmsg_fields.txt
grep fed ${WORKSPACE}/fedmsg_fields.txt > ${WORKSPACE}/fedmsg_fields.txt.tmp
mv ${WORKSPACE}/fedmsg_fields.txt.tmp ${WORKSPACE}/fedmsg_fields.txt
fi
'''
// Load fedmsg fields as environment variables
def fedmsg_map = readProperties file: "${env.WORKSPACE}/fedmsg_fields.txt"
fedmsg_properties = fedmsg_map.collect { key, value -> return key+'='+value }
withEnv(fedmsg_properties) {
// Check if package is in the package list for fedora-atomic host
sh '''
set +e
branch=${fed_branch}
if [ "${branch}" = "master" ]; then
branch="rawhide"
fi
# Save the branch in job.properties
echo "branch=${branch}" >> ${WORKSPACE}/job.properties
# Verify this is a branch in our list of targets defined above in the parameters
if [[ ! "${fed_branch}" =~ ${TARGETS} ]]; then
echo "${fed_branch} is not in the list"
echo "topic=${MAIN_TOPIC}.ci.pipeline.package.ignore" >> ${WORKSPACE}/job.properties
else
# Verify this is a package we are interested in
valid=0
# Verify this commit is not to a user's fork
if [[ $fed_path == *"repositories/forks"* ]] ; then
echo "Not interested - Commit to user fork"
echo "topic=${MAIN_TOPIC}.ci.pipeline.package.ignore" >> ${WORKSPACE}/job.properties
break
fi
# Get the upstream package list
rm -rf fedora-atomic
git clone -b ${fed_branch} --single-branch http://pagure.io/fedora-atomic
pushd fedora-atomic
popd
python ${PROJECT_REPO}/utils/package_checker.py ${fed_repo} ${branch}
CHKR_RC=$?
# If $? -eq 0, we care about package
if [ $CHKR_RC -eq 0 ]; then
valid=1
# If $? -eq 2, upstream package list didn't exist so use legacy method to check
elif [ $CHKR_RC -eq 2 ]; then
for package in $(cat ${PROJECT_REPO}/config/package_list); do
if [ "${package}" = "${fed_repo}" ]; then
valid=1
break
fi
done
fi
if [ $valid -eq 0 ]; then
echo "Not a package we are interested in"
echo "topic=${MAIN_TOPIC}.ci.pipeline.package.ignore" >> ${WORKSPACE}/job.properties
else
echo "topic=${MAIN_TOPIC}.ci.pipeline.package.queued" >> ${WORKSPACE}/job.properties
touch ${WORKSPACE}/trigger.downstream
fi
fi
exit
'''
def jobproperties_map = readProperties file: "${env.WORKSPACE}/job.properties"
job_properties = jobproperties_map.collect { key, value -> return key+'='+value }
withEnv(job_properties) {
messageProperties = ''
// Populate message content
messageContent = [
topic: env.topic,
build_url: BUILD_URL,
build_id: BUILD_ID,
branch: env.branch,
ref: 'fedora/' + env.branch + '/x86_64/atomic-host',
rev: env.fed_rev,
repo: env.fed_repo,
namespace: env.fed_namespace,
username: 'fedora-atomic',
test_guidance: '',
status: currentBuild.currentResult
]
messageContentString = JsonOutput.toJson(messageContent)
// Send message org.centos.prod.ci.pipeline.package.queued or .ignore on fedmsg
sendMessage(messageProperties, messageContentString)
}
}
}
} catch (e) {
// Set build result
currentBuild.result = 'FAILURE'
// Send failure message for appropriate topic
sendMessage(messageProperties, messageContent)
throw e
} finally {
withEnv(fedmsg_properties) {
withEnv(job_properties) {
currentBuild.displayName = "Build#: ${env.BUILD_NUMBER} - Branch: ${env.branch} - Package: ${env.fed_repo}"
currentBuild.description = "${currentBuild.currentResult}"
step([$class: 'ArtifactArchiver', allowEmptyArchive: true, artifacts: 'trigger.downstream,*.txt,*.properties,*.props,*.groovy', excludes: '**/*.example', fingerprint: true])
if (fileExists("${env.WORKSPACE}/trigger.downstream")) {
echo "Trigger downstream pipeline ci-pipeline-2-0"
echo "CI_MESSAGE: ${env.CI_MESSAGE}"
echo "TARGET_BRANCH: ${env.fed_branch}"
echo "MAIN_TOPIC: ${env.MAIN_TOPIC}"
// Let's try 3 times to schedule build
try {
retry(TRIGGER_RETRY_COUNT) {
try {
build job: "continuous-infra-ci-pipeline-${env.branch}",
parameters: [string(name: 'CI_MESSAGE',
value: "${env.CI_MESSAGE}"),
string(name: 'TARGET_BRANCH',
value: "${env.fed_branch}"),
string(name: 'PROJECT_REPO',
value: 'ci-pipeline'),
string(name: 'MAIN_TOPIC',
value: 'org.centos.prod'),
string(name: 'MSG_PROVIDER',
value: 'fedora-fedmsg')],
wait: false
} catch (Exception e) {
e.printStackTrace()
throw e
}
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
currentBuild.description = "*TRIGGER FAILURE*"
error "Error: Build could not be added to queue after " + TRIGGER_RETRY_COUNT + " tries"
}
}
}
}
}
}
}
}
def convertProps(file1, file2) {
def command = $/awk -F'=' '{print "env."$1"=\""$2"\""}' ${file1} > ${file2}/$
sh command
}
def sendMessage(msgProps, msgContent) {
sendCIMessage messageContent: msgContent,
messageProperties: msgProps,
messageType: 'Custom',
overrides: [topic: "${env.topic}"],
providerName: 'fedora-fedmsg'
}