shelljenkinsgroovyjenkins-pipelinecontrol-m

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"
} ``
 

Solution

  • Jenkins in order to consider a stage as failed, it will check the exit code of a command executed, in your case

    curl -k -s -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build
    

    The issue is that the curl, as a command, is executed successfully. But the body of the curl indicates that the api call failed.

    You could add --fail flag to your curl. This will force curl to return an erroneous exit code when the response status is > 400

    (HTTP) Fail silently (no output at all) on server errors. This is mostly done to enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22.

    curl --show-error --fail -k -H 'Authorization: Bearer xxx' -X POST -F definitionsFile=@ctmjobs/Test.json /automation-api/build