Skip to content

Commit

Permalink
Merge pull request #10 from abayer/jenkins-44456
Browse files Browse the repository at this point in the history
[FIXED JENKINS-44456] Add STAGE_NAME to environment
  • Loading branch information
abayer committed May 25, 2017
2 parents 0218206 + 59df6d1 commit 850947f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
28 changes: 19 additions & 9 deletions pom.xml
Expand Up @@ -39,13 +39,23 @@
</pluginRepositories>
<properties>
<jenkins.version>1.642.3</jenkins.version>
<workflow-step-api-plugin.version>2.9</workflow-step-api-plugin.version>
<workflow-step-api-plugin.version>2.10</workflow-step-api-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.11</version>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -62,44 +72,44 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.12</version>
<version>2.14</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.2</version>
<version>2.31</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.9</version>
<version>2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.3</version>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.8</version>
<version>2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.2</version>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.26</version>
<version>1.27</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.google.inject.Inject;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.XmlFile;
import hudson.model.InvisibleAction;
Expand Down Expand Up @@ -35,6 +36,7 @@
import org.jenkinsci.plugins.workflow.graph.FlowStartNode;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
Expand Down Expand Up @@ -64,7 +66,12 @@ public boolean start() throws Exception {
if (step.concurrency != null) {
throw new AbortException(Messages.StageStepExecution_concurrency_not_supported_in_block_mode());
}
getContext().newBodyInvoker().withCallback(BodyExecutionCallback.wrap(getContext())).withDisplayName(step.name).start();
getContext().newBodyInvoker()
.withContexts(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class),
new ExpanderImpl(step.name)))
.withCallback(BodyExecutionCallback.wrap(getContext()))
.withDisplayName(step.name)
.start();
return false;
}
getContext().get(TaskListener.class).getLogger().println(Messages.StageStepExecution_non_block_mode_deprecated());
Expand Down Expand Up @@ -339,6 +346,18 @@ void unblock(String message) {
}
}

private static final class ExpanderImpl extends EnvironmentExpander {
private static final long serialVersionUID = 1;
private final String stageName;
private ExpanderImpl(String stageName) {
this.stageName = stageName;
}
@Override public void expand(EnvVars env) throws IOException, InterruptedException {
env.override("STAGE_NAME", stageName);
}
}


@Extension
public static final class Listener extends RunListener<Run<?,?>> {
@Override public void onCompleted(Run<?,?> r, TaskListener listener) {
Expand Down
Expand Up @@ -67,6 +67,19 @@ public class StageStepTest {
});
}

@Issue("JENKINS-44456")
@Test public void stageNameInEnv() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("stage('foo-stage') {echo \"STAGE_NAME is ${STAGE_NAME}\"}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("STAGE_NAME is foo-stage", b);
story.j.assertLogNotContains(Messages.StageStepExecution_non_block_mode_deprecated(), b);
}
});
}

@Test public void basics() throws Exception {
// Timeline (A has concurrency 2, B 1):
// #1 o-A--------------B-----------------o
Expand Down

0 comments on commit 850947f

Please sign in to comment.