Is there a way to write a stage in a Jenkins shared library so that it can be added to declarative pipelines?
Background
For many projects, I need to run tests on multiple different servers. My solution is to create a bunch of parallel stages, one for each of the servers that I want to run on. I've gotten to this pipeline (running on 2 servers as an example):
pipeline {
agent any
stages {
stage('Test') {
parallel {
stage('San Jose') {
agent {
label "test && san-jose"
}
steps {
sh 'run-tests'
}
}
stage('Dallas') {
agent {
label 'test dallas'
}
steps {
sh 'run-tests'
}
}
}
}
}
}
Which is great, but I'd really like to keep my code DRY and not have to copy and paste this Test stage to every project I have (especially since this has to run on about 10 servers, with only the label changing each time).
I could put the whole pipeline into my shared libary (like Jenkins's example in the documentation), but then I can't customize the other stages (for example, adding a release stage).
What I'd really like to do is this:
pipeline {
agent any
stages {
runOnServers {
sh 'run-tests'
}
stage('Release') {
echo 'release'
}
}
}
Question
Is there a way I can do this?