Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-26942] File stashing.
Originally-Committed-As: 1c4a4416ed06ec5311ffac57f26957eab831b03e
  • Loading branch information
jglick committed Aug 20, 2015
1 parent de5ee75 commit 5f29503
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 0 deletions.
@@ -0,0 +1,72 @@
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.steps.stash;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.flow.StashManager;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class StashTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public JenkinsRule r = new JenkinsRule();

@Issue("JENKINS-26942")
@Test public void smokes() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" writeFile file: 'subdir/fname', text: 'whatever'\n" +
" writeFile file: 'subdir/other', text: 'more'\n" +
" dir('subdir') {stash 'whatever'}\n" +
"}\n" +
"node {\n" +
" dir('elsewhere') {\n" +
" unstash 'whatever'\n" +
" echo \"got fname: ${readFile 'fname'} other: ${readFile 'other'}\"\n" +
" }\n" +
" writeFile file: 'at-top', text: 'ignored'\n" +
" stash name: 'from-top', includes: 'elsewhere/', excludes: '**/other'\n" +
" semaphore 'ending'\n" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("ending/1", b);
assertEquals("{from-top={elsewhere/fname=whatever}, whatever={fname=whatever, other=more}}", StashManager.stashesOf(b).toString());
SemaphoreStep.success("ending/1", null);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("got fname: whatever other: more", b);
assertEquals("{}", StashManager.stashesOf(b).toString());
}

}
@@ -0,0 +1,105 @@
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.support.steps.stash;

import com.google.inject.Inject;
import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.flow.StashManager;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class StashStep extends AbstractStepImpl {

private final @Nonnull String name;
private @CheckForNull String includes;
private @CheckForNull String excludes;

@DataBoundConstructor public StashStep(@Nonnull String name) {
Jenkins.checkGoodName(name);
this.name = name;
}

public String getName() {
return name;
}

public String getIncludes() {
return includes;
}

@DataBoundSetter public void setIncludes(String includes) {
this.includes = Util.fixEmpty(includes);
}

public String getExcludes() {
return excludes;
}

@DataBoundSetter public void setExcludes(String excludes) {
this.excludes = Util.fixEmpty(excludes);
}

public static class Execution extends AbstractSynchronousNonBlockingStepExecution<Void> {

@Inject private transient StashStep step;
@StepContextParameter private transient Run<?,?> build;
@StepContextParameter private transient FilePath workspace;
@StepContextParameter private transient TaskListener listener;

@Override protected Void run() throws Exception {
StashManager.stash(build, step.name, workspace, listener, step.includes, step.excludes);
return null;
}

}

@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {

public DescriptorImpl() {
super(Execution.class);
}

@Override public String getFunctionName() {
return "stash";
}

@Override public String getDisplayName() {
return "Stash some files to be used later in the build";
}

}

}
@@ -0,0 +1,84 @@
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.support.steps.stash;

import com.google.inject.Inject;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Run;
import hudson.model.TaskListener;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.flow.StashManager;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;

public class UnstashStep extends AbstractStepImpl {

private final @Nonnull String name;

@DataBoundConstructor public UnstashStep(@Nonnull String name) {
Jenkins.checkGoodName(name);
this.name = name;
}

public String getName() {
return name;
}

public static class Execution extends AbstractSynchronousNonBlockingStepExecution<Void> {

@Inject private transient UnstashStep step;
@StepContextParameter private transient Run<?,?> build;
@StepContextParameter private transient FilePath workspace;
@StepContextParameter private transient TaskListener listener;

@Override protected Void run() throws Exception {
StashManager.unstash(build, step.name, workspace, listener);
return null;
}

}

@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {

public DescriptorImpl() {
super(Execution.class);
}

@Override public String getFunctionName() {
return "unstash";
}

@Override public String getDisplayName() {
return "Restore files previously stashed";
}

}

}
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2015 CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="name" title="Name">
<f:textbox/>
</f:entry>
<f:entry field="includes" title="Includes">
<f:textbox/>
</f:entry>
<f:entry field="excludes" title="Excludes">
<f:textbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Optional set of Ant-style exclude patterns.
</div>
@@ -0,0 +1,7 @@
<div>
Optional set of Ant-style include patterns.
If blank, treated like <code>**</code>: all files.
The current working directory is the base directory for the saved files,
which will later be restored in the same relative locations,
so if you want to use a subdirectory wrap this in <code>dir</code>.
</div>
@@ -0,0 +1,4 @@
<div>
Name of a stash.
Should be a simple identifier akin to a job name.
</div>
@@ -0,0 +1,4 @@
<div>
Saves a set of files for use later in the same build, generally on another node/workspace.
Stashed files are not otherwise available and are generally discarded at the end of the build.
</div>
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2015 CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="name" title="Name">
<f:textbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Name of a previously saved stash.
</div>
@@ -0,0 +1,3 @@
<div>
Restores a set of files previously <code>stash</code>ed into the current workspace.
</div>

0 comments on commit 5f29503

Please sign in to comment.