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 kVersion implements Comparable<kVersion> {
32 Integer rc = Integer.MAX_VALUE;
45 this.rc = Integer.MAX_VALUE
47 def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rc(\d+))?$/
49 throw new Exception("Invalid kernel version: ${version}")
55 this.major = Integer.parseInt(match.group(1))
56 if (this.major <= 2) {
58 this.majorB = Integer.parseInt(match.group(2))
62 if (match.group(2 + offset) != null) {
63 this.minor = Integer.parseInt(match.group(2 + offset))
67 if (match.group(4 + offset) != null) {
68 this.patch = Integer.parseInt(match.group(4 + offset))
72 if (match.group(8) != null) {
73 this.rc = Integer.parseInt(match.group(8))
77 // Return true if this version is a release candidate
79 return this.rc != Integer.MAX_VALUE
82 // Return true if both version are of the same stable branch
83 Boolean isSameStable(kVersion o) {
84 if (this.major != o.major) {
87 if (this.majorB != o.majorB) {
90 if (this.minor != o.minor) {
97 @Override int compareTo(kVersion o) {
98 if (this.major != o.major) {
99 return Integer.compare(this.major, o.major)
101 if (this.majorB != o.majorB) {
102 return Integer.compare(this.majorB, o.majorB)
104 if (this.minor != o.minor) {
105 return Integer.compare(this.minor, o.minor)
107 if (this.patch != o.patch) {
108 return Integer.compare(this.patch, o.patch)
110 if (this.rc != o.rc) {
111 return Integer.compare(this.rc, o.rc)
119 String vString = "v${this.major}"
121 if (this.majorB > 0) {
122 vString = vString.concat(".${this.majorB}")
125 vString = vString.concat(".${this.minor}")
127 if (this.patch > 0) {
128 vString = vString.concat(".${this.patch}")
131 if (this.rc > 0 && this.rc < Integer.MAX_VALUE) {
132 vString = vString.concat("-rc${this.rc}")
139 // Retrieve parameters of the current build
140 def mversion = build.buildVariableResolver.resolve('mversion')
141 def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild')
142 def kgitrepo = build.buildVariableResolver.resolve('kgitrepo')
143 def kverfloor = new kVersion(build.buildVariableResolver.resolve('kverfloor'))
144 def kverfilter = build.buildVariableResolver.resolve('kverfilter')
145 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
146 def currentJobName = build.project.getFullDisplayName()
148 // Get the out variable
149 def config = new HashMap()
150 def bindings = getBinding()
151 config.putAll(bindings.getVariables())
152 def out = config['out']
154 def jlc = new jenkins.model.JenkinsLocationConfiguration()
155 def jenkinsUrl = jlc.url
157 // Get tags from git repository
158 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call();
160 // Get kernel versions to build
164 def match = ref.getName() =~ /^refs\/tags\/(v[\d\.]+(-rc(\d+))?)$/
167 def v = new kVersion(match.group(1))
169 if (v >= kverfloor) {
182 switch (kverfilter) {
184 // Keep only the head of each stable branch
185 println('Filter kernel versions to keep only the latest point release of each stable branch.')
187 for (i = 0; i < kversions.size(); i++) {
188 def curr = kversions[i]
189 def next = i < kversions.size() - 1 ? kversions[i + 1] : null
192 if (curr.isSameStable(next)) {
201 // No filtering of kernel versions
202 println('No kernel versions filtering selected.')
206 // If the last RC version is newer than the last stable, add it to the build list
207 if (kversionsRC.last() > kversions.last()) {
208 kversions.add(kversionsRC.last())
212 println "Building the following kernel versions:"
213 for (k in kversions) {
217 // Debug: Stop build here
218 //throw new InterruptedException()
220 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
223 def ongoingBuild = []
226 def similarJobQueued = 0;
228 // Loop while we have kernel versions remaining or jobs running
229 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
231 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
232 def kversion = kversions.pop()
234 new StringParameterValue('mversion', mversion),
235 new StringParameterValue('kversion', kversion.toString()),
236 new StringParameterValue('kgitrepo', kgitrepo),
239 // Launch the parametrized build
240 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
241 println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}"
243 // Add it to the ongoing build queue
244 ongoingBuild.push(param_build)
248 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
252 if (e in InterruptedException) {
253 build.setResult(hudson.model.Result.ABORTED)
254 throw new InterruptedException()
260 // If a newer instance of this job is queued, abort to let it run
261 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
262 if ( similarJobQueued > 0 ) {
263 // Abort since new build is queued
264 build.setResult(hudson.model.Result.ABORTED)
265 throw new InterruptedException()
268 def i = ongoingBuild.iterator()
269 while ( i.hasNext() ) {
270 currentBuild = i.next()
271 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
276 def matrixParent = currentBuild.get()
277 allBuilds.add(matrixParent)
278 def kernelStr = matrixParent.buildVariableResolver.resolve("kversion")
279 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
281 // Process child runs of matrixBuild
282 def childRuns = matrixParent.getRuns()
283 for ( childRun in childRuns ) {
284 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
285 if (childRun.result != Result.SUCCESS) {
286 failedRuns.add(childRun)
295 // Get log of failed runs
296 for (failedRun in failedRuns) {
297 println "---START---"
298 failedRun.writeWholeLogTo(out)
302 println "---Build report---"
303 for (b in allBuilds) {
304 def kernelStr = b.buildVariableResolver.resolve("kversion")
305 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
312 // Mark this build failed if any child build has failed
314 build.getExecutor().interrupt(Result.FAILURE)