What would be a Groovy or pipeline step to parse Jenkins console output, identify a string and mark the build as failure if the string is matched?
2 Answers
This question seems similar to another on StackOverflow, but seems to be a close, but not exact duplicate of https://stackoverflow.com/questions/37018509/jenkinsfile-build-log
To quote those :
if you just want to check, that your log contains string myTestString you can just call manager.logContains('.myTestString.')
If you want to get some information from the first matching line you can use manager.getLogMatcher(regexp)
You could use the Groovy flow control to break the build - you just need a step to execute the test:
Following the example on the link :
node {
stage('CheckLog') {
steps {
if (manager.logContains('.*myTestString.*')) {
error("Build failed because of this and that..")
}
}
}
- 3,783
- 4
- 20
- 41
I solved this problem by following this answer on StackOverflow.
You can use currentBuild.rawBuild.getLog(10) (the 10 specifies that you want the last 10 lines) to get the last few lines of the log stream.
According to this documentation, this method returns a List of Strings (List<String>) where each index contains a single line of the log stream.
You can either search line-by-line for what you want, or you can combine the entire List into a single String using the .join() method.
To search the actual String, you can use the .contains() method or you can use a RegExp
- 111
- 2