From f26b3f5a3086f0307ee664907f0bff1e6ff90742 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 20 Dec 2018 14:21:54 +0100 Subject: [PATCH] try block instead of a post --- Jenkinsfile | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index ab8a7c2..fc2a366 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,9 +14,32 @@ node('master') { echo "flag: ${params.cat}" echo "flag: ${params.name}" } - post { - always { - echo "this is printed always" - } - } + + + try { + stage('Test') { + sh 'echo "Fail!"; exit 1' + } + echo 'This will run only if successful' + } catch (e) { + echo 'This will run only if failed' + + // Since we're catching the exception in order to report on it, + // we need to re-throw it, to ensure that the build is marked as failed + throw e + } finally { + def currentResult = currentBuild.result ?: 'SUCCESS' + if (currentResult == 'UNSTABLE') { + echo 'This will run only if the run was marked as unstable' + } + + def previousResult = currentBuild.previousBuild?.result + if (previousResult != null && previousResult != currentResult) { + echo 'This will run only if the state of the Pipeline has changed' + echo 'For example, if the Pipeline was previously failing but is now successful' + } + + echo 'This will always run' + } + }