From 6df89e3340c2e31a67b83384f5333ee23bd7fcc8 Mon Sep 17 00:00:00 2001 From: Daniel Beck Date: Mon, 21 Apr 2014 19:43:18 +0200 Subject: [PATCH] [FIXED JENKINS-22699] Add option to archive only if successful --- .../java/hudson/tasks/ArtifactArchiver.java | 23 +++++++++++++++++-- .../tasks/ArtifactArchiver/config.jelly | 3 +++ .../tasks/ArtifactArchiver/config.properties | 3 ++- .../hudson/tasks/Messages.properties | 1 + .../hudson/tasks/ArtifactArchiverTest.java | 22 ++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/hudson/tasks/ArtifactArchiver.java b/core/src/main/java/hudson/tasks/ArtifactArchiver.java index 7faa7b8986..552c173473 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 659b36011d..d3e6b62690 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 3a6afefe2c..9a50e12c18 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 f89083131f..2a4e01259f 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 f5d7f4ac41..dd4cf1431f 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()); + } + } -- GitLab