Jayashree Ranganath
Jayashree Ranganath

Reputation: 31

jenkins scripted Pipeline stage does not wait for its commands to finish executing

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.

Upvotes: 3

Views: 4689

Answers (1)

DevAntoine
DevAntoine

Reputation: 1942

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'
}

Upvotes: 1

Related Questions