获取Jenkins管道内Maven构建失败的原因
我有一个Jenkins脚本化的管道,在那里我执行许多Maven构建。如果根本原因是已知的,我想将其中一个视为非致命性 我试图通过检查异常的消息来实现这一点,例如获取Jenkins管道内Maven构建失败的原因,maven,jenkins,jenkins-pipeline,Maven,Jenkins,Jenkins Pipeline,我有一个Jenkins脚本化的管道,在那里我执行许多Maven构建。如果根本原因是已知的,我想将其中一个视为非致命性 我试图通过检查异常的消息来实现这一点,例如 try { sh "mvn -U clean verify sonar:sonar ${sonarcloudParams}" } catch ( Exception e ) { if ( e.getMessage().contains("not authorized to run analysis")) {
try {
sh "mvn -U clean verify sonar:sonar ${sonarcloudParams}"
} catch ( Exception e ) {
if ( e.getMessage().contains("not authorized to run analysis")) {
echo "Marking build unstable due to missing SonarCloud onboarding. See https://cwiki.apache.org/confluence/display/SLING/SonarCloud+analysis for steps to fix."
currentBuild.result = 'UNSTABLE'
}
}
问题在于异常的消息不是来自Maven的消息,而是“脚本返回退出代码1”
e.getCause()
中没有进一步的信息
如何在脚本化管道中访问Maven构建失败的原因?您可以获得命令输出,然后解析特定消息
def output = sh(
script: "mvn -U clean verify sonar:sonar ${sonarcloudParams}",
returnStdout: true
).trim()
echo "mvn cmd output: ${output}"
if(output.contains('not authorized to run analysis')) {
currentBuild.result = 'UNSTABLE'
}
// parse jenkins job build log
def logUrl = env.BUILD_URL + 'consoleText'
def cmd = "curl -u \${JENKINS_AUTH} -k ${logUrl} | tail -n 50"
def output = sh(returnStdout: true, script: cmd).trim()
echo "job build log: ${output}"
if(output.contains('not authorized to run analysis')) {
currentBuild.result = 'UNSTABLE'
}
一个选项是使用
def sonarCloudNotEnabled = currentBuild.rawBuild.getLog(50).find {
line -> line.contains("not authorized to run analysis")
}
但是,这在默认情况下不起作用。在Jenkins的例子中,我用它出错了
不允许脚本使用方法org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild。管理员可以决定是否批准或拒绝此签名
有趣。只是一个注释,但我首先要将“关键”(即
sonar:sonar
)部分从其余部分分离为单独的Maven调用。然后我将读取错误流,并在那里查找消息。@JFMeier-我现在正试图访问生成日志,但是currentBuild.rawBuild.getLog(x)
在我的Jenkins实例上不起作用,因为脚本不允许使用方法org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild。
我尝试了这个方法,但没有成功。我假设这是因为我使用的是Pipeline Maven插件(),它使用Maven事件间谍来输出日志。请检查目录中的内容output@yong-它是空的,我刚刚确认。问题是mvn
执行会抛出异常,因此输出
变量永远不会填充。谢谢@yong。我认为这是一个可行的解决方案,但我不喜欢用Jenkins作业触发对Jenkins本身的网络请求。异常将打印到作业生成日志?@yong-是的,它将打印到生成日志。请参阅我的答案,我添加了代码示例来获取和解析生成日志