'Jenkins pipeline stage build is green even though an error is present

I have a Jenkins Pipline with three stages: "Build" "Test" and "Deploy".

Here is the problem I have with "Build":

The build step ensures that structure of the Control-M Automation API json files are valid.

To do this, I utilize the $endpoint/build service provided by Automation API in the build step:

        stage('Build') {
            environment {
                CONTROLM_CREDS = credentials('xxx')
                ENDPOINT = 'xxx'
            }
            steps {
                sh '''
                username=$CONTROLM_CREDS_USR
                password=$CONTROLM_CREDS_PSW
                # Login
                login=$(curl -k -s -H "Content-Type: application/json" -X POST -d \\{\\"username\\":\\"$username\\",\\"password\\":\\"$password\\"\\} "$ENDPOINT/session/login" )
                token=$(echo ${login##*token\\" : \\"} | cut -d '"' -f 1)
                # Build
                curl -k -s -H "Authorization: Bearer $token" -X POST -F "definitionsFile=@ctmjobs/TestCICD.json" "$ENDPOINT/build"
                curl -k -s -H "Authorization: Bearer $token" -X POST "$ENDPOINT/session/logout"
                '''
            }
        }
<snip>

Everything works as expected, but if I intentionally put an error in the json file, Jenkins detects it and prints the error in the terminal, but "Build" still goes green. Can anyone identify the error? My expectation is that the stage "Build" goes to red as soon as there is an error in the JSON file.

Here is a Jenkins output from the terminal:

+ password=****
++ curl -k -s -H 'Content-Type: application/json' -X POST -d '{"username":"xxx","password":"****"}' /automation-api/session/login
+ login='{
  "username" : "xxx",
  "token" : "xxx",
  "version" : "9.19.200"
}'
++ echo 'xxx",
' '"version"' : '"9.19.200"
' '}'
++ cut -d '"' -f 1
+ token=xxx
+ curl -k -s -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build
{
  "errors" : [ {
    "message" : "unknown type: Job:Dummmy",
    "file" : "Test.json",
    "line" : 40,
    "col" : 29
  }, {
    "message" : "unknown type: Job:Dummmy",
    "file" : "Test.json",
    "line" : 63,
    "col" : 29
  } ]
}+ curl -k -s -H 'Authorization: Bearer xxx' -X POST /automation-api/session/logout
{
  "message" : "Successfully logged out from session xxx"
} ``
 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source