Send mail only if timeout exceeds

Jenkins setup: Jenkins 2.387.2

Hello All, I woud like to send a mail to the devOps Team only if the timeout of a build has exceeded.

How can I determine if a FlowInterruptedException has thrown?

options {
            timeout(time: 6, unit: 'HOURS')
        }
...
post {
        failure {
            if (how can we determine if a FlowInterruptedException has thrown) {
                mail to: 'devops-team@acmelab',
                        subject: "Timeout: ${currentBuild.fullDisplayName}",
                        body: "See: ${env.BUILD_URL}"
            }
        }
}


Greetings Tom

Hi @duschata, I am trying to find a solution for the same use case. Did you find anything which helps you accomplish this?

BR

I think to determine if a FlowInterruptedException has been thrown due to a timeout in a Jenkins Pipeline, you could use the catchError step to catch the exception and then check the cause of the failure. :thinking:

Here’s how you could modify your pipeline to send an email to the DevOps team only if the build timeout has been exceeded:

pipeline {
    agent any
    options {
        timeout(time: 6, unit: 'HOURS')
    }
    stages {
        stage('Example') {
            steps {
                script {
                    catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                        // Your build steps here
                    }
                }
            }
        }
    }
    post {
        failure {
            script {
                def timeoutExceeded = false
                currentBuild.rawBuild.getActions(jenkins.model.InterruptedBuildAction).each { action ->
                    action.getCauses().each { cause ->
                        if (cause instanceof org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) {
                            timeoutExceeded = true
                        }
                    }
                }
                if (timeoutExceeded) {
                    mail to: 'devops-team@acmelab',
                         subject: "Timeout: ${currentBuild.fullDisplayName}",
                         body: "See: ${env.BUILD_URL}"
                }
            }
        }
    }
}

Please keep in mind this has not been tested.
Best of luck with this approach. :crossed_fingers: