Keywords: Jenkins | Groovy Script | File Reading | Build Control | Text-finder Plugin
Abstract: This paper provides an in-depth exploration of multiple methods for reading files from the workspace in Jenkins using Groovy scripts, with a focus on the application scenarios of the Text-finder plugin and Groovy environment variable access techniques. Through detailed code examples and comparative analysis, it explains how to dynamically control build status based on file content, offering reliable technical solutions for continuous integration processes. The article covers comparisons between traditional plugin methods and modern Pipeline approaches, helping developers choose the most suitable implementation for their project needs.
Fundamentals of Workspace File Access
In the Jenkins continuous integration environment, accessing workspace files is a critical aspect of automated builds. Traditional Groovy scripts obtain the workspace path through environment variables, with core code as follows:
def workspace = manager.build.getEnvVars()["WORKSPACE"]
myFileName = "output.log"
myFile = new File(workspace + "/" + myFileName)This method utilizes Jenkins' built-in manager object to access environment variables, ensuring path accuracy. It is important to include directory separators when concatenating file paths to avoid path errors.
Deep Application of Text-finder Plugin
For specific requirements involving file content detection and build status control, the Text-finder plugin offers a more professional solution. This plugin supports regular expression matching and can automatically set the build status to unstable or failed:
// Plugin configuration example:
// File path: ${WORKSPACE}/output.log
// Regular expression: .*Fatal Error.*
// Build status: FailedCompared to custom Groovy scripts, the Text-finder plugin offers advantages in simple configuration and low maintenance costs, making it particularly suitable for standard file detection scenarios.
File Reading in Pipeline Environment
Modern Jenkins Pipeline provides a more concise method for file reading:
def content = readFile 'output.log'
// Or using the full path:
def content = readFile "${env.WORKSPACE}/output.log"The readFile step is specifically designed for Pipeline, automatically handling workspace paths, resulting in cleaner and more readable code.
Build Status Control Strategies
When controlling build status based on file content, comprehensive error handling must be considered:
try {
def lines = myFile.readLines()
if (!lines.isEmpty()) {
def lastLine = lines.last()
if (lastLine ==~ /.*Fatal Error.*/) {
println "Fatal error found"
// Use error step in Pipeline:
error("Build terminated due to fatal error")
}
}
} catch (FileNotFoundException e) {
println "File not found: ${e.message}"
}Complete exception handling ensures the stability of the build process, preventing unexpected interruptions due to file access issues.
Comparative Analysis of Technical Solutions
Different solutions are suitable for different scenarios: the Text-finder plugin is ideal for simple text matching needs; traditional Groovy scripts offer maximum flexibility; Pipeline's readFile step represents the most modern solution. Developers should choose the appropriate method based on specific project requirements and technology stack.