Skip to content

Commit

Permalink
[FIXED JENKINS-31386] Define the scm var in standalone projects using…
Browse files Browse the repository at this point in the history
… CpsScmFlowDefinition.

Originally-Committed-As: fe22dc9fa39267239e6ca16001bd9cd97b0ee48c
  • Loading branch information
jglick committed Dec 4, 2015
1 parent 0f853f6 commit 1bb7638
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Expand Up @@ -36,8 +36,11 @@
import jenkins.branch.Branch;
import jenkins.scm.api.SCMRevisionAction;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScript;
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.pickles.Pickle;
import org.jenkinsci.plugins.workflow.support.pickles.SingleTypedPickleFactory;
Expand All @@ -62,6 +65,13 @@
Job<?,?> job = build.getParent();
BranchJobProperty property = job.getProperty(BranchJobProperty.class);
if (property == null) {
if (job instanceof WorkflowJob) {
FlowDefinition defn = ((WorkflowJob) job).getDefinition();
if (defn instanceof CpsScmFlowDefinition) {
// JENKINS-31386: retrofit to work with standalone projects, minus the exact revision support.
return ((CpsScmFlowDefinition) defn).getScm();
}
}
throw new IllegalStateException("inappropriate context");
}
Branch branch = property.getBranch();
Expand Down
Expand Up @@ -27,4 +27,7 @@ THE SOFTWARE.
<j:jelly xmlns:j="jelly:core">
Represents the SCM configuration in a multibranch project build.
Use <code>checkout scm</code> to check out sources matching <code>Jenkinsfile</code>.
<br/>You may also use this in a standalone project configured with <i>Workflow script from SCM</i>,
though in that case the checkout will just be of the latest revision in the branch,
possibly newer than the revision from which the Workflow script was loaded.
</j:jelly>
Expand Up @@ -33,10 +33,12 @@
import jenkins.plugins.git.GitSCMSource;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.global.WorkflowLibRepository;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.scm.GitSampleRepoRule;
import org.jenkinsci.plugins.workflow.steps.scm.GitStep;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Test;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -121,4 +123,21 @@ public class SCMVarTest {
});
}

@Issue("JENKINS-31386")
@Test public void standaloneProject() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
sampleGitRepo.init();
sampleGitRepo.write("Jenkinsfile", "node {checkout scm; echo readFile('file')}");
sampleGitRepo.write("file", "some content");
sampleGitRepo.git("add", "Jenkinsfile");
sampleGitRepo.git("commit", "--all", "--message=flow");
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsScmFlowDefinition(new GitStep(sampleGitRepo.toString()).createSCM(), "Jenkinsfile"));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("some content", b);
}
});
}

}

0 comments on commit 1bb7638

Please sign in to comment.