Skip to content

Commit

Permalink
[JENKINS-27152] Option for pwd step to return temp directory.
Browse files Browse the repository at this point in the history
Originally-Committed-As: 8c74333321f007e829b2a9581af643b792ef47a6
  • Loading branch information
jglick committed Mar 1, 2016
1 parent a150a06 commit ad40c76
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Expand Up @@ -24,6 +24,8 @@

package org.jenkinsci.plugins.workflow.steps;

import hudson.FilePath;
import hudson.slaves.WorkspaceList;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Test;
Expand All @@ -36,8 +38,19 @@ public class PwdStepTest {

@Test public void basics() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node {echo \"cwd=${pwd()}\"}", true));
r.assertLogContains("cwd=" + r.jenkins.getWorkspaceFor(p), r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
p.setDefinition(new CpsFlowDefinition("node {echo \"cwd='${pwd()}'\"}", true));
r.assertLogContains("cwd='" + r.jenkins.getWorkspaceFor(p) + "'", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
}

// TODO use https://github.com/jenkinsci/jenkins/pull/2066
private static FilePath tempDir(FilePath ws) {
return ws.sibling(ws.getName() + System.getProperty(WorkspaceList.class.getName(), "@") + "tmp");
}

@Test public void tmp() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node {echo \"tmp='${pwd tmp: true}'\"}", true));
r.assertLogContains("tmp='" + tempDir(r.jenkins.getWorkspaceFor(p)) + "'", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
}

}
Expand Up @@ -24,9 +24,12 @@

package org.jenkinsci.plugins.workflow.steps;

import com.google.inject.Inject;
import hudson.Extension;
import hudson.FilePath;
import hudson.slaves.WorkspaceList;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Returns the working directory path.
Expand All @@ -40,8 +43,19 @@
* </pre>
*/
public class PwdStep extends AbstractStepImpl {

private boolean tmp;

@DataBoundConstructor public PwdStep() {}

public boolean isTmp() {
return tmp;
}

@DataBoundSetter public void setTmp(boolean tmp) {
this.tmp = tmp;
}

@Extension public static final class DescriptorImpl extends AbstractStepDescriptorImpl {

public DescriptorImpl() {
Expand All @@ -58,12 +72,18 @@ public DescriptorImpl() {

}

// TODO use https://github.com/jenkinsci/jenkins/pull/2066
private static FilePath tempDir(FilePath ws) {
return ws.sibling(ws.getName() + System.getProperty(WorkspaceList.class.getName(), "@") + "tmp");
}

public static class Execution extends AbstractSynchronousStepExecution<String> {

@StepContextParameter private transient FilePath cwd;
@Inject(optional=true) private transient PwdStep step;

@Override protected String run() throws Exception {
return cwd.getRemote();
return (step.isTmp() ? tempDir(cwd) : cwd).getRemote();
}

private static final long serialVersionUID = 1L;
Expand Down
Expand Up @@ -24,4 +24,8 @@ THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core"/>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="tmp" title="Temporary directory">
<f:checkbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,5 @@
<div>
If selected, return a temporary directory associated with the workspace rather than the workspace itself.
This is an appropriate place to put temporary files which should not clutter a source checkout;
local repositories or caches; etc.
</div>

0 comments on commit ad40c76

Please sign in to comment.