2 * Copyright (C) 2016-2018 - Michael Jeanson <mjeanson@efficios.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import hudson.AbortException
20 import hudson.console.HyperlinkNote
21 import java.util.concurrent.CancellationException
22 import org.eclipse.jgit.api.Git
23 import org.eclipse.jgit.lib.Ref
26 class InvalidKVersionException extends Exception {
27 public InvalidKVersionException(String message) {
32 class EmptyKVersionException extends Exception {
33 public EmptyKVersionException(String message) {
38 class RTKVersion implements Comparable<RTKVersion> {
52 static RTKVersion minKVersion() {
53 return new RTKVersion("v0.0.0-rt0-rebase")
56 static RTKVersion maxKVersion() {
57 return new RTKVersion("v" + Integer.MAX_VALUE + ".0.0-rt0-rebase")
60 static RTKVersion factory(version) {
61 return new RTKVersion(version)
72 throw new EmptyKVersionException("Empty kernel version")
75 def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rt(\d+)-rebase)$/
77 throw new InvalidKVersionException("Invalid kernel version: ${version}")
83 this.major = Integer.parseInt(match.group(1))
84 if (this.major <= 2) {
86 this.majorB = Integer.parseInt(match.group(2))
90 if (match.group(2 + offset) != null) {
91 this.minor = Integer.parseInt(match.group(2 + offset))
95 if (match.group(4 + offset) != null) {
96 this.patch = Integer.parseInt(match.group(4 + offset))
100 this.rt = Integer.parseInt(match.group(8))
103 // Return true if both version are of the same stable branch
104 Boolean isSameStable(RTKVersion o) {
105 if (this.major != o.major) {
108 if (this.majorB != o.majorB) {
111 if (this.minor != o.minor) {
118 @Override int compareTo(RTKVersion o) {
119 if (this.major != o.major) {
120 return Integer.compare(this.major, o.major)
122 if (this.majorB != o.majorB) {
123 return Integer.compare(this.majorB, o.majorB)
125 if (this.minor != o.minor) {
126 return Integer.compare(this.minor, o.minor)
128 if (this.patch != o.patch) {
129 return Integer.compare(this.patch, o.patch)
131 if (this.rt != o.rt) {
132 return Integer.compare(this.rt, o.rt)
140 String vString = "v${this.major}"
142 if (this.majorB > 0) {
143 vString = vString.concat(".${this.majorB}")
146 vString = vString.concat(".${this.minor}")
148 if (this.patch > 0) {
149 vString = vString.concat(".${this.patch}")
153 vString = vString.concat("-rt${this.rt}-rebase")
160 // Retrieve parameters of the current build
161 def mversion = build.buildVariableResolver.resolve('mversion')
162 def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild')
163 def kgitrepo = build.buildVariableResolver.resolve('kgitrepo')
164 def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor')
165 def kverceil_raw = build.buildVariableResolver.resolve('kverceil')
166 def kverfilter = build.buildVariableResolver.resolve('kverfilter')
167 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
168 def currentJobName = build.project.getFullDisplayName()
169 def gitmodpath = build.getEnvironment(listener).get('WORKSPACE') + "/src/lttng-modules"
171 // Get the out variable
172 def config = new HashMap()
173 def bindings = getBinding()
174 config.putAll(bindings.getVariables())
175 def out = config['out']
178 // Get the lttng-modules git url
179 def gitmodrepo = Git.open(new File(gitmodpath))
180 def mgitrepo = gitmodrepo.getRepository().getConfig().getString("remote", "origin", "url")
182 // Get tags from git repository
183 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call()
185 // Get kernel versions to build
188 ~/^refs\/tags\/(v[\d\.]+(-rt(\d+)-rebase))$/,
191 ~/v4\.11\.8-rt5-rebase/,
192 ~/v4\.11\.9-rt6-rebase/,
193 ~/v4\.11\.9-rt7-rebase/,
194 ~/v4\.11\.12-rt8-rebase/,
195 ~/v4\.11\.12-rt9-rebase/,
196 ~/v4\.11\.12-rt10-rebase/,
197 ~/v4\.11\.12-rt11-rebase/,
198 ~/v4\.11\.12-rt12-rebase/,
199 ~/v4\.11\.12-rt13-rebase/,
204 def kversionFactory = new RTKVersion()
206 // Parse kernel versions
209 kverfloor = kversionFactory.factory(kverfloor_raw)
210 } catch (EmptyKVersionException e) {
211 kverfloor = kversionFactory.minKVersion()
216 kverceil = kversionFactory.factory(kverceil_raw)
217 } catch (EmptyKVersionException e) {
218 kverceil = kversionFactory.maxKVersion()
221 // Build a sorted list of versions to build
223 for (tagMatchStr in tagMatchStrs) {
224 def tagMatch = ref.getName() =~ tagMatchStr
227 def kversion_raw = tagMatch.group(1)
228 def blacklisted = false
230 // Check if the kversion is blacklisted
231 for (blackMatchStr in blacklist) {
232 def blackMatch = kversion_raw =~ blackMatchStr
241 def v = kversionFactory.factory(kversion_raw)
243 if ((v >= kverfloor) && (v < kverceil)) {
253 //println "Pre filtering kernel versions:"
254 //for (k in kversions) {
258 switch (kverfilter) {
260 // Keep only the head of each stable branch
261 println('Filter kernel versions to keep only the latest point release of each stable branch.')
263 for (i = 0; i < kversions.size(); i++) {
264 def curr = kversions[i]
265 def next = i < kversions.size() - 1 ? kversions[i + 1] : null
268 if (curr.isSameStable(next)) {
277 // No filtering of kernel versions
278 println('No kernel versions filtering selected.')
283 println "Building the following kernel versions:"
284 for (k in kversions) {
288 // Debug: Stop build here
289 //throw new InterruptedException()
291 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
294 def ongoingBuild = []
297 def similarJobQueued = 0;
299 // Loop while we have kernel versions remaining or jobs running
300 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
302 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
303 def kversion = kversions.pop()
305 new StringParameterValue('mversion', mversion),
306 new StringParameterValue('mgitrepo', mgitrepo),
307 new StringParameterValue('ktag', kversion.toString()),
308 new StringParameterValue('kgitrepo', kgitrepo),
311 // Launch the parametrized build
312 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
313 println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}"
315 // Add it to the ongoing build queue
316 ongoingBuild.push(param_build)
320 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
324 if (e in InterruptedException) {
325 build.setResult(hudson.model.Result.ABORTED)
326 throw new InterruptedException()
332 // Abort job if a newer instance is queued
333 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
334 if ( similarJobQueued > 0 ) {
335 build.setResult(hudson.model.Result.ABORTED)
336 throw new InterruptedException()
339 def i = ongoingBuild.iterator()
340 while ( i.hasNext() ) {
341 currentBuild = i.next()
342 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
347 def matrixParent = currentBuild.get()
348 allBuilds.add(matrixParent)
349 def kernelStr = matrixParent.buildVariableResolver.resolve("ktag")
350 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
352 // Process child runs of matrixBuild
353 def childRuns = matrixParent.getRuns()
354 for ( childRun in childRuns ) {
355 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
356 if (childRun.result != Result.SUCCESS) {
357 failedRuns.add(childRun)
366 // Get log of failed runs
367 for (failedRun in failedRuns) {
368 println "---START---"
369 failedRun.writeWholeLogTo(out)
373 println "---Build report---"
374 for (b in allBuilds) {
375 def kernelStr = b.buildVariableResolver.resolve("ktag")
376 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
383 // Mark this build failed if any child build has failed
385 build.setResult(hudson.model.Result.FAILURE)