Skip to content
Permalink
Browse files

[FIXED JENKINS-36528] Include AbstractBuild env vars in buildVariables

  • Loading branch information...
abayer committed Sep 13, 2017
1 parent 236f06c commit 6f0dc827046a6c237295d1f01cb94f1b40a51e51
@@ -187,5 +187,11 @@
<version>1.20</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-build-step</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -28,12 +28,14 @@
import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.scm.ChangeLogSet;
import hudson.security.ACL;
import hudson.security.ACLContext;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
@@ -206,7 +208,14 @@ public String getId() throws AbortException {
public @Nonnull Map<String,String> getBuildVariables() throws AbortException {
Run<?,?> build = build();
if (build instanceof AbstractBuild) {
return Collections.unmodifiableMap(((AbstractBuild<?,?>) build).getBuildVariables());
Map<String,String> buildVars = new HashMap<>();
try {
buildVars.putAll(build.getEnvironment(TaskListener.NULL));
} catch (IOException | InterruptedException e) {
// Do nothing
}
buildVars.putAll(((AbstractBuild<?,?>) build).getBuildVariables());
return Collections.unmodifiableMap(buildVars);
} else {
EnvironmentAction.IncludingOverrides env = build.getAction(EnvironmentAction.IncludingOverrides.class);
if (env != null) { // downstream is also WorkflowRun
@@ -24,13 +24,16 @@

package org.jenkinsci.plugins.workflow.support.steps.build;

import hudson.model.FreeStyleProject;
import hudson.model.Messages;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import java.util.regex.Pattern;

import hudson.model.StringParameterDefinition;
import jenkins.plugins.git.GitSampleRepoRule;
import org.hamcrest.Matcher;
import org.hamcrest.core.SubstringMatcher;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -205,6 +208,24 @@
});
}

@Issue("JENKINS-36528")
@Test public void freestyleEnvVars() {
r.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = r.j.createProject(WorkflowJob.class, "pipeline-job");
FreeStyleProject f = r.j.createProject(FreeStyleProject.class, "freestyle-job");
f.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("param", "default")));
p.setDefinition(new CpsFlowDefinition(
"def b = build(job: 'freestyle-job', parameters: [string(name: 'param', value: 'something')])\n" +
"echo \"b.buildVariables.BUILD_TAG='${b.buildVariables.BUILD_TAG}'\"\n" +
"echo \"b.buildVariables.param='${b.buildVariables.param}'\"\n", true));
WorkflowRun b = r.j.assertBuildStatusSuccess(p.scheduleBuild2(0).get());
r.j.assertLogContains("b.buildVariables.BUILD_TAG='jenkins-freestyle-job-1'", b);
r.j.assertLogContains("b.buildVariables.param='something'", b);
}
});
}

// Like org.hamcrest.text.MatchesPattern.matchesPattern(String) but doing a substring, not whole-string, match:
private static Matcher<String> containsRegexp(final String rx) {
return new SubstringMatcher(rx) {

0 comments on commit 6f0dc82

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