Skip to content
Permalink
Browse files

[JENKINS-26100] Return a map of SCM-contributed variables

  • Loading branch information
abayer committed May 1, 2017
1 parent 9945d0a commit cf36f5b5daa78f8fc4480328c5ac6ac6b4841032
35 pom.xml
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.23</version>
<version>2.27-20170501.155323-2</version> <!-- TODO: Switch to 2.27 once https://github.com/jenkinsci/plugin-pom/pull/59 is released -->
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -38,17 +38,18 @@
</pluginRepository>
</pluginRepositories>
<properties>
<jenkins.version>1.642.3</jenkins.version>
<jenkins-core.version>2.58-20170501.171905-3</jenkins-core.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/jenkins/pull/2730 is merged and released -->
<jenkins-war.version>2.58-20170501.171927-3</jenkins-war.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/jenkins/pull/2730 is merged and released -->
<no-test-jar>false</no-test-jar>
<scm-api-plugin.version>2.0.8</scm-api-plugin.version>
<git-plugin.version>3.1.0</git-plugin.version>
<scm-api-plugin.version>2.1.1</scm-api-plugin.version>
<git-plugin.version>3.3.1-20170501.172949-1</git-plugin.version> <!-- TODO: Switch to release once https://github.com/jenkinsci/git-plugin/pull/492 is merged and released -->
<subversion-plugin.version>2.7.2</subversion-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>workflow-step-api</artifactId>
<version>1.15</version>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -104,38 +105,50 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>1.15</version>
<version>2.9</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>1.15</version>
<version>2.29</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-multibranch</artifactId>
<version>2.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>5.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>1.15</version>
<version>2.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>1.15</version>
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>1.15</version>
<version>2.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>1.15</version>
<version>2.14</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
@@ -34,6 +34,8 @@

import java.io.File;
import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Nonnull;
import javax.inject.Inject;
@@ -70,7 +72,7 @@ public boolean isChangelog() {

protected abstract @Nonnull SCM createSCM();

public static final class StepExecutionImpl extends AbstractSynchronousNonBlockingStepExecution<Void> {
public static final class StepExecutionImpl extends AbstractSynchronousNonBlockingStepExecution<Map<String,String>> {

@Inject private transient SCMStep step;
@StepContextParameter private transient Run<?,?> run;
@@ -79,9 +81,11 @@ public boolean isChangelog() {
@StepContextParameter private transient Launcher launcher;

@Override
protected Void run() throws Exception {
protected Map<String,String> run() throws Exception {
step.checkout(run, workspace, listener, launcher);
return null;
Map<String,String> envVars = new TreeMap<>();
step.createSCM().buildEnvVars(run, envVars);
return envVars;
}

private static final long serialVersionUID = 1L;
@@ -32,10 +32,12 @@
import java.io.File;
import java.util.Iterator;
import java.util.List;
import jenkins.plugins.git.GitSampleRepoRule;

import jenkins.plugins.git.*;
import jenkins.scm.impl.subversion.SubversionSampleRepoRule;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
@@ -55,6 +57,29 @@
@Rule public GitSampleRepoRule sampleGitRepo = new GitSampleRepoRule();
@Rule public SubversionSampleRepoRule sampleSvnRepo = new SubversionSampleRepoRule();

@Issue("JENKINS-26100")
@Test
public void scmVars() throws Exception {
r.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
sampleGitRepo.init();
sampleGitRepo.write("Jenkinsfile", "node('remote') {\n" +
" def gitCommit = checkout(scm).GIT_COMMIT\n" +
" echo \"GIT_COMMIT is ${gitCommit}\"\n" +
"}\n");
sampleGitRepo.git("add", "Jenkinsfile");
sampleGitRepo.git("commit", "--message=files");
String commitHash = sampleGitRepo.head();
WorkflowJob p = r.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsScmFlowDefinition(new jenkins.plugins.git.GitStep(sampleGitRepo.toString()).createSCM(), "Jenkinsfile"));

r.j.createOnlineSlave(Label.get("remote"));
WorkflowRun b = r.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.j.assertLogContains("GIT_COMMIT is " + commitHash, b);
}
});
}

@Issue("JENKINS-26761")
@Test public void checkoutsRestored() throws Exception {
r.addStep(new Statement() {

0 comments on commit cf36f5b

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