1

I am deploying a WAR using Jenkins and I want the final pipeline stage to actually hit the endpoint and check the JSON response. If it comes up and has the right value, then it succeeds, otherwise the build should fail.

How would I do this?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • You can use `CURL` in shell script to call that URL, if you receive anything other than 200 then exit from the script with the error code `exit xxx` ie. `exit 125`. – 11thdimension Jun 06 '17 at 19:09

1 Answers1

2
pipeline {
    agent any

    environment{
        CHECK_URL = "https://stackoverflow.com"
        CMD = "curl --write-out %{http_code} --silent --output /dev/null ${CHECK_URL}"

    }

    stages {
        stage('Stage-One') {
            steps {
                script{
                    sh "${CMD} > commandResult"
                    env.status = readFile('commandResult').trim()
                }
            }
        }
        stage('Stage-Two') {
            steps {
                script {
                    sh "echo ${env.status}"
                    if (env.status == '200') {
                        currentBuild.result = "SUCCESS"
                    }
                    else {
                        currentBuild.result = "FAILURE"
                    }
                }
            }
        }
    }
}

Please change the CMD as per your need and also the post section can be used to set the build status. Thanks!


Below curl is one example of printing the Jason value

  curl -s 'YOUR-URL' | \
        python -c "import sys, json; print json.load(sys.stdin)['VARIABLE-TOFILTER']"

Parsing JSON with Unix tools

Chandan Nayak
  • 10,117
  • 5
  • 26
  • 36