Is it possible to detect TIMEOUT event and act upon it?

63 views
Skip to first unread message

Idan Adar

unread,
Mar 18, 2018, 9:57:16 AM3/18/18
to Jenkins Users
In a declarative pipeline's Jenkinsfile it is possible to add a timeout option both global as well as override it per stage. E.g.: 

stage ("SonarQube analysis") {
  options
{
    timeout
(time: 5, unit: 'MINUTES')
 
}


  steps
{
    script
{
      STAGE_NAME
= "SonarQube analysis"


      withSonarQubeEnv
('SonarQube') {
        sh
"../../../sonar-scanner/bin/sonar-scanner"
     
}


     
// Check whether coverage threshold is met, otherwise fail the job.
     
def qualitygate = waitForQualityGate()
     
if (qualitygate.status != "OK") {
        slackSend
(
          color
: '#F01717',
          message
: "*$JOB_NAME*: <$BUILD_URL|Build #$BUILD_NUMBER>, '$STAGE_NAME' stage failed."
       
)
        error
"Pipeline aborted due to quality gate coverage failure: ${qualitygate.status}"
     
}
   
}
 
}
}


Is it possible to make some action in case the timeout was reached? to detect it somehow?
In case of a timeout I would like to send a Slack notification.

Right now what happens is that the scanning reached 5 minutes and thus never entered the if statement; in which case there is "silent failure".

Victor Martinez

unread,
Mar 19, 2018, 6:48:24 AM3/19/18
to Jenkins Users
I did play with that sometime ago but I don't have access to those pipelines anymore... The below article might help you out:

Although it's not about the declarative pipeline but the scripted one. but likely it works for your use case.

Cheers

Idan Adar

unread,
Mar 19, 2018, 7:15:29 AM3/19/18
to jenkins...@googlegroups.com
According to the Jenkins documentation for timeout, there is an exception thrown:
Executes the code inside the block with a determined time out limit. If the time limit is reached, an exception is thrown, which leads in aborting the build (unless it is caught and processed somehow).


How do I catch the specific exception for timeout? the parameter inside catch is not specific enough for it.

try {
// some stages for the workflow
}

catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e){
}

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/-ifvFnu_Un4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/7b34bb58-d0e0-4d38-8d47-eff33f4839c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Idan Adar

Victor Martinez

unread,
Mar 19, 2018, 7:47:19 AM3/19/18
to Jenkins Users
This is indeed what I did:

But using scripted pipelines, it looks like you cannot use try/catch within the declarative pipeline but post steps (https://jenkins.io/doc/book/pipeline/syntax/#post) but the timeout triggers a failure therefore you need to run something like

pipeline {
    agent any
    options {
        timeout(time: 1, unit: 'SECONDS')
      }
    stages {
        ...
    }
    post { 
        failure { 
            // Catch failures and even the tiemout one
        }
    }
}


I don't know whether the declarative post step allows a kind of more accurate granularity about what caused that particular failure... 

Cheers

Idan Adar

unread,
Mar 19, 2018, 7:53:58 AM3/19/18
to Jenkins Users
In declarative pipeline there is also the option for a script {} block where "traditional" Jenkins scripting can be done, so what you're suggesting can be done there. However, I'm not looking to adding the user prompt like in your example, which seems like SYSTEM depends on, in your example.

Do you have an example without this?
Also, your example is not using the exception try/catch approach in my last comment.

Victor Martinez

unread,
Mar 19, 2018, 11:48:36 AM3/19/18
to Jenkins Users
I'm afraid I do not have any other examples using the global timeout option. I hope someone else might come up with something else. 

Cheers
Reply all
Reply to author
Forward
0 new messages