我需要提取jenkins日志中仅包含错误描述和堆栈跟踪的部分,以便我可以通过电子邮件发送它(我在管道中使用jenkins email ext插件来完成此操作)。
例如,在以下日志片段中,我需要在“ 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 ...
我该如何实现?我将不胜感激
这是我的管道结构:
pipeline { agent {...} environment {...} stages {...} post {...} }
答案 0 :(得分:0)
一种方法是将整个管道脚本包装在try
/ catch
块中,然后使用catch
块中的mail
step发送电子邮件。这是我的写法:
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: 'me@email.com'
throw e // rethrow the error so that it gets printed in the job log, and so the job fails
}
您肯定需要批准getStackTrace
作为“安全”方法,并且您可能必须对getMessage
做同样的事情。
要记住的一点是,此try / catch不会捕获“无此类DSL方法”错误,根据JENKINS-45469,这似乎不是错误。
如果您对更复杂的电子邮件行为感兴趣,可以退出extended email plugin。