2 * Copyright (C) 2016 - 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> {
47 def match = version =~ /^v(\d+)\.(\d+)(\.(\d+))?(\.(\d+))?(-rt(\d+)-rebase)$/
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 this.rt = Integer.parseInt(match.group(8))
75 @Override int compareTo(kVersion o) {
76 if (this.major != o.major) {
77 return Integer.compare(this.major, o.major);
79 if (this.majorB != o.majorB) {
80 return Integer.compare(this.majorB, o.majorB);
82 if (this.minor != o.minor) {
83 return Integer.compare(this.minor, o.minor);
85 if (this.patch != o.patch) {
86 return Integer.compare(this.patch, o.patch);
88 if (this.rt != o.rc) {
89 return Integer.compare(this.rt, o.rt);
97 String vString = "v${this.major}"
99 if (this.majorB > 0) {
100 vString = vString.concat(".${this.majorB}")
103 vString = vString.concat(".${this.minor}")
105 if (this.patch > 0) {
106 vString = vString.concat(".${this.patch}")
110 vString = vString.concat("-rt${this.rt}-rebase")
117 // Retrieve parameters of the current build
118 def mversion = build.buildVariableResolver.resolve('mversion')
119 def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild')
120 def kgitrepo = build.buildVariableResolver.resolve('kgitrepo')
121 def kverfloor = new kVersion(build.buildVariableResolver.resolve('kverfloor'))
122 def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
123 def currentJobName = build.project.getFullDisplayName()
125 // Get the out variable
126 def config = new HashMap()
127 def bindings = getBinding()
128 config.putAll(bindings.getVariables())
129 def out = config['out']
131 def jlc = new jenkins.model.JenkinsLocationConfiguration()
132 def jenkinsUrl = jlc.url
134 // Get tags from git repository
135 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call();
137 // Get kernel versions to build
140 def match = ref.getName() =~ /^refs\/tags\/(v[\d\.]+(-rt(\d+)-rebase))$/
143 def v = new kVersion(match.group(1))
145 if (v >= kverfloor) {
154 println "Building the following kernel versions:"
155 for (k in kversions) {
159 // Debug: Stop build here
160 //throw new InterruptedException()
162 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
165 def ongoingBuild = []
169 // Loop while we have kernel versions remaining or jobs running
170 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
172 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
173 def kversion = kversions.pop()
175 new StringParameterValue('mversion', mversion),
176 new StringParameterValue('kversion', kversion.toString()),
177 new StringParameterValue('kgitrepo', kgitrepo),
180 // Launch the parametrized build
181 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
182 println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}"
184 // Add it to the ongoing build queue
185 ongoingBuild.push(param_build)
189 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
193 if (e in InterruptedException) {
194 build.setResult(hudson.model.Result.ABORTED)
195 throw new InterruptedException()
201 // Check for queued similar job since we only want to run latest
202 // as Mathieu Desnoyers requirement
203 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
204 if ( similarJobQueued > 0 ) {
205 // Abort since new build is queued
206 build.setResult(hudson.model.Result.ABORTED)
207 throw new InterruptedException()
210 def i = ongoingBuild.iterator()
211 while ( i.hasNext() ) {
212 currentBuild = i.next()
213 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
218 def matrixParent = currentBuild.get()
219 allBuilds.add(matrixParent)
220 def kernelStr = matrixParent.buildVariableResolver.resolve("kversion")
221 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
223 // Process child runs of matrixBuild
224 def childRuns = matrixParent.getRuns()
225 for ( childRun in childRuns ) {
226 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
227 if (childRun.result != Result.SUCCESS) {
228 failedRuns.add(childRun)
237 // Get log of failed runs
238 for (failedRun in failedRuns) {
239 println "---START---"
240 failedRun.writeWholeLogTo(out)
244 println "---Build report---"
245 for (b in allBuilds) {
246 def kernelStr = b.buildVariableResolver.resolve("kversion")
247 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
254 // Mark this build failed if any child build has failed
256 build.getExecutor().interrupt(Result.FAILURE)