I want to add a Build step with the Groovy plugin to read a file and trigger a build fail depending on the content of the file.
How can I inject the workspace file path in the groovy plugin ?
myFileDirectory = // Get workspace filepath here ???
myFileName = "output.log"
myFile = new File(myFileDirectory + myFileName)
lastLine = myFile.readLines().get(myFile.readLines().size().toInteger() - 1)
if (lastLine ==~ /.Fatal Error.*/ ){
println "Fatal error found"
System.exit(1)
} else{
println "nothing to see here"
}
Based on your comments, you would be better off with Text-finder plugin.
It allows to search file(s), as well as console, for a regular expression and then set the build either unstable
or failed
if found.
As for the Groovy, you can use the following to access ${WORKSPACE}
environment variable:
def workspace = manager.build.getEnvVars()["WORKSPACE"]
Note that in "Execute system Groovy script" step you can use build.getEnvVars()
directly (also in sandboxed script). For example:
def workspace = build.getEnvVars()["WORKSPACE"]
def myFilePath = "my-logs/script-output.log"
def myFile = new File("${workspace}/${myFilePath}")
def newContent = "<h2>Script log</h2> <pre>${myFile.text}</pre>"
def existingDescription = build.getDescription() ?: ""
build.setDescription(existingDescription + newContent)
The build
variable should be available without any imports in a system Groovy script.