Skip to content

Commit

Permalink
Merge pull request #229 from t-8ch/step-environment-contributor
Browse files Browse the repository at this point in the history
[JENKINS-51170] Enable the usage of the StepEnvironmentContributor extensionpoint
  • Loading branch information
dwnusbaum authored Feb 1, 2019
2 parents a18ed94 + 213af04 commit d9f2a6c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.28</version>
<version>3.32</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -68,7 +68,7 @@
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<git-plugin.version>3.1.0</git-plugin.version>
<workflow-support-plugin.version>2.21</workflow-support-plugin.version>
<workflow-support-plugin.version>3.2-rc711.a7efba98306d</workflow-support-plugin.version>
<scm-api-plugin.version>2.2.6</scm-api-plugin.version>
<groovy-cps.version>1.25</groovy-cps.version>
<structs-plugin.version>1.17</structs-plugin.version>
Expand All @@ -77,7 +77,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.18</version>
<version>2.19-rc508.410020b15345</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ private EnvActionImpl() {
}

@Override public EnvVars getEnvironment() throws IOException, InterruptedException {
return getEnvironment(getListener());
}

private TaskListener getListener() throws IOException {
TaskListener listener;
if (owner instanceof FlowExecutionOwner.Executable) {
FlowExecutionOwner executionOwner = ((FlowExecutionOwner.Executable) owner).asFlowExecutionOwner();
Expand All @@ -75,6 +79,10 @@ private EnvActionImpl() {
} else {
listener = new LogTaskListener(LOGGER, Level.INFO);
}
return listener;
}

private EnvVars getEnvironment(TaskListener listener) throws IOException, InterruptedException {
EnvVars e = owner.getEnvironment(listener);
e.putAll(env);
return e;
Expand All @@ -88,7 +96,12 @@ private EnvActionImpl() {
@Override public String getProperty(String propertyName) {
try {
CpsThread t = CpsThread.current();
return EnvironmentExpander.getEffectiveEnvironment(getEnvironment(), t.getContextVariable(EnvVars.class), t.getContextVariable(EnvironmentExpander.class)).get(propertyName);
TaskListener listener = getListener();

return EnvironmentExpander.getEffectiveEnvironment(
getEnvironment(listener), t.getContextVariable(EnvVars.class),
t.getContextVariable(EnvironmentExpander.class), null, listener)
.get(propertyName);
} catch (Exception x) {
LOGGER.log(Level.WARNING, null, x);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,30 @@

package org.jenkinsci.plugins.workflow;

import com.google.common.collect.ImmutableSet;
import hudson.EnvVars;
import hudson.model.EnvironmentContributor;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepEnvironmentContributor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.ClassRule;
import org.junit.Rule;
Expand Down Expand Up @@ -145,4 +153,59 @@ private static class ExpanderImpl extends EnvironmentExpander {
}
}

@Issue("JENKINS-51170")
@Test public void perStepEnvironment() {
story.then(r -> {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("printEnv \"VAR\"; printEnv \"VAR\"", true));
WorkflowRun b = story.j.buildAndAssertSuccess(p);
r.assertLogContains("VAR=1", b);
r.assertLogContains("VAR=2", b);
});
}

@TestExtension("perStepEnvironment") public static class StepEnvAdder extends StepEnvironmentContributor {
private Map<String, Integer> stepNumbers = new HashMap<>();

@Override
public void buildEnvironmentFor(@CheckForNull StepContext stepContext,
@Nonnull EnvVars envs,
@CheckForNull TaskListener listener) throws IOException, InterruptedException {

FlowNode node = stepContext.get(FlowNode.class);
int stepNumber = stepNumbers.computeIfAbsent(node.getId(), (k) -> stepNumbers.size() + 1);
envs.override("VAR", String.valueOf(stepNumber));
}
}

public static class PrintEnvStep extends Step {
private final String var;
@DataBoundConstructor
public PrintEnvStep(String var) {
this.var = var;
}

@Override
public StepExecution start(StepContext context) throws Exception { return new Execution(context, var); }

private static class Execution extends SynchronousStepExecution<Void> {
private final String var;

Execution(StepContext context, String var) { super(context); this.var = var; }

@Override
protected Void run() throws Exception {
StepContext context = getContext();
String message = this.var + "=" + context.get(EnvVars.class).get(var);
context.get(TaskListener.class).getLogger().println(message);
return null;
}
}

@TestExtension("perStepEnvironment") public static class DescriptorImpl extends StepDescriptor {
@Override public String getFunctionName() { return "printEnv"; }
@Override public Set<? extends Class<?>> getRequiredContext() { return ImmutableSet.of(EnvVars.class, TaskListener.class); }
}
}

}

0 comments on commit d9f2a6c

Please sign in to comment.