Jenkins declarative pipeline: how to enter properties

I have Jenkins 2.19.4 with Pipeline: Declarative Agent API 1.0.1. How to use readProperties if you cannot define a variable to assign properties that are readable?

For example, to capture the SVN version number, I am currently capturing it in the following scenario:

"" "

echo "SVN_REVISION=\$(svn info ${svnUrl}/projects | \ grep Revision | \ sed 's/Revision: //g')" > svnrev.txt 

"" "

 def svnProp = readProperties file: 'svnrev.txt' 

Then I can access using:

 ${svnProp['SVN_REVISION']} 

Since it is not legal to define svnProp in a declarative style, how is readProperties used?

+1
jenkins jenkins-pipeline
source share
3 answers

You can use the script step inside the steps tag to run arbitrary pipeline code.

So something in the lines:

 pipeline { agent any stages { stage('A') { steps { writeFile file: 'props.txt', text: 'foo=bar' script { def props = readProperties file:'props.txt'; env['foo'] = props['foo']; } } } stage('B') { steps { echo env.foo } } } } 

Here I use env to propagate values ​​between steps, but other solutions are possible.

+9
source share

The @ jon-s solution requires granting script permission because it sets the environment variables. This is not required when working in one step.

 pipeline { agent any stages { stage('A') { steps { writeFile file: 'props.txt', text: 'foo=bar' script { def props = readProperties file:'props.txt'; } sh "echo $props['foo']" } } } } 
0
source share

To define common classes available for all steps, define values, for example, in props.txt as follows:

 version=1.0 fix=alfa 

and mix the Jenkins script and declarative pipeline like:

 def props def VERSION def FIX def RELEASE node { props = readProperties file:'props.txt' VERSION = props['version'] FIX = props['fix'] RELEASE = VERSION + "_" + FIX } pipeline { stages { stage('Build') { echo ${RELEASE} } } } 
0
source share

All Articles