Reputation: 21
I need to extract the part of the jenkins log that contains just the error description and stack trace so that I can send it in an email (which I do using the jenkins email ext plugin in the pipeline).
For e.g., in the following log snippet, I would need all lines after "org.jenkinsci.plugins.scriptsecurity ..."
... Sending build end EMAIL [Pipeline] echo Build Number: 218 [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node Scripts not permitted to use method hudson.model.Actionable getAction java.lang.Class. Administrators can decide whether to approve or reject this signature. [Pipeline] End of Pipeline org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.model.Actionable getAction java.lang.Class at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:175) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:137) at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159) at org.kohsuke.groovy.sandbox.impl.Checker$checkedCall$0.callStatic(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194) at WorkflowScript.sendBuildEmail(WorkflowScript:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157) at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:133) at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159) at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:124) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17) at WorkflowScript.run(WorkflowScript:131) at ...
How can I achieve this? I'll appreciate any help
This is my pipeline structure:
pipeline { agent {...} environment {...} stages {...} post {...} }
Upvotes: 2
Views: 3755
Reputation: 1283
A way to do this is to wrap your whole pipeline script in a try
/catch
block and then sending an email using the mail
step in the catch
block. Here's how I would write it:
try {
// your pipeline code
// ...
} catch (e) {
mail subject: "${env.JOB_NAME}#${env.BUILD_NUMBER} - Failed",
body: """
Build: ${env.BUILD_URL}
Error message:
${e.getMessage()}
Stack Trace:
${e.getStackTrace().join('\n')}
""",
to: '[email protected]'
throw e // rethrow the error so that it gets printed in the job log, and so the job fails
}
You will definitely need to approve getStackTrace
as a "secure" method and you might have to do the same for getMessage
.
Something to keep in mind is that this try/catch will not catch 'No such DSL method ' errors, which seems to be not a bug according to JENKINS-45469.
If you're interested in more complicated email behavior, you can alo checkout the extended email plugin.
Upvotes: 0