diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index c3c5a16e04..eb22b98a86 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -35,6 +35,7 @@ import hudson.EnvVars; import hudson.Extension; import hudson.model.Item; +import hudson.model.Node; import hudson.model.TaskListener; import hudson.plugins.git.BranchSpec; import hudson.plugins.git.GitException; @@ -53,10 +54,13 @@ import java.io.Writer; import java.net.URISyntaxException; import java.util.Arrays; +import java.util.Objects; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.logging.Level; import java.util.logging.Logger; + +import jenkins.model.Jenkins; import jenkins.scm.api.SCMFile; import jenkins.scm.api.SCMFileSystem; import jenkins.scm.api.SCMHead; @@ -252,19 +256,32 @@ public static class BuilderImpl extends SCMFileSystem.Builder { @Override public boolean supports(SCM source) { - return source instanceof GitSCM + TaskListener listener = new LogTaskListener(LOGGER, Level.FINE); + if (source instanceof GitSCM && ((GitSCM) source).getUserRemoteConfigs().size() == 1 && ((GitSCM) source).getBranches().size() == 1 - && !((GitSCM) source).getBranches().get(0).getName().equals("*") // JENKINS-57587 - && ( - ((GitSCM) source).getBranches().get(0).getName().matches( - "^((\\Q" + Constants.R_HEADS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$" - ) - || ((GitSCM) source).getBranches().get(0).getName().matches( - "^((\\Q" + Constants.R_TAGS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$" - ) + ) { + try { + Node n = (Node) Jenkins.get(); + String branchName = ((GitSCM) source).getBranches().get(0).getName(); + EnvVars env = new EnvVars(EnvVars.masterEnvVars); + env.putAllNonNull((Objects.requireNonNull(n.toComputer())).buildEnvironment(listener)); + ((GitSCM) source).getBranches().get(0).setName(env.expand(branchName)); // JENKINS-64406 + return !((GitSCM) source).getBranches().get(0).getName().equals("*") // JENKINS-57587 + && ( + ((GitSCM) source).getBranches().get(0).getName().matches( + "^((\\Q" + Constants.R_HEADS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$" + ) + || ((GitSCM) source).getBranches().get(0).getName().matches( + "^((\\Q" + Constants.R_TAGS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$" + ) ); - // we only support where the branch spec is obvious and not a wildcard + // we only support where the branch spec is obvious and not a wildcard + } catch (IOException | InterruptedException | NullPointerException e) { + e.printStackTrace(listener.error("Unable to build environment variables")); + } + } + return false; } @Override diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index b4dde88a66..e745cd86ee 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -37,6 +37,8 @@ import java.util.Iterator; import java.util.Set; import java.util.TreeSet; + +import hudson.slaves.EnvironmentVariablesNodeProperty; import jenkins.scm.api.SCMFile; import jenkins.scm.api.SCMFileSystem; import jenkins.scm.api.SCMHead; @@ -427,6 +429,27 @@ public void filesystem_supports_descriptor() throws Exception { assertTrue(SCMFileSystem.supports(descriptor)); } + @Issue("JENKINS-64406") + @Test + public void expand_environment_variables_lightweight_checkout() throws Exception { + setEnvironmentVariables(); + sampleRepo.init(); + GitSCM scm = new GitSCM(GitSCM.createRepoList(sampleRepo.toString(), null), + Collections.singletonList(new BranchSpec("${repoBranchName}")), // Jenkins 64406 + null, null, + Collections.emptyList()); + boolean fs = GitSCMFileSystem.supports(scm); + System.out.println("Success " + scm.getBranches().get(0).getName()); + assertTrue("Branch variable not expanded" + scm.getBranches().get(0).getName(), fs); + } + + private void setEnvironmentVariables() { + EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); + EnvVars envVars = prop.getEnvVars(); + envVars.put("repoBranchName", "Jenkinsfile"); + r.jenkins.getGlobalNodeProperties().add(prop); + } + /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ private boolean isWindows() { return java.io.File.pathSeparatorChar==';';