Jenkins Joy: Shared library for Jenkins Declarative Pipeline
Since the introduction of the Jenkins Declarative Pipeline syntax (as opposed to the Scripted Pipeline syntax) the concept of a shared library has become more important as we are otherwise restricted to the sections of the pipeline model. So we’ll make a basic set-up with a call to a defined step in a shared library.
A shared library called "jdriven" is configured globally in Jenkins (see link to jenkins.io at the end on how to do that).
It has a defined step showQuote
defined in it’s own file on branch "blog/shared-lib-example"
def call() {
def quotes = ['Make it so', 'Tea. Earl Grey. Hot.', 'Engage']
println quotes.get(new Random().nextInt(quotes.size()))
}
A basic Jenkinsfile
defining the pipeline, calling step showQuote from the shared library:
@Library('jdriven@blog/shared-lib-example') _
pipeline {
agent any
stages {
stage('Show quote') {
steps {
showQuote()
}
}
}
}
More information on the Declarative Pipeline and the use of Shared Libraries at jenkins.io
Written using Jenkins 2.32.2 and Pipeline plugin 2.5