From ef378cc38d5b0c0e0065a64fcbd4faf8c5b95feb Mon Sep 17 00:00:00 2001 From: Markus Winter Date: Thu, 1 Feb 2024 08:56:48 +0100 Subject: [PATCH] [JENKINS-71700] avoid stacktrace from artifactarchiver when no artifacts are found (#8908) [JENKINS-71700] avoid stacktrace in artifactarchiver when no artifacts When the validateAntFileMask fails because there are too many files or it takes too long an exception is thrown that was intendend to be catched in the ArtifactArchiver. But when the build happens on an agent and not the built-in, this didn't work as the the exception is deeply wrapped in 2 other exceptions. As printing this information is of no real help, just catch all exceptions and print them to the jenkins log but not the build log. Any other message might just be misleading as one could get the impression that jenkins stopped after looking at 10000 files and only because of this no files where archived but this is not the case. At this place the ArtifactArchiver is just trying to give a hint why it didn't archive anything. --- .../java/hudson/tasks/ArtifactArchiver.java | 5 +-- .../hudson/tasks/ArtifactArchiverTest.java | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hudson/tasks/ArtifactArchiver.java b/core/src/main/java/hudson/tasks/ArtifactArchiver.java index bd4d432fac..02137ad369 100644 --- a/core/src/main/java/hudson/tasks/ArtifactArchiver.java +++ b/core/src/main/java/hudson/tasks/ArtifactArchiver.java @@ -31,7 +31,6 @@ import hudson.AbortException; import hudson.EnvVars; import hudson.Extension; import hudson.FilePath; -import hudson.Functions; import hudson.Launcher; import hudson.Util; import hudson.model.AbstractBuild; @@ -272,10 +271,8 @@ public class ArtifactArchiver extends Recorder implements SimpleBuildStep { if (msg != null) { listener.getLogger().println(msg); } - } catch (FilePath.FileMaskNoMatchesFoundException e) { - listener.getLogger().println(e.getMessage()); } catch (Exception e) { - Functions.printStackTrace(e, listener.getLogger()); + LOG.log(Level.FINE, e, () -> "Failed to validate ant file mask."); } if (allowEmptyArchive) { listener.getLogger().println(Messages.ArtifactArchiver_NoMatchFound(artifacts)); diff --git a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java index 92dee288b4..2157e89492 100644 --- a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java +++ b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java @@ -46,6 +46,7 @@ import hudson.model.FreeStyleProject; import hudson.model.Label; import hudson.model.Result; import hudson.model.Run; +import hudson.model.Slave; import hudson.remoting.VirtualChannel; import hudson.slaves.DumbSlave; import java.io.File; @@ -134,8 +135,48 @@ public class ArtifactArchiverTest { @Test @Issue("JENKINS-51913") public void testFileMaskNoMatchesFoundException() throws Exception { + hudson.FilePath.VALIDATE_ANT_FILE_MASK_BOUND = 1; FreeStyleProject project = j.createFreeStyleProject(); - String pattern = "dir/**"; + project.getBuildersList().replaceBy(Collections.singleton(new TestBuilder() { + @Override + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { + FilePath dir = build.getWorkspace().child("dir"); + dir.child("file").write("content", "UTF-8"); + dir.child("file2").write("content", "UTF-8"); + dir.child("file3").write("content", "UTF-8"); + return true; + } + })); + + String pattern = "dir/*.log"; + ArtifactArchiver aa = new ArtifactArchiver(pattern); + aa.setAllowEmptyArchive(true); + project.getPublishersList().replaceBy(Collections.singleton(aa)); + FreeStyleBuild build = j.buildAndAssertSuccess(project); + assertFalse(project.getBuildByNumber(1).getHasArtifacts()); + j.assertLogContains("No artifacts found that match the file pattern \"" + pattern + "\"", build); + assertThat("No stacktrace shown", build.getLog(31), Matchers.iterableWithSize(lessThan(30))); + } + + @Test + @Issue("JENKINS-71700") + public void testFileMaskNoMatchesFoundExceptionOnAgent() throws Exception { + Slave agent = j.createOnlineSlave(); + hudson.FilePath.VALIDATE_ANT_FILE_MASK_BOUND = 1; + FreeStyleProject project = j.createFreeStyleProject(); + project.setAssignedNode(agent); + project.getBuildersList().replaceBy(Collections.singleton(new TestBuilder() { + @Override + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { + FilePath dir = build.getWorkspace().child("dir"); + dir.child("file").write("content", "UTF-8"); + dir.child("file2").write("content", "UTF-8"); + dir.child("file3").write("content", "UTF-8"); + return true; + } + })); + + String pattern = "dir/*.log"; ArtifactArchiver aa = new ArtifactArchiver(pattern); aa.setAllowEmptyArchive(true); project.getPublishersList().replaceBy(Collections.singleton(aa));