Jenkins Pipeline log management transition to Declarative post blocks
0 reputation · 25 Nov 2020, 17:04 UTC
0 reputation · 25 Nov 2020, 17:04 UTC
Transitioning from Freestyle projects to Declarative Pipelines changes how failure diagnostics are captured. While Freestyle projects rely on the general console output, Declarative Pipelines introduce the post block to handle specific build outcomes.
A primary constraint is the storage of console output as flat text files on the controller. When implementing automated log archiving within post { failure { ... } } blocks to prevent data loss during rotation, there is a risk of saturating controller disk space if verbosity is high across numerous concurrent builds.
Given the lack of a native indexed search for historical logs across multiple builds, the strategy for archiving these diagnostics must balance visibility with resource consumption.
31775 reputation · 25 Nov 2020, 22:27 UTC
Answer the question first: use a post { failure { ... } } block to copy the build log to a workspace file, filter or trim it, then publish the filtered file as an artifact to an external repository (e.g., Artifactory, S3, Nexus). This keeps the controller’s disk usage low because only the reduced artifact is stored long‑term, while the full console log remains subject to the controller’s built‑in log rotation.
post { failure { ... } } block, capture the raw log to a temporary file in the workspace:post {
failure {
script {
def logFile = "failure-log.txt"
// Copy the current build's log to the workspace
writeFile file: logFile, text: currentBuild.rawBuild.getLog(0, currentBuild.rawBuild.getLogSize())
// Optional: filter to only ERROR/WARN lines or limit line count
sh "grep -i -E 'error|warn' ${logFile} > ${logFile}.filtered && mv ${logFile}.filtered ${logFile}
// Archive the filtered log as an artifact
archiveArtifacts artifacts: logFile, fingerprint: true
}
}
}
The Pipeline Stage View plugin correlates log output with stages by using the timestamps and stage IDs emitted during pipeline execution (flowNodes). When logs are stored as external artifacts, the plugin can still display stage‑specific sections if the artifact contains stage‑level markers or timestamps that align with those flowNodes.
[STAGE_NAME] before archiving. Stage View reads these markers and shows the corresponding segment under the correct stage.timestamps step is used, the plugin can match timestamp ranges in the artifact to the start/end times of each stage.Therefore, to retain stage granularity while archiving externally, include either stage markers or ensure the timestamps step is active before copying the log.
Because the research notes that current verification is required, perform the following checks in a non‑production controller:
post { failure { ... } } block as shown above.failure-log.txt after the build completes (check with ls -lh workspace/).$JENKINS_HOME/jobs/<job>/builds/<num>/log.Note: Verify that your external artifact storage has appropriate retention and that network bandwidth usage stays within limits; adjust the filter (e.g., line count or keyword list) based on observed log sizes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.