Keywords: Jenkins | Console Output | Log Filesystem | Post-build Processing | Continuous Integration
Abstract: This technical paper provides an in-depth analysis of Jenkins console output log locations in the filesystem and various access methods. It covers both direct filesystem access through $JENKINS_HOME directories and URL-based access via ${BUILD_URL}/consoleText, with detailed code examples for Linux, Windows, and MacOS platforms. The paper compares different approaches and provides best practices for efficient console log processing in Jenkins build pipelines.
Overview of Jenkins Console Output Logs
In continuous integration and deployment workflows, Jenkins serves as a widely adopted automation server where console output logs from build tasks contain crucial information about the build process. These logs are essential for debugging build issues, analyzing build performance, and auditing build history. However, many developers face challenges when attempting to programmatically access these logs, particularly in locating and retrieving the log files accurately.
Filesystem Location of Console Output
Jenkins console output logs are primarily stored in the filesystem of the Jenkins master server, not on the build agent nodes. According to Jenkins architecture design, all build logs are centrally managed in specific directory structures on the master server. The core log file path pattern is: $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/log, where $JENKINS_HOME is the Jenkins home directory, $JOB_NAME is the job name, and $BUILD_NUMBER is the specific build number.
For common build type queries, symbolic links can be used to simplify path access. For example, to access the log of the last successful build, use the path: $JENKINS_HOME/jobs/$JOB_NAME/builds/lastSuccessfulBuild/log. Similarly, symbolic links like lastFailedBuild, lastStableBuild point to logs of builds with corresponding statuses.
The following code example demonstrates how to obtain the current build's log file path in a Jenkins Pipeline:
def logFilePath = "${env.JENKINS_HOME}/jobs/${env.JOB_NAME}/builds/${env.BUILD_NUMBER}/log"
echo "Current build log file path: ${logFilePath}"Accessing Console Output via URL
Beyond direct filesystem access, Jenkins provides URL-based access through RESTful APIs, offering greater flexibility without dependency on specific filesystem paths. The core access URL is: ${BUILD_URL}/consoleText, where ${BUILD_URL} is a Jenkins environment variable pointing to the current build's detail page.
Compared to the consoleFull URL, consoleText provides output in plain text format, making it more suitable for programmatic processing and text analysis. This approach benefits from cross-platform compatibility and the convenience of not requiring direct access to the Jenkins master server's filesystem.
Implementation in Linux Environment
In Linux environments, wget or curl commands can be used to download console output to the workspace. Here is a complete example:
#!/bin/bash
# Download console output to workspace
wget ${BUILD_URL}/consoleText -O ${WORKSPACE}/console_output.txt
# Use grep to process the log in subsequent build steps
grep "specific pattern" ${WORKSPACE}/console_output.txtFor more granular control, the curl command can be used:
curl -s ${BUILD_URL}/consoleText > ${WORKSPACE}/jenkins_console.logImplementation in Windows Environment
In Windows environments, curl command (available in PowerShell 3.0 and later) or the traditional Invoke-WebRequest cmdlet can be used via PowerShell:
# Using curl command (if available)
curl ${BUILD_URL}/consoleText -OutFile ${WORKSPACE}\console_output.txt
# Or using Invoke-WebRequest
Invoke-WebRequest -Uri ${BUILD_URL}/consoleText -OutFile ${WORKSPACE}\console_output.txtImplementation in MacOS Environment
As a Unix-like system, MacOS can use curl command similar to Linux:
curl ${BUILD_URL}/consoleText -o ${WORKSPACE}/console_output.txtAdvanced Access Methods in Jenkins Pipeline
In declarative or scripted Pipelines, Jenkins internal APIs can be used to directly access log content. This method avoids external HTTP requests, offering better performance and reliability.
The following example demonstrates how to obtain and process raw log content in a Pipeline:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "This is build log"
sh 'echo "Executing Shell command"'
}
}
stage('Process Log') {
steps {
script {
// Get raw log content
def logContent = currentBuild.rawBuild.logFile.text
// Write to workspace file
writeFile file: "${WORKSPACE}/raw_console_output.txt", text: logContent
// Process log with grep
sh """
grep "is" ${WORKSPACE}/raw_console_output.txt || true
"""
}
}
}
}
}Log Content Processing and Cleaning
Jenkins console output may contain ANSI color codes and other control characters that can interfere with text processing. Particularly when using currentBuild.rawBuild.logFile.text to obtain logs, the returned format is annotated log.
The following code shows how to clean ANSI color codes:
pipeline {
agent any
stages {
stage('Clean Log') {
steps {
script {
// Get raw log and write to file
writeFile file: "${WORKSPACE}/annotated_log.txt", text: currentBuild.rawBuild.logFile.text
// Use sed to clean ANSI color codes
sh '''
sed -ri "s/\\x1b\\[8m.*?\\x1b\\[0m//g" ${WORKSPACE}/annotated_log.txt
'''
// Now safely process the cleaned log
sh "grep 'specific pattern' ${WORKSPACE}/annotated_log.txt"
}
}
}
}
}Method Comparison and Best Practices
The two main access methods each have advantages and disadvantages, suitable for different scenarios:
Direct Filesystem Access:
- Advantages: Direct, efficient, no network requests required
- Disadvantages: Requires access to Jenkins master server filesystem, may not be feasible in distributed environments
- Suitable for: Scripts on Jenkins master server, administrator operations
URL API Access:
- Advantages: Cross-platform, no filesystem permissions needed, suitable for distributed environments
- Disadvantages: Requires network connection, potential performance overhead
- Suitable for: Log processing in build steps, cross-node access
In practical applications, it is recommended to choose the appropriate method based on specific environment and requirements. For most post-build processing scenarios, URL API access is a safer and more reliable choice.
Security Considerations and Permission Management
When accessing Jenkins console output, the following security factors should be considered:
- Ensure build jobs have permission to read their own logs
- In distributed environments, pay attention to network transmission security
- Avoid exposing sensitive information in logs, such as passwords, keys, etc.
- Regularly clean old build logs to prevent disk space exhaustion
By properly configuring Jenkins permission systems and log rotation policies, the security of log access and system stability can be ensured.