Jenkins pipeline - How to read the success status of build?

本秂侑毒 提交于 2019-12-25 00:13:29

问题


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" approach did not help me.

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


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


回答1:


Use this to grab the exit status of the command:

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

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"'

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

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

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



来源:https://stackoverflow.com/questions/56960876/jenkins-pipeline-how-to-read-the-success-status-of-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!