我设置了Jenkins脚本化管道,在其中执行了许多Maven构建。如果根本原因是已知原因,我想将其中之一视为非致命。
我试图通过检查Exception的消息来实现这一点,例如
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构建失败的原因?
答案 0 :(得分:2)
您可以获取命令输出,然后解析容器特定的消息。
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'
}
答案 1 :(得分:0)
一种选择是使用来检查最后的日志行
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方法。管理员可以决定是批准还是拒绝此签名。