Reputation: 3251
Unable to get output of Failing Jenkins jobs to pipeline's console
I'm calling several jobs from my pipeline. Below is an example job 'printuser' that gets triggered from my pipeline.
I'm able to get the output of the job on my pipeline console which is my requirement.
pipeline {
agent none
stages {
stage ("Check Parameters"){
steps {
echo "In pipeline"
script {
echo "Start condition check"
}
def slaveJob = build job: 'printuser'
println slaveJob.rawBuild.log
}
}
}
}
The issue happens when 'printuser' fails in which case the failing output does not show in the pipeline console page.
I tried adding the println to the post failure block like below:
post {
failure {
script {
println slaveJob.rawBuild.log
}
}
}
But, I get the below error in doing so.
Error when executing failure post condition:
groovy.lang.MissingPropertyException: No such property: slaveJob for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
Can you please suggest how can I get the output of "FAILING" jobs to the pipeline console ?
Upvotes: 0
Views: 1447
Reputation: 1369
On your post block you can print the whole job build log.
post {
failure {
echo Jenkins.getInstance().getItemByFullName('printuser').getLastBuild().logFile.text
}
}
Note that you should approve specific signatures to allow pipeline and groovy to access the above functions. If you haven't done this yet, the above script will fail with
Start condition check Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance. Administrators can decide whether to approve or reject this signature.
You can follow the error message by clicking on the link of the message which will redirect you to the scriptApproval page of Jenkins where you can add the signature that needs approval to the approval list.
You should have the following list of approved signatures for the above to work
method hudson.model.ItemGroup getItem java.lang.String
method hudson.model.Job getLastBuild
method hudson.model.Run getLogFile
method jenkins.model.Jenkins getItemByFullName java.lang.String
staticMethod jenkins.model.Jenkins getInstance
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getText java.io.File
If you follow this solution, you can change the post's failure action to always so it print the log always (not only on errors) and remove the println slaveJob.rawBuild.log
Upvotes: 1
Reputation: 1334
When using the def
keyword a variable has block scope. Try slaveJob = build job: 'printuser'
without the def
.
Upvotes: 0