I’ve added a timeout to a declarative pipeline, but I can’t figure out how to throw an error and set the build status to “failed” when the timeout limit is reached.
I’ve tried using a try-catch like this:
pipeline {
agent { label 'importer' }
stages {
stage('Import') {
steps {
script {
try {
timeout(time: 8, unit: 'MINUTES') {
sh label: 'import', script: '''
ssh user@server <<\'ENDSSH\'
cd job/folder
bash job.sh
ENDSSH'''
}
}
catch (error) {
println error
error 'Timeout reached.'
}
}
}
}
}
}
I also tried asking ChatGPT to write me some code to accomplish this, and this is what it gave back to me:
pipeline {
agent any
stages {
stage('Example') {
steps {
timeout(time: 8, unit: 'MINUTES', failFast: true) {
// put your pipeline code here
} // end of timeout step
script {
if (currentBuild.result == 'ABORTED') {
error('The pipeline timed out after 8 minutes')
}
} // end of script block
} // end of steps block
} // end of stage block
} // end of stages block
} // end of pipeline block
I tried altering my pipeline to match this, but it fails because it doesn’t like the failFast parameter as one of the timeout params.
Anyway, if anyone could give me some guidance here, I would greatly appreciate it.