My project has many common variables for many other projects, so I use Jenkins Shared Library and created a vars/my_vars.groovy
file where I defined my variables and return Map of them:
class my_vars {
static Map varMap = [:]
static def loadVars (Map config) {
varMap.var1 = "val1"
varMap.var2 = "val2"
// Many more variables ...
return varMap
}
}
I load the Shared Library in my Jenkinsfile, and call the function in the environment bullet, as I want those variables to be as environment variables .
Jenkinsfile:
pipeline {
environment {
// initialize common vars
common_vars = my_vars.loadVars()
} // environment
stages {
stage('Some Stage') {
// ...
}
}
post {
always {
script {
// Print environment variables
sh "env"
} // script
} // always
} // post
} // pipeline
The thing is that the environment bullet gets KEY=VALUE
pairs, thus my common_vars
map is loaded like a String value (I can see that on sh "env"
).
...
vars=[var1:val1, var2:val2]
...
What is the correct way to declare those values as an environment variables? My target to get this:
...
var1=val1
var2=val2
...
env
map. – Zumstein