2 * Copyright (C) 2016-2017 - 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 kVersion implements Comparable<kVersion> {
44 Integer rc = Integer.MAX_VALUE;
57 this.rc = Integer.MAX_VALUE
60 throw new EmptykVersionException("Empty kernel version")
63 def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rc(\d+))?$/
65 throw new InvalidkVersionException("Invalid kernel version: ${version}")
71 this.major = Integer.parseInt(match.group(1))
72 if (this.major <= 2) {
74 this.majorB = Integer.parseInt(match.group(2))
78 if (match.group(2 + offset) != null) {
79 this.minor = Integer.parseInt(match.group(2 + offset))
83 if (match.group(4 + offset) != null) {
84 this.patch = Integer.parseInt(match.group(4 + offset))
88 if (match.group(8) != null) {
89 this.rc = Integer.parseInt(match.group(8))
93 // Return true if this version is a release candidate
95 return this.rc != Integer.MAX_VALUE
98 // Return true if both version are of the same stable branch
99 Boolean isSameStable(kVersion o) {
100 if (this.major != o.major) {
103 if (this.majorB != o.majorB) {
106 if (this.minor != o.minor) {
113 @Override int compareTo(kVersion o) {
114 if (this.major != o.major) {
115 return Integer.compare(this.major, o.major)
117 if (this.majorB != o.majorB) {
118 return Integer.compare(this.majorB, o.majorB)
120 if (this.minor != o.minor) {
121 return Integer.compare(this.minor, o.minor)
123 if (this.patch != o.patch) {
124 return Integer.compare(this.patch, o.patch)
126 if (this.rc != o.rc) {
127 return Integer.compare(this.rc, o.rc)
135 String vString = "v${this.major}"
137 if (this.majorB > 0) {
138 vString = vString.concat(".${this.majorB}")
141 vString = vString.concat(".${this.minor}")
143 if (this.patch > 0) {
144 vString = vString.concat(".${this.patch}")
147 if (this.rc > 0 && this.rc < Integer.MAX_VALUE) {
148 vString = vString.concat("-rc${this.rc}")
155 // Retrieve parameters of the current build
156 def mversion = build.buildVariableResolver.resolve('mversion')
157 def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild')
158 def kgitrepo = build.buildVariableResolver.resolve('kgitrepo')
159 def kverfloor_raw = build.buildVariableResolver.resolve('kverfloor')
160 def kverceil_raw = build.buildVariableResolver.resolve('kverceil')
161 def kverfilter = build.buildVariableResolver.resolve('kverfilter')
162 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
163 def currentJobName = build.project.getFullDisplayName()
165 // Parse kernel versions
166 def kverfloor = new kVersion(kverfloor_raw)
170 kverceil = new kVersion(kverceil_raw)
171 } catch (EmptykVersionException e) {
172 kverceil = new kVersion("v" + Integer.MAX_VALUE + ".0.0")
175 // Get the out variable
176 def config = new HashMap()
177 def bindings = getBinding()
178 config.putAll(bindings.getVariables())
179 def out = config['out']
182 // Get tags from git repository
183 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call();
185 // Get kernel versions to build
189 def match = ref.getName() =~ /^refs\/tags\/(v[\d\.]+(-rc(\d+))?)$/
192 def v = new kVersion(match.group(1))
194 if ((v >= kverfloor) && (v < kverceil)) {
207 switch (kverfilter) {
209 // Keep only the head of each stable branch
210 println('Filter kernel versions to keep only the latest point release of each stable branch.')
212 for (i = 0; i < kversions.size(); i++) {
213 def curr = kversions[i]
214 def next = i < kversions.size() - 1 ? kversions[i + 1] : null
217 if (curr.isSameStable(next)) {
226 // No filtering of kernel versions
227 println('No kernel versions filtering selected.')
231 // If the last RC version is newer than the last stable, add it to the build list
232 if (kversionsRC.last() > kversions.last()) {
233 kversions.add(kversionsRC.last())
237 println "Building the following kernel versions:"
238 for (k in kversions) {
242 // Debug: Stop build here
243 //throw new InterruptedException()
245 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
248 def ongoingBuild = []
251 def similarJobQueued = 0;
253 // Loop while we have kernel versions remaining or jobs running
254 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
256 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
257 def kversion = kversions.pop()
259 new StringParameterValue('mversion', mversion),
260 new StringParameterValue('kversion', kversion.toString()),
261 new StringParameterValue('kgitrepo', kgitrepo),
264 // Launch the parametrized build
265 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
266 println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}"
268 // Add it to the ongoing build queue
269 ongoingBuild.push(param_build)
273 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
277 if (e in InterruptedException) {
278 build.setResult(hudson.model.Result.ABORTED)
279 throw new InterruptedException()
285 // If a newer instance of this job is queued, abort to let it run
286 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
287 if ( similarJobQueued > 0 ) {
288 // Abort since new build is queued
289 build.setResult(hudson.model.Result.ABORTED)
290 throw new InterruptedException()
293 def i = ongoingBuild.iterator()
294 while ( i.hasNext() ) {
295 currentBuild = i.next()
296 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
301 def matrixParent = currentBuild.get()
302 allBuilds.add(matrixParent)
303 def kernelStr = matrixParent.buildVariableResolver.resolve("kversion")
304 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
306 // Process child runs of matrixBuild
307 def childRuns = matrixParent.getRuns()
308 for ( childRun in childRuns ) {
309 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
310 if (childRun.result != Result.SUCCESS) {
311 failedRuns.add(childRun)
320 // Get log of failed runs
321 for (failedRun in failedRuns) {
322 println "---START---"
323 failedRun.writeWholeLogTo(out)
327 println "---Build report---"
328 for (b in allBuilds) {
329 def kernelStr = b.buildVariableResolver.resolve("kversion")
330 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
337 // Mark this build failed if any child build has failed
339 build.getExecutor().interrupt(Result.FAILURE)