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

Commit

Permalink
[FIXED JENKINS-31086] Add useDefaultExcludes option to stash
Browse files Browse the repository at this point in the history
Defaults to true, meaning use Ant default excludes for
DirScanner.Glob. If false, don't use the default excludes, to pick up
files like .gitignore etc.
  • Loading branch information
abayer committed Dec 7, 2015
1 parent 85330f6 commit 5ceda70
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Only noting significant user changes, not internal code cleanups and minor bug f
## 1.12 (upcoming)

* [JENKINS-29413](https://issues.jenkins-ci.org/browse/JENKINS-29413): hung build when running the `parallel` step with an empty map.
* [JENKINS-31086](https://issues.jenkins-ci.org/browse/JENKINS-31086): Added `useDefaultExcludes` option, defaulting to true, to `stash`. When false, Ant default excludes are not used.

## 1.12-beta-2 (Nov 25 2015)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
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.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;

public class StashTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
Expand Down Expand Up @@ -69,4 +70,26 @@ public class StashTest {
assertEquals("{}", StashManager.stashesOf(b).toString());
}

@Issue("JENKINS-31086")
@Test public void testDefaultExcludes() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" writeFile file: 'subdir/.gitignore', text: 'whatever'\n" +
" writeFile file: 'subdir/otherfile', text: 'whatever'\n" +
" dir('subdir') {stash name:'has-gitignore', useDefaultExcludes: false}\n" +
" dir('subdir') {stash name:'no-gitignore' }\n" +
" dir('first-unstash') {\n" +
" unstash('has-gitignore')\n" +
" echo \"gitignore exists? ${fileExists '.gitignore'}\"\n" +
" }\n" +
" dir('second-unstash') {\n" +
" unstash('no-gitignore')\n" +
" echo \"gitignore does not exist? ${fileExists '.gitignore'}\"\n" +
" }\n" +
"}", true));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.assertLogContains("gitignore exists? true", b);
r.assertLogContains("gitignore does not exist? false", b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@
import hudson.org.apache.tools.tar.TarInputStream;
import hudson.util.DirScanner;
import hudson.util.io.ArchiverFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.ArtifactManager;
import jenkins.model.Jenkins;
import org.apache.commons.io.FileUtils;
Expand All @@ -54,6 +43,12 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.*;
import java.util.Map;
import java.util.TreeMap;

/**
* Manages per-build stashes of files.
* Unlike artifacts managed by {@link ArtifactManager}, stashes:
Expand All @@ -66,16 +61,24 @@
*/
public class StashManager {

@Deprecated
public static void stash(@Nonnull Run<?,?> build, @Nonnull String name, @Nonnull FilePath workspace, @Nonnull TaskListener listener,
@CheckForNull String includes, @CheckForNull String excludes) throws IOException, InterruptedException {
stash(build, name, workspace, listener, includes, excludes, true);
}

/**
* Saves a stash of some files from a build.
* @param build a build to use as storage
* @param name a simple name to assign to the stash (must follow {@link Jenkins#checkGoodName} constraints)
* @param workspace a directory to use as a base
* @param includes a set of Ant-style file includes, separated by commas; null/blank is allowed as a synonym for {@code **} (i.e., everything)
* @param excludes an optional set of Ant-style file excludes
* @param useDefaultExcludes whether to use Ant default excludes
*/
@SuppressFBWarnings(value="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", justification="fine if mkdirs returns false")
public static void stash(@Nonnull Run<?,?> build, @Nonnull String name, @Nonnull FilePath workspace, @Nonnull TaskListener listener, @CheckForNull String includes, @CheckForNull String excludes) throws IOException, InterruptedException {
public static void stash(@Nonnull Run<?,?> build, @Nonnull String name, @Nonnull FilePath workspace, @Nonnull TaskListener listener,
@CheckForNull String includes, @CheckForNull String excludes, boolean useDefaultExcludes) throws IOException, InterruptedException {
Jenkins.checkGoodName(name);
File storage = storage(build, name);
storage.getParentFile().mkdirs();
Expand All @@ -84,7 +87,7 @@ public static void stash(@Nonnull Run<?,?> build, @Nonnull String name, @Nonnull
}
OutputStream os = new FileOutputStream(storage);
try {
int count = workspace.archive(ArchiverFactory.TARGZ, os, new DirScanner.Glob(Util.fixEmpty(includes) == null ? "**" : includes, excludes));
int count = workspace.archive(ArchiverFactory.TARGZ, os, new DirScanner.Glob(Util.fixEmpty(includes) == null ? "**" : includes, excludes, useDefaultExcludes));
if (count == 0) {
throw new AbortException("No files included in stash");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
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;
Expand All @@ -41,11 +39,15 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public class StashStep extends AbstractStepImpl {

private final @Nonnull String name;
private @CheckForNull String includes;
private @CheckForNull String excludes;
private boolean useDefaultExcludes = true;

@DataBoundConstructor public StashStep(@Nonnull String name) {
Jenkins.checkGoodName(name);
Expand All @@ -72,6 +74,14 @@ public String getExcludes() {
this.excludes = Util.fixEmpty(excludes);
}

public boolean isUseDefaultExcludes() {
return useDefaultExcludes;
}

@DataBoundSetter public void setUseDefaultExcludes(boolean useDefaultExcludes) {
this.useDefaultExcludes = useDefaultExcludes;
}

public static class Execution extends AbstractSynchronousNonBlockingStepExecution<Void> {

private static final long serialVersionUID = 1L;
Expand All @@ -82,7 +92,8 @@ public static class Execution extends AbstractSynchronousNonBlockingStepExecutio
@StepContextParameter private transient TaskListener listener;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ THE SOFTWARE.
<f:entry field="excludes" title="Excludes">
<f:textbox/>
</f:entry>
<f:entry field="useDefaultExcludes" title="Use Default Ant Excludes">
<f:checkbox default="true" />
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
If selected, use the default excludes from Ant - see
<a href="http://ant.apache.org/manual/dirtasks.html#defaultexcludes" target="_blank">here</a> for the list.
</div>

0 comments on commit 5ceda70

Please sign in to comment.