Skip to content
Permalink
Browse files

[FIXED JENKINS-42836] env expand the script path from SCM.

  • Loading branch information
abayer committed Feb 23, 2018
1 parent b3d7ea3 commit 2dc4bf82a1e095b09dde4638a50c893320432f93
@@ -103,11 +103,12 @@ public boolean isLightweight() {
throw new IOException("can only check out SCM into a Run");
}
Run<?,?> build = (Run<?,?>) _build;
String expandedScriptPath = build.getEnvironment(listener).expand(scriptPath);
if (isLightweight()) {
try (SCMFileSystem fs = SCMFileSystem.of(build.getParent(), scm)) {
if (fs != null) {
String script = fs.child(scriptPath).contentAsString();
listener.getLogger().println("Obtained " + scriptPath + " from " + scm.getKey());
String script = fs.child(expandedScriptPath).contentAsString();
listener.getLogger().println("Obtained " + expandedScriptPath + " from " + scm.getKey());
Queue.Executable exec = owner.getExecutable();
FlowDurabilityHint hint = (exec instanceof Item) ? DurabilityHintProvider.suggestedFor((Item)exec) : GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint();
return new CpsFlowExecution(script, true, owner, hint);
@@ -127,7 +128,7 @@ public boolean isLightweight() {
} else { // should not happen, but just in case:
dir = new FilePath(owner.getRootDir());
}
listener.getLogger().println("Checking out " + scm.getKey() + " into " + dir + " to read " + scriptPath);
listener.getLogger().println("Checking out " + scm.getKey() + " into " + dir + " to read " + expandedScriptPath);
String script = null;
Computer computer = node.toComputer();
if (computer == null) {
@@ -161,8 +162,8 @@ public boolean isLightweight() {
listener.getLogger().println("Retrying after 10 seconds");
Thread.sleep(10000);
}
FilePath scriptFile = dir.child(scriptPath);

FilePath scriptFile = dir.child(expandedScriptPath);
if (!scriptFile.absolutize().getRemote().replace('\\', '/').startsWith(dir.absolutize().getRemote().replace('\\', '/') + '/')) { // TODO JENKINS-26838
throw new IOException(scriptFile + " is not inside " + dir);
}
@@ -202,4 +202,22 @@
r.assertLogContains("version one", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("VERSION", "one")))));
}

@Issue("JENKINS-42836")
@Test public void usingParameterInScriptPath() throws Exception {
sampleRepo.init();
sampleRepo.write("flow.groovy", "echo 'version one'");
sampleRepo.git("add", "flow.groovy");
sampleRepo.write("otherFlow.groovy", "echo 'version two'");
sampleRepo.git("add", "otherFlow.groovy");
sampleRepo.git("commit", "--all", "--message=commits");
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitSCM(Collections.singletonList(new UserRemoteConfig(sampleRepo.fileUrl(), null, null, null)),
Collections.singletonList(new BranchSpec("master")),
false, Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList()), "${SCRIPT_PATH}");
def.setLightweight(false); // TODO SCMFileSystem.of cannot pick up build parameters
p.setDefinition(def);
p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("SCRIPT_PATH", "flow.groovy")));
r.assertLogContains("version one", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
r.assertLogContains("version two", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("SCRIPT_PATH", "otherFlow.groovy")))));
}
}

0 comments on commit 2dc4bf8

Please sign in to comment.
You can’t perform that action at this time.