Skip to content

Commit ba5e325

Browse files
committed
LibraryConfiguration: defaultedVersion(): check for BRANCH_NAME first; and then it is safer to check getSCMs() not for WorkflowRun, but for the FlowDefinition from WorkflowJob [JENKINS-69731]
1 parent 768f251 commit ba5e325

File tree

1 file changed

+65
-27
lines changed

1 file changed

+65
-27
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryConfiguration.java

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import hudson.scm.SCM;
3939
import hudson.util.FormValidation;
4040
import jenkins.model.Jenkins;
41+
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
42+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
4143
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
4244
import org.kohsuke.accmod.Restricted;
4345
import org.kohsuke.accmod.restrictions.NoExternalUse;
@@ -196,18 +198,64 @@ public LibraryCachingConfiguration getCachingConfiguration() {
196198

197199
// without a runParent we can't validateVersion() anyway
198200
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.
202203
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+
211259
if (scm0 != null) {
212260
// Avoid importing GitSCM and so requiring that
213261
// it is always installed even if not used by
@@ -255,24 +303,14 @@ public LibraryCachingConfiguration getCachingConfiguration() {
255303
} // else SVN, Gerritt or some other SCM -
256304
// add handling when needed and known how
257305
// or rely on BRANCH_NAME (if set) below...
258-
}
259306

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+
}
265313
}
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);
276314
} catch (Exception x) {
277315
// no-op, keep null
278316
}

0 commit comments

Comments
 (0)