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'))
124 // Get the out variable
125 def config = new HashMap()
126 def bindings = getBinding()
127 config.putAll(bindings.getVariables())
128 def out = config['out']
130 def jlc = new jenkins.model.JenkinsLocationConfiguration()
131 def jenkinsUrl = jlc.url
133 // Get tags from git repository
134 def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call();
136 // Get kernel versions to build
139 def match = ref.getName() =~ /^refs\/tags\/(v[\d\.]+(-rt(\d+)-rebase))$/
142 def v = new kVersion(match.group(1))
144 if (v >= kverfloor) {
153 println "Building the following kernel versions:"
154 for (k in kversions) {
158 // Debug: Stop build here
159 //throw new InterruptedException()
161 def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
164 def ongoingBuild = []
168 // Loop while we have kernel versions remaining or jobs running
169 while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
171 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
172 def kversion = kversions.pop()
174 new StringParameterValue('mversion', mversion),
175 new StringParameterValue('kversion', kversion.toString()),
176 new StringParameterValue('kgitrepo', kgitrepo),
179 // Launch the parametrized build
180 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
181 println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}"
183 // Add it to the ongoing build queue
184 ongoingBuild.push(param_build)
188 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
192 if (e in InterruptedException) {
193 build.setResult(hudson.model.Result.ABORTED)
194 throw new InterruptedException()
200 def i = ongoingBuild.iterator()
201 while ( i.hasNext() ) {
202 currentBuild = i.next()
203 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
208 def matrixParent = currentBuild.get()
209 allBuilds.add(matrixParent)
210 def kernelStr = matrixParent.buildVariableResolver.resolve("kversion")
211 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
213 // Process child runs of matrixBuild
214 def childRuns = matrixParent.getRuns()
215 for ( childRun in childRuns ) {
216 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
217 if (childRun.result != Result.SUCCESS) {
218 failedRuns.add(childRun)
227 // Get log of failed runs
228 for (failedRun in failedRuns) {
229 println "---START---"
230 failedRun.writeWholeLogTo(out)
234 println "---Build report---"
235 for (b in allBuilds) {
236 def kernelStr = b.buildVariableResolver.resolve("kversion")
237 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
242 // Mark this build failed if any child build has failed
244 build.getExecutor().interrupt(Result.FAILURE)