Groovy Jenkins Pipeline, Baby!
Problem: Tedious and Hard to Track Changes When Working With Many Microservices
Jenkins is a widely used CICD tool. Working with many microservices can be very complex. Small changes, like a new URL, can be a tedious task, because it has to be done for every job. Due to the missing changelog, it's also hard to track which changes were made and by whom.
Solution: Define Jobs as Groovy Code With the "Jenkins Pipeline"
With the plugin suite "Jenkins Pipeline" you can define different jobs as Groovy code. You can then check this into your preferred version control and maintain and further develop it in addition to the project code.
In connection with a multi-branch pipeline, all branches of a project are automatically built according to the instructions in the 'Jenkinsfile'.
Example for Groovy Code
A project with multiple services, all built with Maven. A merge on the branch develop is to build a new version.
Coffee-Service, Food-Service: Jenkinsfile
def pipeline
stage('Load pipeline') {
// Load the pipeline from the shared repository
fileLoader.withGit(
'https://url-to-pipeline-repo.git',
'master',
' id-of-in-jenkins-stored-credentials') {
// Every service is able to use pipeline.groovy
pipeline = fileLoader.load('pipeline.groovy')
}
}
pipeline.execute()
Pipeline Repo: pipeline.groovy
def execute() {
stage('Checkout') {
checkout scm
}
stage('Build service') {
sh "mvn clean install"
}
// The variable env.BRANCH_NAME is automatically set to the current branch.
// As a consequence of this feature the pipeline can deal with every new branch
if ("develop".equals(env.BRANCH_NAME) {
stage('Release') {
sh "mvn release:prepare release:perform"
}
}
}
Further Aspects
---
Author
Christof Huber / Software Developer / Business Division Automotive Bavaria
Download Toilet Paper #110: Groovy Jenkins Pipeline, Baby! (pdf)