Reputation: 799
I'm wondering would it be possible to create my own variant of steps
in Jenkins declarative pipeline, so I could say:
pipeline {
stages {
stage('Do work') {
stepsChuckNorrisWrote {
...
}
}
}
}
What I'm looking for is an elegant way of writing steps which won't cause the build to fail even if they fail.
Upvotes: 0
Views: 447
Reputation: 5321
I don't know of any way to do exactly what you want, but I think you could do something that gets you 75%-100% there (depending on what you expect to happen for later steps when an error occurs) with a shared library:
pipeline {
stages {
stage('Do work') {
steps {
stepsChuckNorrisWrote {
...
}
}
}
}
}
Then create a global variable in a shared library called stepsChuckNorrisWrote
. You are passing a closure to it here. That closure can contain all the steps you want. Wrap the execution of that closure in a try/catch and don't let it fail.
Something like this:
// vars/stepsChuckNorrisWrote.groovy
def call(Closure body) {
node('windows') {
try {
body()
} catch ( all ) {
// handle errors here
}
}
}
With this method, as soon as the first step fails, none of the others will continue. There may be some way to pull each step out of the closure and essentially wrap it in a try/catch, but I don't know how to do such a thing.
Upvotes: 2