Home > OS >  How to do polling for REST endpoint on Jenkins?
How to do polling for REST endpoint on Jenkins?

Time:02-11

On the Jenkins pipeline that I've created, I make a POST curl request once. However, since this job goes into a queue first, I need to wait till the job is completed. This is an example of my command

curl -X POST -H "Content-Type: application/json" \
-d '{"source_path": "source/path/example", "destination_path": "destination/path/example"}' \
http://example.com/v1/postJob

This returns a JSON response {"jobID": 1234}

I want the Jenkins job to stay in 'running' state till the GET endpoint returns "status": success. The GET endpoint returns a JSON body with a field 'status' that indicates whether the job is done. This is the GET endpoint

curl http://example.com/v1/jobs/1234

This returns a JSON response

{
   "job": 1234, 
   "status": success
}

Can anyone provide me with an example of polling that can be implemented directly on Jenkins without using a Jenkinsfile?

CodePudding user response:

For polling an endpoint on bash, this code snippet should do the trick

GETURL="http://exmple.com/v1/tasks/${TASK_ID}"

while true; 
    do 
        STATUS=$(curl $GETURL | jq -r '.status'); //'status' is to check JSON response field
        echo ${STATUS}
        if [[ "$STATUS" == "succeeded" ]]; then 
            break; 
        fi; 
        sleep 2700; //The endpoint will be hit again after 2700 seconds
    done
  • Related