In this script , want to repeat the stage if requestState id PENDING after 2 minutes every time, and should stop if its not pending.

pipeline {
  agent {
    label "cicd-npe-slave0"
  }
  options {
    buildDiscarder(logRotator(numToKeepStr: '20'))
    disableConcurrentBuilds()
  }
  parameters {
    string(name: 'access_token', defaultValue: '')
    string(name: 'body', defaultValue: '')
  }
  stages {
    stage('Execute curl command') {
      steps {
        script {
          def response = sh(script: '''curl --location 'URL' \\
            --header 'Content-Type: application/json' \\
            --header 'Authorization: Bearer xxx' \\
            --data '{
              xxxx
             }' ''', returnStdout: true).trim()
             println(response)
             def jsonSlurper = new groovy.json.JsonSlurper()
             def jsonResponse = jsonSlurper.parseText(response)
             requestState = jsonResponse.response.requestState
             echo "Request State = '${requestState}'"
        }
      }
    }
  }
}

I tried many things, getting below errors for the above script If I was adding sleep intervels


Caused: java.io.NotSerializableException: groovy.json.internal.LazyMap
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:274)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)

Finished: FAILURE
1

There are 1 best solutions below

0
ycr On

Here is a working solution. Please adjust it as you need.

pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '20'))
    disableConcurrentBuilds()
  }
  parameters {
    string(name: 'access_token', defaultValue: '')
    string(name: 'body', defaultValue: '')
  }
  stages {
    stage('Execute curl command') {
      steps {
        script {
          timeout(180) {
                waitUntil {
                   script {
                      def response = sh(script: '''curl --location 'http://demo5333947.mockable.io/hello' \\
                     --header 'Content-Type: application/json' \\
                     --header 'Authorization: Bearer xxx' \\
                     --data '{}' ''', returnStdout: true).trim()
                      println(response)
                      def requestState = getState(response);
                      echo "Request State = '${requestState}'"
                      if(requestState.equals('PENDING')) {
                        sleep 120; // sleep for 2 mins
                        return false;
                      } else {
                        return true;
                      }
                   }
                }
            }
        }
      }
    }
  }
}

def getState(response) {
    def jsonSlurper = new groovy.json.JsonSlurper()
    def jsonResponse = jsonSlurper.parseText(response)
    println jsonResponse.response
    def requestState = jsonResponse.response.requestState
    return requestState
}