2
votes

I have written a jenkins scripted pipeline which has 3 stages in it. And in each stage i am calling a curl command to start a jenkins job which is on my remote server. But, the problem is 2nd stage is getting executed before 1st stage completes its execution. Please help me how to resolve this?

node{
   properties([
        disableConcurrentBuilds()
    ])
    stage('stage1'){
       sh 'curl -X POST -H "Content-Type: application/json" -d "{         "tagname": "$tagname" }" -vs http://pkg.rtbrick.com:8080/generic-webhook-trigger/invoke?token=qwerty'
    }

    stage('stage2'){
        sh 'curl -X POST -H "Content-Type: application/json" -d "{ "tagname": "$tagname" }" -vs http://image.rtbrick.com:8080/generic-webhook-trigger/invoke?token=1234'
    }

     stage('stage3'){
       sh 'curl -X POST -H "Content-Type: application/json" -d "{ "tagname": "$tagname" }" -vs http://image.rtbrick.com:8080/generic-webhook-trigger/invoke?token=1804'}
    }
}

"Stage2" should start only if "stage1" is completed.

1
Hi Jay, can you share the log from your job's execution please?hakamairi
[Pipeline] { [Pipeline] properties [Pipeline] stage [Pipeline] { (build) [Pipeline] sh + curl -X POST -H Content-Type: application/json -d { tagname: ewetel-staging } -vs pkg.rtbrick.com:8080/generic-webhook-trigger/… * Trying 104.131.27.92... * Connected to pkg.rtbrick.com port 8080 (#0) > POST /generic-webhook-trigger/invoke?token=qwerty HTTP/1.1 > Host: pkg.rtbrick.com:8080 > Content-Type: application/json > Content-Length: 27 } [27 bytes data] * upload completely sent off: 27 out of 27 bytes < HTTP/1.1 200 OKJayashree Ranganath
that's the complete log?hakamairi
< X-Content-Type-Options: nosniff < Content-Type: application/json;charset=utf-8 < Content-Length: 220 < Server: Jetty(9.4.z-SNAPSHOT) < { [220 bytes data] * Connection #0 to host pkg.rtbrick.com left intact {"status":"ok","data":{"triggerResults":{"Refresh-Custom-Apt-Repo":{"id":702,"triggered":true,"url":"queue/item/702/"}}}}[Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (trigger) [Pipeline] sh [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESSJayashree Ranganath
Can you actually update your question with the complete log from your pipeline?hakamairi

1 Answers

1
votes

It's doable using the Jenkins REST API and the waitUntil step in combination with timeout (without a timeout it could hang forever):

def response

timeout(30) {
    waitUntil {
        response = sh(
            script: 'curl http://pkg.rtbrick.com:8080/view/job/my-job/lastBuild/api/json | grep "\"result\":\"SUCCESS\""',
            returnStatus: true
        )

        return (response == 0)
    }
}

if (response != 0) {
    build.result = 'ERROR'
}