Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 562
Jayashree Ranganath Avatar asked Jan 23 '26 12:01

Jayashree Ranganath


1 Answers

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'
}
like image 116
DevAntoine Avatar answered Jan 27 '26 00:01

DevAntoine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!