Commit | Line | Data |
---|---|---|
81fa42ce 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 | ||
18 | import hudson.model.* | |
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 | |
24 | ||
25 | ||
26 | // Retrieve parameters of the current build | |
27 | def mversion = build.buildVariableResolver.resolve('mversion') | |
28 | def maxConcurrentBuild = build.buildVariableResolver.resolve('maxConcurrentBuild') | |
29 | def kgitrepo = build.buildVariableResolver.resolve('kgitrepo') | |
30 | def uversion = build.buildVariableResolver.resolve('uversion') | |
31 | def job = Hudson.instance.getJob(build.buildVariableResolver.resolve('kbuildjob')) | |
32 | def currentJobName = build.project.getFullDisplayName() | |
33 | ||
34 | // Get the out variable | |
35 | def config = new HashMap() | |
36 | def bindings = getBinding() | |
37 | config.putAll(bindings.getVariables()) | |
38 | def out = config['out'] | |
39 | ||
40 | def jlc = new jenkins.model.JenkinsLocationConfiguration() | |
41 | def jenkinsUrl = jlc.url | |
42 | ||
43 | // Get tags from git repository | |
44 | def refs = Git.lsRemoteRepository().setTags(true).setRemote(kgitrepo).call(); | |
45 | ||
46 | // Get kernel versions to build | |
47 | def kversions = [] | |
48 | ||
49 | def matchStrs = [] | |
50 | ||
51 | switch (uversion) { | |
52 | case 'xenial': | |
53 | matchStrs = [ | |
54 | ~/^refs\/tags\/(Ubuntu-4\.4\.0-\d{1,3}\.[\d\.]+)$/, | |
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 | ||
72 | for (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 | ||
82 | kversions.sort() | |
83 | ||
84 | // Debug | |
85 | println "Building the following kernel versions:" | |
86 | for (k in kversions) { | |
87 | println k | |
88 | } | |
89 | ||
90 | // Debug: Stop build here | |
91 | //throw new InterruptedException() | |
92 | ||
93 | def joburl = HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName) | |
94 | ||
95 | def allBuilds = [] | |
96 | def ongoingBuild = [] | |
97 | def failedRuns = [] | |
98 | def isFailed = false | |
99 | ||
100 | // Loop while we have kernel versions remaining or jobs running | |
101 | while ( 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 | ||
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 | ||
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 | |
169 | for (failedRun in failedRuns) { | |
170 | println "---START---" | |
171 | failedRun.writeWholeLogTo(out) | |
172 | println "---END---" | |
173 | } | |
174 | ||
175 | println "---Build report---" | |
176 | for (b in allBuilds) { | |
177 | def kernelStr = b.buildVariableResolver.resolve("kversion") | |
178 | println "${b.fullDisplayName} (${kernelStr}) completed with status ${b.result}" | |
179 | // Cleanup builds | |
180 | try { | |
181 | b.delete() | |
182 | } catch (all) {} | |
183 | } | |
184 | ||
185 | // Mark this build failed if any child build has failed | |
186 | if (isFailed) { | |
187 | build.getExecutor().interrupt(Result.FAILURE) | |
188 | } | |
189 | ||
190 | // EOF |