提交 6df89e33 编写于 作者: D Daniel Beck

[FIXED JENKINS-22699] Add option to archive only if successful

上级 dcfeaf3c
...@@ -74,16 +74,26 @@ public class ArtifactArchiver extends Recorder { ...@@ -74,16 +74,26 @@ public class ArtifactArchiver extends Recorder {
@Nonnull @Nonnull
private Boolean allowEmptyArchive; 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) { 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) { 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.artifacts = artifacts.trim();
this.excludes = Util.fixEmptyAndTrim(excludes); this.excludes = Util.fixEmptyAndTrim(excludes);
this.latestOnly = latestOnly; this.latestOnly = latestOnly;
this.allowEmptyArchive = allowEmptyArchive; this.allowEmptyArchive = allowEmptyArchive;
this.onlyIfSuccessful = onlyIfSuccessful;
} }
// Backwards compatibility for older builds // Backwards compatibility for older builds
...@@ -106,6 +116,10 @@ public class ArtifactArchiver extends Recorder { ...@@ -106,6 +116,10 @@ public class ArtifactArchiver extends Recorder {
return latestOnly; return latestOnly;
} }
public boolean isOnlyIfSuccessful() {
return onlyIfSuccessful;
}
public boolean getAllowEmptyArchive() { public boolean getAllowEmptyArchive() {
return allowEmptyArchive; return allowEmptyArchive;
} }
...@@ -126,6 +140,11 @@ public class ArtifactArchiver extends Recorder { ...@@ -126,6 +140,11 @@ public class ArtifactArchiver extends Recorder {
return true; return true;
} }
if (build.getResult().isWorseThan(Result.UNSTABLE) && onlyIfSuccessful) {
listener.getLogger().println(Messages.ArtifactArchiver_SkipBecauseOnlyIfSuccessful());
return true;
}
listener.getLogger().println(Messages.ArtifactArchiver_ARCHIVING_ARTIFACTS()); listener.getLogger().println(Messages.ArtifactArchiver_ARCHIVING_ARTIFACTS());
try { try {
FilePath ws = build.getWorkspace(); FilePath ws = build.getWorkspace();
......
...@@ -37,5 +37,8 @@ THE SOFTWARE. ...@@ -37,5 +37,8 @@ THE SOFTWARE.
<f:entry field="allowEmptyArchive" > <f:entry field="allowEmptyArchive" >
<f:checkbox title="${%allowEmptyArchive}"/> <f:checkbox title="${%allowEmptyArchive}"/>
</f:entry> </f:entry>
<f:entry field="onlyIfSuccessful" >
<f:checkbox title="${%onlyIfSuccessful}"/>
</f:entry>
</f:advanced> </f:advanced>
</j:jelly> </j:jelly>
\ No newline at end of file
...@@ -21,4 +21,5 @@ ...@@ -21,4 +21,5 @@
# THE SOFTWARE. # THE SOFTWARE.
lastBuildOnly=Discard all but the last successful/stable artifact to save disk space lastBuildOnly=Discard all but the last successful/stable artifact to save disk space
allowEmptyArchive=Do not fail build if archiving returns nothing allowEmptyArchive=Do not fail build if archiving returns nothing
\ No newline at end of file onlyIfSuccessful=Archive artifacts only if build is successful
\ No newline at end of file
...@@ -31,6 +31,7 @@ Ant.ProjectConfigNeeded= Maybe you need to configure the job to choose one of yo ...@@ -31,6 +31,7 @@ Ant.ProjectConfigNeeded= Maybe you need to configure the job to choose one of yo
ArtifactArchiver.ARCHIVING_ARTIFACTS=Archiving artifacts ArtifactArchiver.ARCHIVING_ARTIFACTS=Archiving artifacts
ArtifactArchiver.DeletingOld=Deleting old artifacts from {0} ArtifactArchiver.DeletingOld=Deleting old artifacts from {0}
ArtifactArchiver.DisplayName=Archive the artifacts ArtifactArchiver.DisplayName=Archive the artifacts
ArtifactArchiver.SkipBecauseOnlyIfSuccessful=Skipped archiving because build is not successful
ArtifactArchiver.FailedToArchive=Failed to archive artifacts: {0} ArtifactArchiver.FailedToArchive=Failed to archive artifacts: {0}
ArtifactArchiver.NoIncludes=\ ArtifactArchiver.NoIncludes=\
No artifacts are configured for archiving.\n\ No artifacts are configured for archiving.\n\
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
package hudson.tasks; package hudson.tasks;
import hudson.AbortException;
import hudson.FilePath; import hudson.FilePath;
import hudson.Launcher; import hudson.Launcher;
import hudson.model.AbstractBuild; import hudson.model.AbstractBuild;
...@@ -253,4 +254,25 @@ public class ArtifactArchiverTest { ...@@ -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());
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册