I created a Jenkins Shared Library with many functions into /vars. Among them, there is a devopsProperties.groovy
with many properties :
class devopsProperties {
//HOSTS & SLAVES
final String delivery_host = "****"
final String yamale_slave = "****"
//GIT
final Map<String,String> git_orga_by_project = [
"project1" : "orga1",
"project2" : "orga2",
"project3" : "orga3"
]
...
}
Other functions in my Shared Library use these parameters. For example, gitGetOrga.groovy
:
def call(String project_name) {
devopsProperties.git_orga_by_project.each{
if (project_name.startsWith(it.key)){
orga_found = it.value
}
}
return orga_found
}
But now, as we have many environments, we need to load at the beginning of the pipeline the devopsProperties. I create properties files in the resources :
+-resources
+-properties
+-properties-dev.yaml
+-properties-val.yaml
+-properties-prod.yaml
and create a function to load it :
def call(String environment="PROD") {
// load the specific environment properties file
switch(environment.toUpperCase()) {
case "DEV":
def propsText = libraryResource 'properties/properties-dev.yaml'
devopsProperties = readYaml text:propsText
print "INFO : DEV properties loaded"
break
case "VAL":
def propsText = libraryResource 'properties/properties-val.yaml'
devopsProperties = readYaml text:propsText
print "INFO : VAl properties loaded"
break
case "PROD":
def propsText = libraryResource 'properties/properties-prod.yaml'
devopsProperties = readYaml text:propsText
print "INFO : PROD properties loaded"
break
default:
print "ERROR : environment unkown, choose between DEV, VAL or PROD"
break
}
return devopsProperties
}
but when I try to use it in a pipeline :
@Library('Jenkins-SharedLibraries')_
devopsProperties = initProperties("DEV")
pipeline {
agent none
stages {
stage("SLAVE JENKINS") {
agent {
node {
label ***
}
}
stages{
stage('Test') {
steps {
script {
print devopsProperties.delivery_host // THIS IS OK
print devopsProperties.git_orga_by_project["project1"] // THIS IS OK
print gitGetOrga("project1") //THIS IS NOT OK
}
}
}
}
}
}
}
The last print generates an error : groovy.lang.MissingPropertyException: No such property: devopsProperties for class: gitGetOrga
How I can use a global variable into all my Jenkins Shared Library functions ? If possible, I prefer to not pass it in parameter of all functions.
You need to import your class in gitGetOrga.groovy
import path/to/devopsProperties.groovy;
def call(String project_name) {
devopsProperties.git_orga_by_project.each{
if (project_name.startsWith(it.key)){
orga_found = it.value
}
}
return orga_found
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.