My jenkinsfile contains stages like these:
stages {
stage('init'){
steps {
sh 'do-init'
}
}
stage('region1') {
stages {
stage('build') {
steps{
script {
dir("testing"){
build(region:'region1', labels:"label1")
}
}
}
}
stage('delete') {
steps{
script {
dir ("testing"){
delete('region1')
}
}
}
}
}
}
stage('region2') {
// repeat region1 stage for region2, label2 - edited for brevity
}
}
Is it possible to refactor the inner "region stage" into a shared library, and then just call it twice in a script {} block?
My attempts so far have ended with the common DSL error "stages not found in steps"
Yes it is possible to achieve what you want, you just need to make sure that in the shared library function you are suing scripted pipeline syntax and not declarative syntax.
For example in your pipeline you can do the following:
Now in you shared library
vars
folder you will have a file calledbuildAndDelete.groovy
, and its contents will be:You can of course change the input parameters as needed.