-
Notifications
You must be signed in to change notification settings - Fork 59
/
template_3.17.jenkinsfile
234 lines (216 loc) · 10.7 KB
/
template_3.17.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
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env groovy
import groovy.transform.Field
// PARAMETERS for this pipeline:
// SOURCE_REPO
// SOURCE_BRANCH
// MIDSTM_REPO
// MIDSTM_BRANCH
// MIDSTM_NAME
// UPDATE_BASE_IMAGES_FLAGS = "" or "--tag \"1\\\\.13|8\\\\.[0-9]-\" " (if project is locked on golang 1.13 instead of latest)
// FORCE_BUILD = "false"
// CLEAN_ON_FAILURE = "true"
def SYNC_REPO = MIDSTM_NAME
def OLD_SOURCE_SHA=""
def OLD_MID_SHA=""
def NEW_MID_SHA=""
def String nodeLabel = 'x86_64-rhel8||ppc64le-rhel8'
timeout(env.TIMEOUT?.trim() ? env.TIMEOUT.toInteger() : 240) {
node(nodeLabel) {
stage ("Sync repos on ${nodeLabel}") {
wrap([$class: 'TimestamperBuildWrapper']) {
sh('curl -sSLO https://raw.githubusercontent.com/redhat-developer/devspaces/' + MIDSTM_BRANCH + '/product/util2.groovy')
def util = load "${WORKSPACE}/util2.groovy"
// store the URL for comments on the associated PR for this latest build (if applicable)
def comments_url_this = util.globalVar({comments_url})?.trim() ? util.globalVar({comments_url})?.trim() : ""
cleanWs()
DS_VERSION = util.getDsVersion(MIDSTM_BRANCH)
println "DS_VERSION = '" + DS_VERSION + "'"
withCredentials([string(credentialsId:'crw_devstudio-release-token', variable: 'GITHUB_TOKEN')]) {
println "########################################################################################################"
println "## Clone and update github.com/${SOURCE_REPO}.git"
println "########################################################################################################"
SOURCE_DIR="${WORKSPACE}/sources"
util.cloneRepo("https://github.com/${SOURCE_REPO}.git", SOURCE_DIR, SOURCE_BRANCH, true)
// fetch correct build tag if defined (eg., traefik)
if (util.globalVar({SOURCE_TAG})?.trim()) {
sh('''#!/bin/bash
echo "Switch to SOURCE_TAG = ''' + util.globalVar({SOURCE_TAG}) + '''"
git config advice.detachedHead false
cd ''' + SOURCE_DIR + ''' && git checkout ''' + util.globalVar({SOURCE_TAG})
)
} else {
// do not comment if we're building from a tag, only a branch! (see https://github.com/redhat-developer/devspaces-images/issues/357 )
// comment on the PR with links to this job
if (comments_url_this?.trim()) {
println("Used PR comment URL: " + comments_url_this)
comments_url_this=util.commentOnPullRequestBuildLinks(comments_url_this)
} else {
comments_url_this=util.commentOnPullRequestBuildLinks(SOURCE_REPO,util.getLastCommitSHA(SOURCE_DIR,40))
println("Computed PR comment URL: " + comments_url_this)
}
comments_url_this=comments_url_this.replaceAll("#.+","")
}
OLD_SOURCE_SHA = util.getLastCommitSHA(SOURCE_DIR)
println "Got OLD_SOURCE_SHA in sources folder: " + OLD_SOURCE_SHA
if (!util.globalVar({SOURCE_TAG})?.trim()) {
if(SOURCE_REPO.contains("devspaces")) {
util.updateBaseImages(SOURCE_DIR, SOURCE_BRANCH, "-f Dockerfile " + util.globalVar({UPDATE_BASE_IMAGES_FLAGS})) // push updated base image change to upstream
} else {
util.updateBaseImages(SOURCE_DIR, SOURCE_BRANCH, "-f Dockerfile --nocommit " + util.globalVar({UPDATE_BASE_IMAGES_FLAGS}))
}
}
println "########################################################################################################"
println "## Sync ${SYNC_REPO} to devspaces-images"
println "########################################################################################################"
util.cloneRepo("https://github.com/${MIDSTM_REPO}", "targetdwn", MIDSTM_BRANCH, false)
TARGET_DIR="${WORKSPACE}/targetdwn/" + util.getDSLongName(SYNC_REPO)
OLD_MID_SHA = util.getLastCommitSHA("${WORKSPACE}/targetdwn")
println "Got OLD_SHA in targetdwn/${SYNC_REPO} folder: " + OLD_MID_SHA
// might be already defined by the referring job
CSV_VERSION = util.globalVar({CSV_VERSION})?.trim() ? util.globalVar({CSV_VERSION}) : util.getCSVVersion(MIDSTM_BRANCH)
CSV_VERSION_BASE=CSV_VERSION.replaceAll("([0-9]+\\.[0-9]+)\\.[0-9]+","\$1"); // extract 2.yy from 2.yy.z
// print "CSV_VERSION_BASE=" + CSV_VERSION_BASE
// CRW-2039 this can happen if the 2.x jobs have not all run and the operator sources still have the previous release's version
if (!CSV_VERSION_BASE.equals(DS_VERSION)) {
println "[WARNING] CSV version (from getCSVVersion() -> csv.yaml = " + CSV_VERSION +
") does not match DS version (from getDsVersion() -> VERSION = " + DS_VERSION + ") !"
CSV_VERSION=DS_VERSION+".0" // for .x builds, use DS_VERSION.0
}
println("[INFO] Using CSV version = " + CSV_VERSION)
// rsync files to devspaces-images, and transform them
sh('''#!/bin/bash -xe
cd ''' + TARGET_DIR + ''' && ./build/scripts/sync.sh -v ''' + CSV_VERSION + ''' -s ''' + SOURCE_DIR + ''' -t ''' + TARGET_DIR
)
// push changes to devspaces-images
util.updateBaseImages(TARGET_DIR, MIDSTM_BRANCH, "--nocommit -maxdepth 3 -f \"*ockerfile\"" + util.globalVar({UPDATE_BASE_IMAGES_FLAGS}))
sh('''#!/bin/bash -xe
cd ${WORKSPACE}/targetdwn
git add ''' + util.getDSLongName(SYNC_REPO) + '''
git update-index --refresh || true # ignore timestamp updates
if [[ \$(git diff-index HEAD --) ]]; then # file changed
cd ''' + TARGET_DIR + '''
# check for various dockerfile options
dfiles=""
for d in Dockerfile build/dockerfiles/Dockerfile build/dockerfiles/rhel.Dockerfile build/dockerfiles/brew.Dockerfile; do
if [[ -f $d ]]; then
dfiles="${dfiles} $d"
git add $d . -A -f
fi
done
# note this might fail if we're syncing from a tag vs. a branch
git commit -s -m "ci: [sync] Update from ''' + SOURCE_REPO + ''' @ ''' + OLD_SOURCE_SHA + '''" $dfiles . || true
git pull origin ''' + MIDSTM_BRANCH + ''' || true
git push origin ''' + MIDSTM_BRANCH + ''' || true
echo "[sync] Updated @ \$(git rev-parse --short=4 HEAD) from ''' + SOURCE_REPO + ''' @ ''' + OLD_SOURCE_SHA + '''"
fi
''')
NEW_MID_SHA = util.getLastCommitSHA("${WORKSPACE}/targetdwn")
println "Got NEW_MID_SHA in targetdwn folder: " + NEW_MID_SHA
if (NEW_MID_SHA.equals(OLD_MID_SHA) && !FORCE_BUILD.equals("true")) {
currentBuild.result='UNSTABLE'
}
currentBuild.description="Upstream sync done"
println "########################################################################################################"
println "## Trigger sync to downstream job"
println "########################################################################################################"
if (!OLD_MID_SHA.equals(NEW_MID_SHA) || FORCE_BUILD == "true") {
JOB_BRANCH = util.getJobBranch(MIDSTM_BRANCH)
jobPath='/job/DS_CI/job/sync-to-downstream_' + JOB_BRANCH
final jobResult = build(
job: jobPath.replaceAll("/job/","/"),
wait: true,
propagate: true,
quietPeriod: 0,
parameters: [
[
$class: 'StringParameterValue',
name: 'comments_url',
value: comments_url_this
],
[
$class: 'StringParameterValue',
name: 'token',
value: "CI_BUILD"
],
[
$class: 'StringParameterValue',
name: 'cause',
value: SYNC_REPO + "+respin+by+${BUILD_TAG}"
],
[
$class: 'StringParameterValue',
name: 'REPOS',
value: util.getDSLongName(SYNC_REPO)
],
[
$class: 'StringParameterValue',
name: 'JOB_BRANCH',
value: JOB_BRANCH
],
[
$class: 'StringParameterValue',
name: 'CSV_VERSION',
value: (CSV_VERSION ? CSV_VERSION : util.globalVar({CSV_VERSION}))
],
[
$class: 'StringParameterValue',
name: 'UPDATE_BASE_IMAGES_FLAGS',
value: util.globalVar({UPDATE_BASE_IMAGES_FLAGS})
],
[
$class: 'StringParameterValue',
name: 'MIDSTM_BRANCH',
value: MIDSTM_BRANCH
],
[
$class: 'BooleanParameterValue',
name: 'FORCE_BUILD',
value: true
],
[
$class: 'BooleanParameterValue',
name: 'SCRATCH',
value: false
],
[
$class: 'StringParameterValue',
name: 'TIMEOUT',
value: env.TIMEOUT?.trim() ? env.TIMEOUT + "" : ""
],
[
$class: 'BooleanParameterValue',
name: 'CLEAN_ON_FAILURE',
value: CLEAN_ON_FAILURE
]
]
)
jobLink=jobPath + "/" + jobResult?.number?.toString()
println("waiting for build(" + jobPath + ")")
println("++> Job ${JENKINS_URL}${jobLink}/console completed.")
currentBuild.description+="; <a href=${jobLink}/>" + (jobLink.replaceAll("/job/","/")) + "</a> triggered"
} else {
println "No changes upstream, nothing to sync / build in brew!"
currentBuild.description+="; No sync or Brew build"
currentBuild.result = 'UNSTABLE'
}
// comment on the PR with this job's build description
println("Used PR comment URL: " + util.commentOnPullRequestBuildDescription(comments_url_this))
// only send emails for failures, not for currentBuild.result == 'UNSTABLE' (no sync or brew build downstream of this job)
if (currentBuild.result == 'FAILURE') {
util.notifyBuildFailed(currentBuild.description)
}
} // with
} // wrap
cleanWs(
cleanWhenSuccess: true,
cleanWhenUnstable: true,
cleanWhenNotBuilt: false,
cleanWhenFailure: CLEAN_ON_FAILURE,
cleanWhenAborted: true,
deleteDirs: true,
disableDeferredWipeout: true,
notFailBuild: true
)
} // stage
} // node
} // timeout