My requirement is simple, i just want to externalize some 'values' to make my Jenkinsfile more re usable and for this i need to load the properties from a file which is going to be right next to Jenkinsfile, and make sure that these properties are available anywhere in the pipeline. I am still new to groovy and Jenkins code but never thought such a simple thing would be so difficult. I enabled some methods in script security plugin but the following code (and several variations i tried around it) always pose errors or print null or give me NPE. I have tried multiple combinations and the below code is just one of them.
properties = null
@NonCPS
def loadProperties() {
checkout scm
File propertiesFile = new File('${workspace}/pipeline.properties')
propertiesFile.withInputStream {
properties.load(propertiesFile)
}
}
pipeline {
agent none
stages {
stage ('prepare') {
agent any
steps {
script {
loadProperties()
echo "${properties['repo']}"
}
}
}
stage('Build') {
agent any
steps {
sh 'echo ${properties.repo}'
}
}
}
}
I figured out a couple of ways to externalize properties in Jenkins pipelines. You can choose your pick based on the main difference.
1) Using groovy code entirely. This code snippet would require you to enable several method signatures in 'In-process script approval' which comes in with script security plugin, and hence this should be done only after due consideration.
2) Using pipeline utility steps plugin - Pipeline suite of plugins include this by default and it allows a better way to load properties without requiring to enable security exceptions. I would recommend this method.
Thanks for Answer #1. It helped a lot, I used option 2. I faced some problems, and would like to share my solution:
1) I added a "prepare" stage, inside the pipeline section of the declarative pipeline:
The format of the properties file is:
Please note that there are no quote on the properties.
I used the properties as ${properties.nextBranchName} also on other steps, stages and post. As mentioned on answer 1: there is a need to install "pipeline utility steps" plugin
Using the "pipeline utility steps" plugin, you can define general vars available to all stages. For example, let
props.txt
as:and mix script and declarative Jenkins pipeline as: