0

I am trying to place timeout in below code and skip rest of the stages in jenkins if still "status = progress" after 3 minutes.

script {
                        def date = new Date()
                        currtime = date.getTime()
                        future_time = date.getTime() + 3 * 60000

                            while (currtime < future_time) {
                            date = new Date()
                            currtime = date.getTime()

                            Status= "IN_PROGRESS"

                            if (Status == 'IN_PROGRESS') {
                                echo 'Status is IN_PROGRESS'
                            } else if (Status == 'SUCCEEDED') {
                                break
                            } else if (Status == 'FAILED') {
                                echo "Status  is FAILED"
                                exit(1)
                            } else {
                                exit(1)
                            }
                        }
}
abhi
  • 41
  • 1
  • 7
  • Does this answer your question? [How to add a timeout step to Jenkins Pipeline](https://stackoverflow.com/questions/38096004/how-to-add-a-timeout-step-to-jenkins-pipeline) – Moro Apr 29 '22 at 19:01

1 Answers1

0

You can just use timeout inside your pipeline without any custom time calculation. It automatically works while stage in the "IN_PROGRESS" status. When a timeout is reached, all next stages will be skipped, and job status will be "ABORTED".

pipeline {
    agent {
        label 'agent_name'
    }

    stages {
        stage('Stage_name') {
            steps {
                timeout(time: 3, unit: 'MINUTES') {
                    script {
                        // Your logic here ...
                    }
                }
            }
        }

        stage { .. }

        stage { .. }
    }
}

Also, I guess your question is related to this one. You can find there more examples.

cheburek
  • 101
  • 1
  • 8