简体   繁体   English

在 Jenkins 共享库中定义全局变量

[英]Define global variable in Jenkins Shared Library

I created a Jenkins Shared Library with many functions into /vars.我在 /vars 中创建了一个包含许多功能的 Jenkins 共享库。 Among them, there is a devopsProperties.groovy with many properties :其中,有一个devopsProperties.groovy有很多属性:

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 :例如, 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.但是现在,由于我们有很多环境,我们需要在管道的开头加载 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最后一次打印产生错误: groovy.lang.MissingPropertyException: No such property: devopsProperties for class: gitGetOrga

How I can use a global variable into all my Jenkins Shared Library functions ?如何在我的所有 Jenkins 共享库函数中使用全局变量? If possible, I prefer to not pass it in parameter of all functions.如果可能的话,我宁愿不在所有函数的参数中传递它。

You need to import your class in gitGetOrga.groovy您需要在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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM