我设置了一个 Jenkins 脚本管道,我在其中执行多个 Maven 构建。如果根本原因是已知的,我想将其中之一视为非致命的。
我试图通过检查异常的消息来实现这一点,例如
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'
}
关于maven - 获取 Jenkins 管道内 Maven 构建失败的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55742773/