Skip to content
Permalink
Browse files

Merge pull request #16 from abayer/jenkins-26100

[JENKINS-26100] Return a map of SCM-contributed variables
  • Loading branch information...
jglick committed Jun 20, 2017
2 parents 6b6716f + 65952d9 commit 2d2c718cb1c1b75d791c4e858571fdc9fa1b3951
@@ -1 +1 @@
buildPlugin(jenkinsVersions: [null, '2.32.3'])
buildPlugin()
43 pom.xml
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.23</version>
<version>2.27</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -38,17 +38,18 @@
</pluginRepository>
</pluginRepositories>
<properties>
<jenkins.version>1.642.3</jenkins.version>
<jenkins.version>2.60</jenkins.version>
<java.level>8</java.level>
<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>
<subversion-plugin.version>2.7.2</subversion-plugin.version>
<scm-api-plugin.version>2.1.1</scm-api-plugin.version>
<git-plugin.version>3.3.0</git-plugin.version>
<subversion-plugin.version>2.8</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,41 +105,59 @@
<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.12</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>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mercurial</artifactId>
@@ -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().buildEnvironment(run, envVars);
return envVars;
}

private static final long serialVersionUID = 1L;
@@ -6,6 +6,17 @@
and check if it is shown in the list below.
Then select the SCM to use from the dropdown list and configure it as needed.
</p>
<p>This step returns a <code>Map</code> of any variables the SCM plugin would
set in a Freestyle job, so if your SCM is git, you can do:
</p>
<pre>
def scmVars = checkout scm
def commitHash = scmVars.GIT_COMMIT

// or

def commitHash = checkout(scm).GIT_COMMIT
</pre>
<p>
Any other specific step to run checkouts (like <code>svn</code> or <code>git</code>)
are simplistic options of this step.
@@ -32,10 +32,12 @@
import java.io.File;
import java.util.Iterator;
import java.util.List;

import jenkins.plugins.git.GitSampleRepoRule;
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 {
sampleSvnRepo.init();
sampleSvnRepo.write("Jenkinsfile", "node('remote') {\n" +
" def svnRev = checkout(scm).SVN_REVISION\n" +
" echo \"SVN_REVISION is ${svnRev}\"\n" +
"}\n");
sampleSvnRepo.svnkit("add", sampleSvnRepo.wc() + "/Jenkinsfile");
sampleSvnRepo.svnkit("commit", "--message=+Jenkinsfile", sampleSvnRepo.wc());
long revision = sampleSvnRepo.revision();
WorkflowJob p = r.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsScmFlowDefinition(new SubversionStep(sampleSvnRepo.trunkUrl()).createSCM(), "Jenkinsfile"));

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

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

0 comments on commit 2d2c718

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