Reputation: 121
Is there a plugin to fail a build if a pattern occurs on the console output?
For example:
Build Action - success
Build Action - success
Build Action - error_pattern
Build Action - success
Let's assume that the Jenkins build process does not fail the build on error_pattern, I need to have an external fail trigger of some sort.
Looking for a solution to fail the build during, not a post build task.
Upvotes: 12
Views: 19150
Reputation: 143
I've used this answer as the basis of a pipeline script. In the "Build" stage I have two parallel sub-stages - one of them is doing the actual build and outputting to a log, whilst the other sub-stage is grepping the same log. I've negated the exit code (! grep
) so that it'll error when the "ERROR:" string is found. Thanks to the failFast
setting, this will cause the whole "Build" stage to fail as soon as the string is found. There's an extra grep
at the end of the first sub-stage, in case an error was produced right at the end.
I'm tracking the build status with a variable (BUILD_COMPLETE
).
pipeline {
agent {
label 'master'
}
environment {
BUILD_COMPLETE = false
}
stages {
stage('Build') {
failFast true
parallel {
stage('Building') {
steps {
sh 'echo foo | tee output.log'
sleep 5
sh 'echo bar | tee -a output.log'
sleep 5
sh 'echo ERROR: baz | tee -a output.log'
sleep 5
sh 'echo qux | tee -a output.log'
sleep 5
sh '! grep "^ERROR:" output.log'
script {
BUILD_COMPLETE = true
}
}
}
stage('Monitoring the logs') {
steps {
script {
while (BUILD_COMPLETE != true) {
sh '! grep "^ERROR:" output.log'
sleep 1
}
}
}
}
}
}
}
}
Upvotes: 1
Reputation: 1035
As an ugly workaround I do the following: in the build script redirect all the output to some resulting .log file, then you can grep through this file in the background the way you like (personally I do the freezing check additionally - calculate the checksum and compare with previous, if the same - start counting for timeout until threshold), etc...
Disadvantage is the output goes to some file instead of Jenkins console, but I guess you can do both using tee
(I don't care, because my goal is to archive the log anyways and send it via email, - so I just gzip
my resulting .log file and attach it as an artifact to the build record + to the email).
Advantage is you have full control on what happens in the build output and can interrupt the build using your own return code / message.
Upvotes: 1
Reputation: 5431
You should try the Post Build Task plugin. You can search for a pattern and then launch a script.
edit: There is also Text finder plugin, looks better for your problem
Upvotes: 9