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

Commit

Permalink
[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 149577f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 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.assertEquals;

public class StashTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
Expand Down Expand Up @@ -69,4 +70,30 @@ 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/other', text: 'more'\n" +
" dir('subdir') {stash name:'whatever', useDefaultExcludes: false}\n" +
"}\n" +
"node {\n" +
" dir('elsewhere') {\n" +
" unstash 'whatever'\n" +
" echo \"got .gitignore: ${readFile '.gitignore'} other: ${readFile 'other'}\"\n" +
" }\n" +
" writeFile file: 'at-top', text: 'ignored'\n" +
" stash name: 'from-top', includes: 'elsewhere/'\n" +
" semaphore 'ending'\n" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("ending/1", b);
assertEquals("{from-top={elsewhere/other=more}, whatever={.gitignore=whatever, other=more}}", StashManager.stashesOf(b).toString());
SemaphoreStep.success("ending/1", null);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("got .gitignore: whatever other: more", b);
assertEquals("{}", StashManager.stashesOf(b).toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,12 @@
package org.jenkinsci.plugins.workflow.flow;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.AbortException;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.FilePath;
import hudson.Util;
import hudson.*;
import hudson.model.Run;
import hudson.model.TaskListener;
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 +39,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 @@ -73,9 +64,11 @@ public class StashManager {
* @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 +77,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 getUseDefaultExcludes() {
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

0 comments on commit 149577f

Please sign in to comment.