Skip to content

Commit

Permalink
[JENKINS-71700] avoid stacktrace in artifactarchiver when no artifacts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mawinter69 committed Jan 28, 2024
1 parent f640efc commit 3866618
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
5 changes: 1 addition & 4 deletions core/src/main/java/hudson/tasks/ArtifactArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
Expand Down Expand Up @@ -272,10 +271,8 @@ public void perform(Run<?, ?> build, FilePath ws, EnvVars environment, Launcher
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));
Expand Down
43 changes: 42 additions & 1 deletion test/src/test/java/hudson/tasks/ArtifactArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
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;
Expand Down Expand Up @@ -134,8 +135,48 @@ public void testAllowEmptyArchive() throws Exception {
@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));
Expand Down

0 comments on commit 3866618

Please sign in to comment.