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.
properties = null
def loadProperties() {
node {
checkout scm
properties = new Properties()
File propertiesFile = new File("${workspace}/pipeline.properties")
properties.load(propertiesFile.newDataInputStream())
echo "Immediate one ${properties.repo}"
}
}
pipeline {
agent none
stages {
stage ('prepare') {
agent any
steps {
script {
loadProperties()
echo "Later one ${properties.branch}"
}
}
}
stage('Build') {
agent { label 'master' }
steps {
// works fine. properties is available everywhere
echo properties.branch
}
}
}
}
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.
properties = null
def loadProperties() {
node {
checkout scm
properties = readProperties file: 'pipeline.properties'
echo "Immediate one ${properties.repo}"
}
}
pipeline {
agent none
stages {
stage ('prepare') {
agent any
steps {
script {
loadProperties()
echo "Later one ${properties.ansible}"
}
}
}
stage('Build') {
agent any
steps {
echo properties.branch
}
}
}
}