This script
pipeline {
agent any
stages {
stage('play with properties') {
steps {
script {
def propsText = 'prop1 = val1\nprop2 =
val2\nbuild.host=mybuildhost.hosts.myorganization.org'
writeFile file: 'myProps.properties', text: propsText
def props = readProperties file: 'myProps.properties'
echo "${props}"
echo props['build.host']
}
}
}
}
}
works. You can read properties from a properties file into a variable and
then access the properties by indexing that variable.
But you have to place the variable into a script block. The declarative
syntax of Jenkins pipelines
(https://www.jenkins.io/doc/book/pipeline/syntax/#declarative-pipeline)
doesn't allow them anywhere else, in particular not "... right at the
beginning.".
So if you need the properties in several script blocks you have to
readProperties them in each of the blocks.
This is different with the scripted syntax
(https://www.jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline) where
you can set a variable in an outer block that spans multiple stages, like
this:
node {
def props = ''
stage('read properties') {
writeFile file: 'myProps.properties', text: 'prop1 = val1\nprop2 =
val2\nbuild.host=mybuildhost.hosts.myorganization.org'
props = readProperties file: 'myProps.properties'
echo "${props}"
}
stage('access properties') {
echo props['build.host']
}
}
--
You received this message because you are subscribed to the Google Groups
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/eeb2c2a9-4088-410e-a8bf-0b7f5c92b068n%40googlegroups.com.