|
38 | 38 | import hudson.scm.SCM; |
39 | 39 | import hudson.util.FormValidation; |
40 | 40 | import jenkins.model.Jenkins; |
| 41 | +import org.jenkinsci.plugins.workflow.flow.FlowDefinition; |
| 42 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
41 | 43 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
42 | 44 | import org.kohsuke.accmod.Restricted; |
43 | 45 | import org.kohsuke.accmod.restrictions.NoExternalUse; |
@@ -196,18 +198,64 @@ public LibraryCachingConfiguration getCachingConfiguration() { |
196 | 198 |
|
197 | 199 | // without a runParent we can't validateVersion() anyway |
198 | 200 | if (runParent != null) { |
199 | | - // First ask SCM source of the pipeline (if any), |
200 | | - // as the most authoritative source of the branch |
201 | | - // name we want: |
| 201 | + // First, check if envvar BRANCH_NAME is defined? |
| 202 | + // Trust the plugins and situations where it is set. |
202 | 203 | try { |
203 | | - if (run instanceof WorkflowRun) { |
204 | | - // This covers both "Multibranch Pipeline" |
205 | | - // and "Pipeline script from SCM" jobs; |
206 | | - // it also covers "inline" pipeline scripts |
207 | | - // but throws a hudson.AbortException since |
208 | | - // there is no SCM attached. |
209 | | - WorkflowRun wfRun = (WorkflowRun) run; |
210 | | - SCM scm0 = wfRun.getSCMs().get(0); |
| 204 | + runVersion = run.getEnvironment(listener).get("BRANCH_NAME", null); |
| 205 | + } catch (Exception x) { |
| 206 | + // no-op, keep null |
| 207 | + } |
| 208 | + |
| 209 | + if (runVersion == null) { |
| 210 | + // Probably not in a multibranch pipeline workflow |
| 211 | + // type of job? |
| 212 | + // Ask for SCM source of the pipeline (if any), |
| 213 | + // as the most authoritative source of the branch |
| 214 | + // name we want: |
| 215 | + try { |
| 216 | + SCM scm0 = null; |
| 217 | + |
| 218 | + if (runParent instanceof WorkflowJob) { |
| 219 | + // This covers both "Multibranch Pipeline" |
| 220 | + // and "Pipeline script from SCM" jobs; |
| 221 | + // it also covers "inline" pipeline scripts |
| 222 | + // but should return an empty Collection of |
| 223 | + // SCMs since there is no SCM attached to |
| 224 | + // the "static source". |
| 225 | + // TODO: If there are "pre-loaded libraries" |
| 226 | + // in a Jenkins deployment, can they interfere? |
| 227 | + FlowDefinition fd = ((WorkflowJob)runParent).getDefinition(); |
| 228 | + if (fd != null) { |
| 229 | + for (SCM scmN : fd.getSCMs()) { |
| 230 | + if ("hudson.plugins.git.GitSCM".equals(scm0.getClass().getName())) { |
| 231 | + // The best we can do here is accept |
| 232 | + // the first seen SCM (with branch |
| 233 | + // support which we know how to query). |
| 234 | + scm0 = scmN; |
| 235 | + break; |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | +/* |
| 242 | + // NOTE: the list of SCMs used by the run does not |
| 243 | + // seem trustworthy: if an "inline" pipeline script |
| 244 | + // (not from SCM) is used, there is no "relevant" |
| 245 | + // branch name to request; however the list of SCMs |
| 246 | + // would be populated as @Library lines are processed |
| 247 | + // and some SCM sources get checked out. |
| 248 | + if (scm0 == null && run instanceof WorkflowRun) { |
| 249 | + // This covers both "Multibranch Pipeline" |
| 250 | + // and "Pipeline script from SCM" jobs; |
| 251 | + // it also covers "inline" pipeline scripts |
| 252 | + // but throws a hudson.AbortException since |
| 253 | + // there is no SCM attached. |
| 254 | + WorkflowRun wfRun = (WorkflowRun) run; |
| 255 | + scm0 = wfRun.getSCMs().get(0); |
| 256 | + } |
| 257 | +*/ |
| 258 | + |
211 | 259 | if (scm0 != null) { |
212 | 260 | // Avoid importing GitSCM and so requiring that |
213 | 261 | // it is always installed even if not used by |
@@ -255,24 +303,14 @@ public LibraryCachingConfiguration getCachingConfiguration() { |
255 | 303 | } // else SVN, Gerritt or some other SCM - |
256 | 304 | // add handling when needed and known how |
257 | 305 | // or rely on BRANCH_NAME (if set) below... |
258 | | - } |
259 | 306 |
|
260 | | - // Still alive? Chop off leading '*/' |
261 | | - // (if any) from single-branch MBP and |
262 | | - // plain "Pipeline" job definitions. |
263 | | - if (runVersion != null) { |
264 | | - runVersion = runVersion.replaceFirst("^\\*/", ""); |
| 307 | + // Still alive? Chop off leading '*/' |
| 308 | + // (if any) from single-branch MBP and |
| 309 | + // plain "Pipeline" job definitions. |
| 310 | + if (runVersion != null) { |
| 311 | + runVersion = runVersion.replaceFirst("^\\*/", ""); |
| 312 | + } |
265 | 313 | } |
266 | | - } |
267 | | - } catch (Exception x) { |
268 | | - // no-op, keep null |
269 | | - } |
270 | | - |
271 | | - if (runVersion == null) { |
272 | | - // Probably not in a multibranch pipeline workflow |
273 | | - // type of job? Is envvar BRANCH_NAME defined? |
274 | | - try { |
275 | | - runVersion = run.getEnvironment(listener).get("BRANCH_NAME", null); |
276 | 314 | } catch (Exception x) { |
277 | 315 | // no-op, keep null |
278 | 316 | } |
|
0 commit comments