Abort modules build if a new build is queued
[lttng-ci.git] / scripts / lttng-modules / master-ubuntu.groovy
CommitLineData
f3d8604b
MJ
1/**
2 * Copyright (C) 2016 - Michael Jeanson <mjeanson@efficios.com>
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18import hudson.model.*
19import hudson.AbortException
20import hudson.console.HyperlinkNote
21import java.util.concurrent.CancellationException
22import org.eclipse.jgit.api.Git
23import org.eclipse.jgit.lib.Ref
24
25
26// Retrieve parameters of the current build
27def mversion = build.buildVariableResolver.resolve('mversion')
28def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild')
29def kgitrepo = build.buildVariableResolver.resolve('kgitrepo')
30def uversion = build.buildVariableResolver.resolve('uversion')
31def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob'))
bcf4d9d0 32def currentJobName = build.project.getFullDisplayName()
f3d8604b
MJ
33
34// Get the out variable
35def config = new HashMap()
36def bindings = getBinding()
37config.putAll(bindings.getVariables())
38def out = config['out']
39
40def jlc = new jenkins.model.JenkinsLocationConfiguration()
41def jenkinsUrl = jlc.url
42
43// Get tags from git repository
44def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call();
45
46// Get kernel versions to build
47def kversions = []
48
49def matchStrs = []
50
51switch (uversion) {
52 case 'xenial':
53 matchStrs = [
e05248e5 54 ~/^refs\/tags\/(Ubuntu-4\.4\.0-\d{1,3}\.[\d\.]+)$/,
f3d8604b
MJ
55 ~/^refs\/tags\/(Ubuntu-lts-.*_16\.04\.\d+)$/,
56 ]
57 break
58
59 case 'trusty':
60 matchStrs = [
61 ~/^refs\/tags\/(Ubuntu-3\.13\.0-[\d\.]+)$/,
62 ~/^refs\/tags\/(Ubuntu-lts-.*_14\.04\.\d+)$/,
63 ]
64 break
65
66 default:
67 println 'Unsupported Ubuntu version: ${uversion}'
68 throw new InterruptedException()
69 break
70}
71
72for (ref in refs) {
73 for (matchStr in matchStrs) {
74 def match = ref.getName() =~ matchStr
75
76 if (match) {
77 kversions.add(match.group(1))
78 }
79 }
80}
81
82kversions.sort()
83
84// Debug
85println "Building the following kernel versions:"
86for (k in kversions) {
87 println k
88}
89
90// Debug: Stop build here
91//throw new InterruptedException()
92
93def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
94
95def allBuilds = []
96def ongoingBuild = []
97def failedRuns = []
98def isFailed = false
99
100// Loop while we have kernel versions remaining or jobs running
101while ( kversions.size() != 0 || ongoingBuild.size() != 0 ) {
102
103 if(ongoingBuild.size() < maxConcurrentBuild.toInteger() && kversions.size() != 0) {
104 def kversion = kversions.pop()
105 def job_params = [
106 new StringParameterValue('mversion', mversion),
107 new StringParameterValue('kversion', kversion),
108 new StringParameterValue('kgitrepo', kgitrepo),
109 ]
110
111 // Launch the parametrized build
112 def param_build = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(job_params))
113 println "triggering ${joburl} for the ${mversion} branch on kernel ${kversion}"
114
115 // Add it to the ongoing build queue
116 ongoingBuild.push(param_build)
117
118 } else {
119
120 println "Waiting... Queued: " + kversions.size() + " Running: " + ongoingBuild.size()
121 try {
122 Thread.sleep(5000)
123 } catch(e) {
124 if (e in InterruptedException) {
125 build.setResult(hudson.model.Result.ABORTED)
126 throw new InterruptedException()
127 } else {
128 throw(e)
129 }
130 }
131
bcf4d9d0
JR
132 // Check for queued similar job since we only want to run latest
133 // as Mathieu Desnoyers requirement
134 similarJobQueued = Hudson.instance.queue.items.count{it.task.getFullDisplayName() == currentJobName}
135 if ( similarJobQueued > 0 ) {
136 // Abort since new build is queued
137 build.setResult(hudson.model.Result.ABORTED)
138 throw new InterruptedException()
139 }
140
f3d8604b
MJ
141 def i = ongoingBuild.iterator()
142 while ( i.hasNext() ) {
143 currentBuild = i.next()
144 if ( currentBuild.isCancelled() || currentBuild.isDone() ) {
145 // Remove from queue
146 i.remove()
147
148 // Print results
149 def matrixParent = currentBuild.get()
150 allBuilds.add(matrixParent)
151 def kernelStr = matrixParent.buildVariableResolver.resolve("kversion")
152 println "${matrixParent.fullDisplayName} (${kernelStr}) completed with status ${matrixParent.result}"
153
154 // Process child runs of matrixBuild
155 def childRuns = matrixParent.getRuns()
156 for ( childRun in childRuns ) {
157 println "\t${childRun.fullDisplayName} (${kernelStr}) completed with status ${childRun.result}"
158 if (childRun.result != Result.SUCCESS) {
159 failedRuns.add(childRun)
160 isFailed = true
161 }
162 }
163 }
164 }
165 }
166}
167
168// Get log of failed runs
169for (failedRun in failedRuns) {
170 println "---START---"
171 failedRun.writeWholeLogTo(out)
172 println "---END---"
173}
174
175println "---Build report---"
176for (b in allBuilds) {
177 def kernelStr = b.buildVariableResolver.resolve("kversion")
178 println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}"
7e02032c 179 // Cleanup builds
f3d8604b
MJ
180 b.delete()
181}
182
183// Mark this build failed if any child build has failed
184if (isFailed) {
185 build.getExecutor().interrupt(Result.FAILURE)
186}
187
188// EOF
This page took 0.028031 seconds and 4 git commands to generate.