Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #350 from jglick/temp-dir-JENKINS-27152
Browse files Browse the repository at this point in the history
[JENKINS-27152] Option for pwd step to return temp directory
  • Loading branch information
jglick committed Mar 4, 2016
2 parents 7d2004e + 8f78b04 commit 7bbcab0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,10 @@

Only noting significant user changes, not internal code cleanups and minor bug fixes.

## 1.15 (upcoming)

* [JENKINS-27152](https://issues.jenkins-ci.org/browse/JENKINS-27152): offering `tmp` option to `pwd` step.

## 1.14 (Feb 25 2016)

* [JENKINS-32727](https://issues.jenkins-ci.org/browse/JENKINS-32727): new facility to replay Pipeline builds with a modified script.
Expand Down
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 1.652 use WorkspaceList.tempDir
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 1.652 use WorkspaceList.tempDir
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 7bbcab0

Please sign in to comment.