Issue
Suppose the yaml file is like this:
#test.yaml
0.6.5.1.0:
module:
- mysql
- zk
0.7.1.0.0:
module:
- java
Now I want to get the module list of specified version, and the version is a variable, I try to write the jenkins pipeline like this:
yamlFile = readYaml file: test.yaml
version = '0.7.1.0.0'
moduleList = yamlFile.get("${version}").get(module)
but this can't work, yamlFile.get("${version}") is a null object, how can I achieve this?
Solution
This works for me:
pipeline {
agent any
stages {
stage ('read') {
steps {
script {
def data = readYaml text: """
0.6.5.1.0:
module:
- mysql
- zk
0.7.1.0.0:
module:
- java
"""
version = '0.7.1.0.0'
println data.get(version).get('module')
}
}
}
}
}
The output:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on server in /home/user/workspace/task
[Pipeline] {
[Pipeline] stage
[Pipeline] { (read)
[Pipeline] script
[Pipeline] {
[Pipeline] readYaml
[Pipeline] echo
[java]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Answered By - MaratC
Answer Checked By - Senaida (JavaFixing Volunteer)