MMA
MMA

Reputation: 458

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,

Upvotes: 3

Views: 12223

Answers (2)

MMA
MMA

Reputation: 458

I was trying to run npm start that runs forever

However i got to know there was alternate command

npm build

that fixed my issue. So i dont need to include try catch block and time out.

Upvotes: 0

Sysanin
Sysanin

Reputation: 1803

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?

Upvotes: 3

Related Questions