Skip to content

Commit

Permalink
[JENKINS-43491] Feature flag ".archive-jenkins-maven-event-spy-logs" …
Browse files Browse the repository at this point in the history
…to archive the logs file generated by the Jenkins Maven Event Spy. Primarily intended for troubleshooting purpose.

The ".archive-jenkins-maven-event-spy-logs" has to be created in the Jenkins build workspace.
  • Loading branch information
Cyrille Le Clerc committed Apr 11, 2017
1 parent 0d33ab5 commit eea8e9e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Expand Up @@ -32,6 +32,7 @@
import org.jenkinsci.plugins.pipeline.maven.reporters.FindbugsAnalysisReporter;
import org.jenkinsci.plugins.pipeline.maven.reporters.GeneratedArtifactsReporter;
import org.jenkinsci.plugins.pipeline.maven.reporters.JunitTestsReporter;
import org.jenkinsci.plugins.pipeline.maven.reporters.JenkinsMavenEventSpyLogsReporter;
import org.jenkinsci.plugins.pipeline.maven.reporters.TasksScannerReporter;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -84,6 +85,12 @@ public void processMavenSpyLogs(StepContext context, FilePath mavenSpyLogFolder)
throw new IllegalStateException("InputStream for " + mavenSpyLogs.getRemote() + " is null");
}

FilePath archiveJenkinsMavenEventSpyLogs = workspace.child(".archive-jenkins-maven-event-spy-logs");
if (archiveJenkinsMavenEventSpyLogs.exists()) {
LOGGER.log(Level.FINE, "Archive Jenkins Maven Event Spy logs {0}", mavenSpyLogs.getRemote());
new JenkinsMavenEventSpyLogsReporter().process(context, mavenSpyLogs);
}

Element mavenSpyLogsElt = documentBuilder.parse(mavenSpyLogsInputStream).getDocumentElement();

FilePath skipArchiveArtifactsFile = workspace.child(".skip-archive-generated-artifacts");
Expand Down
@@ -0,0 +1,50 @@
package org.jenkinsci.plugins.pipeline.maven.reporters;

import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.ArtifactManager;
import jenkins.util.BuildListenerAdapter;
import org.jenkinsci.plugins.workflow.steps.StepContext;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Map;

import javax.annotation.Nonnull;

/**
* Primarily for debugging purpose, archive the Maven build logs
*
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class JenkinsMavenEventSpyLogsReporter {
public void process(@Nonnull StepContext context, @Nonnull FilePath mavenSpyLogs) throws IOException, InterruptedException {

Run run = context.get(Run.class);
ArtifactManager artifactManager = run.pickArtifactManager();
Launcher launcher = context.get(Launcher.class);
TaskListener listener = context.get(TaskListener.class);
FilePath workspace = context.get(FilePath.class);

// ARCHIVE MAVEN BUILD LOGS
FilePath tmpFile = new FilePath(workspace, "." + mavenSpyLogs.getName());
try {
mavenSpyLogs.copyTo(tmpFile);
listener.getLogger().println("[withMaven] archive " + mavenSpyLogs.getRemote() + " as " + mavenSpyLogs.getName());
// filePathInArchiveZone -> filePathInWorkspace
Map<String, String> mavenBuildLogs = Collections.singletonMap(mavenSpyLogs.getName(), tmpFile.getName());
artifactManager.archive(workspace, launcher, new BuildListenerAdapter(listener), mavenBuildLogs);
} catch (Exception e) {
PrintWriter errorWriter = listener.error("[withMaven] WARNING Exception archiving Maven build logs " + mavenSpyLogs + ", skip file. ");
e.printStackTrace(errorWriter);
} finally {
boolean deleted = tmpFile.delete();
if (!deleted) {
listener.error("[withMaven] WARNING Failure to delete temporary file " + tmpFile);
}
}
}
}

0 comments on commit eea8e9e

Please sign in to comment.