Jenkins pipeline - How to read the success status

2019-08-18 23:32发布

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条回答
We Are One
2楼-- · 2019-08-19 00:05

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).

查看更多
登录 后发表回答