2

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,

MMA
  • 408
  • 3
  • 7
  • 19

2 Answers2

2

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?

Sysanin
  • 1,501
  • 20
  • 27
  • Getting error.**No stages specified**. Looks like try block is not recognised in stages block. Here is how i gave stages{ try{ stage ('testjob') {....... Did i miss anything – MMA Aug 29 '18 at 11:29
  • @MMA could you please share more detailed code of your script to clarify this issue? and from where exactly you are getting such error? at console output, at pipeline groovy sandbox? or any other place? – Sysanin Aug 30 '18 at 07:47
0

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
MMA
  • 408
  • 3
  • 7
  • 19