Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline timeout

I have a stage in Jenkins pipeline(declarative) on my windows machine. There is a script that runs forever. However i need logs and have to ensure it doesnt break in between so i cannot run it in background. Bascially i am looking for if there is not logs populating in jenkins for sometime it should proceed to next stage with "SUCCESS" status of that stage.

I have used time option, however it is marking the job as failure and successive stage wont run and job gets aborted.

timeout(time: 150, unit: 'SECONDS', activity: true)

Any way if i can mark the status of stage to success after so and so duration.

Thanks,

like image 849
MMA Avatar asked Nov 27 '25 18:11

MMA


1 Answers

You can try to play with try\catch block and currentBuild global variable, for example (tested):

try {
    stage("UNSTABLE"){
        timeout(time: 20, unit: 'MINUTES') {
        //do something
        echo "UNSTABLE stage"
        currentBuild.result = "UNSTABLE"
        }
    }
    stage("SUCCESS"){
        timeout(time: 20, unit: 'MINUTES') {
        //do something
        echo "SUCCESS stage"
        currentBuild.result = "SUCCESS"
        }
    }
    stage("FAILURE"){
        timeout(time: 20, unit: 'MINUTES') {
        //do something
        echo "FAILURE stage"
        //currentBuild.result = "FAILURE" //this will fail all the pipeline
        }
    }
} catch (err) {
    echo err
    currentBuild.result = "SUCCESS"
    //do job
} finally {
    currentBuild.result = "SUCCESS"
    //do job
}

From pipeline-syntax docs (link to access this on your local jenkins - http://localhost:8080/pipeline-syntax/globals#currentBuild):

The currentBuild variable may be used to refer to the currently running build. It has the following readable properties:

result - typically SUCCESS, UNSTABLE, or FAILURE (may be null for an ongoing build)

currentResult - typically SUCCESS, UNSTABLE, or FAILURE. Will never be null.

Also see: How to manipulate the build result of a Jenkins pipeline job?

like image 101
Sysanin Avatar answered Nov 30 '25 00:11

Sysanin