Libraries I use in some of my Jenkins pipelines to make life easier
The functions so far are heavily inspired by this repository, although I use it mainly in a multi-branch pipeline setup, so I have read the jenkins documentation about shared libraries and come up with this, to try to keep my Pipeline scripts DRY.
Executing mvn test
in the project root will run all the defined test
Refer to the Jenkins book for reference on setting up a scm repository as a source of your Jenkins Shared Libraries
The import is as follows:
@Library('JoSSteJenkinsGlobalLibraries')
import com.stevnsvig.jenkins.release.ReleaseUtil
...
This requires the library to be configured in Jenkins.
For more flexibility you could include it like this:
library identifier: "JenkinsGlobalLibrary@1.0.0", retreiver: modernSCM(
[$lass: "GitSCMSource", remote: "https://github.com/JoSSte/JenkinsGlobalLibraries.git, credentialsId: "${SCM_CREDENTIALS}"])
Note that this is all that is required for the ReleaseUtil class since all the methods and variables are static.
You can refer to the Jenkins book about non-static imports.
Using the functions defined is as easy as calling ReleaseUtil.*
For instance, here is an example of sending the globals to the script:
def branch_type = ReleaseUtil.get_branch_type "${env.BRANCH_NAME}"
def branch_deployment_environment = ReleaseUtil.get_branch_deployment_environment branch_type
... and then you can use the variables to check for what to do either scripted:
if (branch_deployment_environment) {
stage('deploy') {
if (branch_deployment_environment == ReleaseUtil.prodEnv) {
timeout(time: 1, unit: 'DAYS') {
input "Deploy to ${branch_deployment_environment} ?"
}
}
node {
echo "Deploying to ${branch_deployment_environment}"
}
}
if (branch_deployment_environment != ReleaseUtil.prodEnv) {
stage('integration tests') {
node {
echo "Running integration tests in ${branch_deployment_environment}"
//TODO do the actual tests
}
}
}
}
... or in a Declarative Pipeline:
pipeline {
stages {
stage ('Integration Tests') {
when {
expression { branch_deployment_environment == ReleaseUtil.testEnv }
}
steps {
echo "integration tests go here!"
}
}
}
}