I'm just starting with using Jenkins declarative pipelines. As I'm supporting a few similar projects I was thinking of putting similar pipeline steps (or even stages) into reusable building blocks. These blocks should be maintained at a central spot and then included by individual pipelines (speak: DRY).
I saw shared libraries as an option for scripted pipelines but I'm not sure if it works for declarative pipelines, too.
Do you know a way to use something like building blocks in Jenkins declarative pipelines?
Example to clarify:
If you got a standard pipeline for Maven projects (e. g. Spring Boot), it looks somewhat like:
pipeline {
agent {
dockerfile true
}
stages {
stage('Build') {
steps {
sh 'mvn -U -DskipTests clean package'
}
}
stage('Test') {
parallel {
stage('Unit Tests') {
steps {
sh 'mvn test'
}
}
stage('Integration Tests') {
steps {
sh 'mvn integration-test'
}
}
}
}
stage('Deploy') {
steps {
sh 'mvn deploy'
}
}
}
}
But instead of copying this to all projects, it would be great if following use cases could be easy handled.
For a project without need to customize it would be good to use it for examle like:
defaultMavenpipeline{}
where defaultMavenpipeline will be replaced by above pipeline (I think this is possible with shared libraries).
For a project with need to customize only some stages would something like this be possible?
pipeline {
defaultDockerAgent{}
stages {
stage('Build') {
steps {
sh 'mvn -U -DskipTests clean package'
// ... customize some stuff ...
}
}
defaultTestStage{}
stage('Deploy') {
steps {
// ... customize some stuff ...
sh 'mvn deploy'
}
}
}
}
Sorry for long post and thank you very much in advance!
