I am trying to add try catch block
in Jenkins declarative pipeline but I end up with the following error, I read the document on adding try catch block for scripted pipeline syntax of Jenkins(https://jenkins.io/doc/book/pipeline/syntax/#post-conditions) but I didn't get anything on declarative syntax.
pipeline {
agent any
stages {
try {
stage('Checkout') {
steps {
script {
if (ci_branches.contains(env.BRANCH_NAME)) {
// Pull the code from bitbucket repository
checkout scm
}
}
}
}
}
catch(all) {
currentBuild.result='FAILURE'
}
}
}
Jenkins ci build result
[Bitbucket] Build result notified
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 36: Expected a stage @ line 36, column 13.
try {
^
WorkflowScript: 35: No stages specified @ line 35, column 9.
stages {
^
Try/catch should be inside a script when using declarative pipeline syntax. Test the following:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
script {
try {
if (ci_branches.contains(env.BRANCH_NAME)) {
// Pull the code from bitbucket repository
checkout scm
}
}
catch(all) {
currentBuild.result='FAILURE'
}
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With