Knowledge base
Search…
Jenkins - force exit pipeline when failure

1. Issue

In some specific cases, if we use try catch exception, Jenkins knows that issues, but still continue running other stage in pipeline. We need a force handling to make this pipeline failure immediately

2. Solution

1
currentBuild.result = 'FAILURE'
2
error("Failure right here, force exit")
Copied!
1
stage("DB migration") {
2
steps { script {
3
slackSend (color: COLOR_MAP['SUCCESS'],
4
channel: '#live-deployment',
5
token: 'Fawxxxxxxxxxxxx',
6
message: "*`STARTING`*: *core db migrating*, build #${env.BUILD_NUMBER}, <${env.BUILD_URL}|Go to this job>")
7
try {
8
withCredentials([usernamePassword(credentialsId: 'xxxxx-credential', usernameVariable: 'DB_USERNAME', passwordVariable: 'DB_PASSWORD')]) {
9
withAWS(region:"eu-cexxxxxxx", credentials:"aws") {
10
sh """
11
packer build -force dbmigration/vm-db-migration.json
12
"""
13
}
14
}
15
} catch (error) {
16
slackSend (color: COLOR_MAP['FAILURE'],
17
channel: '#live-deployment',
18
token: 'Fawxxxxxxxxxxxx',
19
message: "*`FAILURE`*: *core db migrating*, build #${env.BUILD_NUMBER} \n${error}\n<${env.BUILD_URL}|Go to this job>")
20
currentBuild.result = 'FAILURE'
21
error("${error}")
22
}
23
}}
24
}
Copied!
Last modified 2yr ago
Copy link