Jenkins管道-如何读取构建的成功状态? [英] Jenkins pipeline - How to read the success status of build?

查看:714
本文介绍了Jenkins管道-如何读取构建的成功状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是运行构建后的输出(成功):

Below is the output after running the build(with success):

$ sam build
2019-06-02 15:36:37 Building resource 'SomeFunction'
2019-06-02 15:36:37 Running PythonPipBuilder:ResolveDependencies
2019-06-02 15:36:39 Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Package: sam package --s3-bucket <yourbucket>


[command] && echo "Yes"方法没有帮助我.


[command] && echo "Yes" approach did not help me.

我试图在Jenkins管道中使用它

I tried to use this in Jenkins pipeline

def samAppBuildStatus =  sh(script: '[cd sam-app-folder; sam build  | grep 'Succeeded' ] && echo true', returnStatus: true) as Boolean

作为单行脚本命令,但不起作用

as one-liner script command, but does not work

如何使用bash脚本获取成功构建状态?詹金斯管道

How to grab the success build status using bash script? for Jenkins pipeline

推荐答案

使用它来获取命令的退出状态:

Use this to grab the exit status of the command:

def samAppBuildStatus = sh returnStatus: true, script: 'cd sam-app-folder; sam build | grep "Succeeded"'

,或者如果您不想在输出中看到任何stderr,则此:

or this if you don't want to see any stderr in the output:

def samAppBuildStatus = sh returnStatus: true, script: 'cd sam-app-folder; sam build 2>&1 | grep "Succeeded"'

然后,在您的Jenkinsfile中,您可以执行以下操作:

then later in your Jenkinsfile you can do something like this:

if (!samAppBuildStatus){
    echo "build success [$samAppBuildStatus]"
} else {
    echo "build failed [$samAppBuildStatus]"
}

!的原因是因为shell和groovy之间的truefalse的定义不同(shell的0true).

The reason for the ! is because the definitions of true and false between shell and groovy differ (0 is true for shell).

这篇关于Jenkins管道-如何读取构建的成功状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆