diff --git a/core/src/main/java/hudson/tasks/ArtifactArchiver.java b/core/src/main/java/hudson/tasks/ArtifactArchiver.java
index 7faa7b8986a5105bd7821faea1d4cd489f64777a..552c17347303e2eda9ed00ff58d806d79e15e7a6 100644
--- a/core/src/main/java/hudson/tasks/ArtifactArchiver.java
+++ b/core/src/main/java/hudson/tasks/ArtifactArchiver.java
@@ -74,16 +74,26 @@ public class ArtifactArchiver extends Recorder {
@Nonnull
private Boolean allowEmptyArchive;
+ /**
+ * Archive only if build is successful, skip archiving on failed builds.
+ */
+ private boolean onlyIfSuccessful;
+
public ArtifactArchiver(String artifacts, String excludes, boolean latestOnly) {
- this(artifacts, excludes, latestOnly, false);
+ this(artifacts, excludes, latestOnly, false, false);
}
- @DataBoundConstructor
public ArtifactArchiver(String artifacts, String excludes, boolean latestOnly, boolean allowEmptyArchive) {
+ this(artifacts, excludes, latestOnly, allowEmptyArchive, false);
+ }
+
+ @DataBoundConstructor
+ public ArtifactArchiver(String artifacts, String excludes, boolean latestOnly, boolean allowEmptyArchive, boolean onlyIfSuccessful) {
this.artifacts = artifacts.trim();
this.excludes = Util.fixEmptyAndTrim(excludes);
this.latestOnly = latestOnly;
this.allowEmptyArchive = allowEmptyArchive;
+ this.onlyIfSuccessful = onlyIfSuccessful;
}
// Backwards compatibility for older builds
@@ -106,6 +116,10 @@ public class ArtifactArchiver extends Recorder {
return latestOnly;
}
+ public boolean isOnlyIfSuccessful() {
+ return onlyIfSuccessful;
+ }
+
public boolean getAllowEmptyArchive() {
return allowEmptyArchive;
}
@@ -126,6 +140,11 @@ public class ArtifactArchiver extends Recorder {
return true;
}
+ if (build.getResult().isWorseThan(Result.UNSTABLE) && onlyIfSuccessful) {
+ listener.getLogger().println(Messages.ArtifactArchiver_SkipBecauseOnlyIfSuccessful());
+ return true;
+ }
+
listener.getLogger().println(Messages.ArtifactArchiver_ARCHIVING_ARTIFACTS());
try {
FilePath ws = build.getWorkspace();
diff --git a/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.jelly b/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.jelly
index 659b36011d8170ba1084fa432af5548631b39395..d3e6b62690b1b598a214f4fba13ea16d0f11364a 100644
--- a/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.jelly
+++ b/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.jelly
@@ -37,5 +37,8 @@ THE SOFTWARE.
+
+
+
\ No newline at end of file
diff --git a/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.properties b/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.properties
index 3a6afefe2c61eba6dcdbada68ce146a645a151fe..9a50e12c18dff5f4e58ea3f4ec049d276acd28f8 100644
--- a/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.properties
+++ b/core/src/main/resources/hudson/tasks/ArtifactArchiver/config.properties
@@ -21,4 +21,5 @@
# THE SOFTWARE.
lastBuildOnly=Discard all but the last successful/stable artifact to save disk space
-allowEmptyArchive=Do not fail build if archiving returns nothing
\ No newline at end of file
+allowEmptyArchive=Do not fail build if archiving returns nothing
+onlyIfSuccessful=Archive artifacts only if build is successful
\ No newline at end of file
diff --git a/core/src/main/resources/hudson/tasks/Messages.properties b/core/src/main/resources/hudson/tasks/Messages.properties
index f89083131fd7472a69cb0463a869dbbb6a9f2d99..2a4e01259fe6f9d3d5d2297019f1250e1d69788a 100644
--- a/core/src/main/resources/hudson/tasks/Messages.properties
+++ b/core/src/main/resources/hudson/tasks/Messages.properties
@@ -31,6 +31,7 @@ Ant.ProjectConfigNeeded= Maybe you need to configure the job to choose one of yo
ArtifactArchiver.ARCHIVING_ARTIFACTS=Archiving artifacts
ArtifactArchiver.DeletingOld=Deleting old artifacts from {0}
ArtifactArchiver.DisplayName=Archive the artifacts
+ArtifactArchiver.SkipBecauseOnlyIfSuccessful=Skipped archiving because build is not successful
ArtifactArchiver.FailedToArchive=Failed to archive artifacts: {0}
ArtifactArchiver.NoIncludes=\
No artifacts are configured for archiving.\n\
diff --git a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java
index f5d7f4ac41188e827f246e2ccaa74074684b1691..dd4cf1431fdd1999e15842e14f6f59c952f4bbae 100644
--- a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java
+++ b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java
@@ -24,6 +24,7 @@
package hudson.tasks;
+import hudson.AbortException;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
@@ -253,4 +254,25 @@ public class ArtifactArchiverTest {
}
}
+ static class CreateArtifactAndFail extends TestBuilder {
+ public boolean perform(AbstractBuild,?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
+ build.getWorkspace().child("f").write("content", "UTF-8");
+ throw new AbortException("failing the build");
+ }
+ }
+
+ @Test
+ @Bug(22698)
+ public void testArchivingSkippedWhenOnlyIfSuccessfulChecked() throws Exception {
+ FreeStyleProject project = j.createFreeStyleProject();
+ project.getPublishersList().replaceBy(Collections.singleton(new ArtifactArchiver("f", "", false, false, false)));
+ project.getBuildersList().replaceBy(Collections.singleton(new CreateArtifactAndFail()));
+ assertEquals(Result.FAILURE, build(project));
+ assertTrue(project.getBuildByNumber(1).getHasArtifacts());
+ project.getPublishersList().replaceBy(Collections.singleton(new ArtifactArchiver("f", "", false, false, true)));
+ assertEquals(Result.FAILURE, build(project));
+ assertTrue(project.getBuildByNumber(1).getHasArtifacts());
+ assertFalse(project.getBuildByNumber(2).getHasArtifacts());
+ }
+
}