Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-33273] Allow lightweight checkouts to be used from CpsScmFlo…
…wDefinition.
  • Loading branch information
jglick committed Jan 13, 2017
1 parent 12934f4 commit 5960d4b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Expand Up @@ -33,7 +33,7 @@
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.24-SNAPSHOT</version>
<version>2.24-beta-1-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Pipeline: Groovy</name>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Groovy+Plugin</url>
Expand Down Expand Up @@ -83,7 +83,7 @@
<version>2.11</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>1.15</version>
</dependency>
Expand All @@ -95,7 +95,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>1.1</version>
<version>2.0.1-beta-3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -116,7 +116,7 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>1.15</version>
<scope>test</scope>
Expand Down Expand Up @@ -199,7 +199,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.4.1</version>
<version>3.0.2-beta-2</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down
Expand Up @@ -42,6 +42,7 @@
import java.util.Collection;
import java.util.List;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMFileSystem;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.JOB;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
Expand All @@ -51,6 +52,7 @@
import org.jenkinsci.plugins.workflow.steps.scm.SCMStep;
import org.jenkinsci.plugins.workflow.support.actions.WorkspaceActionImpl;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -59,6 +61,7 @@ public class CpsScmFlowDefinition extends FlowDefinition {

private final SCM scm;
private final String scriptPath;
private boolean lightweight;

@DataBoundConstructor public CpsScmFlowDefinition(SCM scm, String scriptPath) {
this.scm = scm;
Expand All @@ -73,18 +76,35 @@ public String getScriptPath() {
return scriptPath;
}

public boolean isLightweight() {
return lightweight;
}

@DataBoundSetter public void setLightweight(boolean lightweight) {
this.lightweight = lightweight;
}

@Override public CpsFlowExecution create(FlowExecutionOwner owner, TaskListener listener, List<? extends Action> actions) throws Exception {
for (Action a : actions) {
if (a instanceof CpsFlowFactoryAction2) {
return ((CpsFlowFactoryAction2) a).create(this, owner, actions);
}
}
FilePath dir;
Queue.Executable _build = owner.getExecutable();
if (!(_build instanceof Run)) {
throw new IOException("can only check out SCM into a Run");
}
Run<?,?> build = (Run<?,?>) _build;
if (lightweight) {
SCMFileSystem fs = SCMFileSystem.of(build.getParent(), scm);
if (fs == null) {
throw new AbortException("Lightweight checkout support not available");
}
String script = fs.child(scriptPath).contentAsString();
listener.getLogger().println("Obtained " + scriptPath + " from " + scm.getKey());
return new CpsFlowExecution(script, true, owner);
}
FilePath dir;
Node node = Jenkins.getActiveInstance();
if (build.getParent() instanceof TopLevelItem) {
FilePath baseWorkspace = node.getWorkspaceFor((TopLevelItem) build.getParent());
Expand Down Expand Up @@ -138,10 +158,12 @@ private String getFilePathSuffix() {

public Collection<? extends SCMDescriptor<?>> getApplicableDescriptors() {
StaplerRequest req = Stapler.getCurrentRequest();
Job job = req != null ? req.findAncestorObject(Job.class) : null;
return job != null ? SCM._for(job) : /* TODO 1.599+ does this for job == null */ SCM.all();
Job<?,?> job = req != null ? req.findAncestorObject(Job.class) : null;
return SCM._for(job);
}

// TODO doCheckLightweight impossible to write even though we have SCMFileSystem.supports(SCM), because form validation cannot pass the SCM object

}

}
Expand Up @@ -29,6 +29,9 @@ THE SOFTWARE.
<f:entry field="scriptPath" title="${%Script Path}">
<f:textbox default="Jenkinsfile"/>
</f:entry>
<f:entry field="lightweight" title="${%Lightweight checkout}">
<f:checkbox/>
</f:entry>
<f:block>
<a href="pipeline-syntax" target="_blank">${%Pipeline Syntax}</a>
</f:block>
Expand Down
@@ -0,0 +1,5 @@
<div>
If selected, try to obtain the Pipeline script contents directly from the SCM without performing a full checkout.
The advantage of this mode is its efficiency; however, you will not get any changelogs or polling based on the SCM.
Only selected SCM plugins support this mode.
</div>
Expand Up @@ -125,6 +125,24 @@ public class CpsScmFlowDefinitionTest {
assertEquals(Collections.emptyList(), changeSets);
}

@Issue("JENKINS-33273")
@Test public void lightweight() throws Exception {
sampleRepo.init();
sampleRepo.write("flow.groovy", "echo 'version one'");
sampleRepo.git("add", "flow.groovy");
sampleRepo.git("commit", "--message=init");
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
GitStep step = new GitStep(sampleRepo.toString());
step.setCredentialsId("nonexistent"); // TODO work around NPE pending https://github.com/jenkinsci/git-plugin/pull/467
CpsScmFlowDefinition def = new CpsScmFlowDefinition(step.createSCM(), "flow.groovy");
def.setLightweight(true);
p.setDefinition(def);
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.assertLogNotContains("Cloning the remote Git repository", b);
r.assertLogContains("Obtained flow.groovy from git " + sampleRepo, b);
r.assertLogContains("version one", b);
}

@Issue("JENKINS-28447")
@Test public void usingParameter() throws Exception {
sampleRepo.init();
Expand Down

0 comments on commit 5960d4b

Please sign in to comment.