简体   繁体   English

如何从 Jenkins 中的配置文件(groovy 脚本)中读取值?

[英]How to read value from config file (groovy script) in Jenkins?

I want to read some value(ex. user Login info) from config file in Jenkins pipeline script.我想从 Jenkins 管道脚本中的配置文件中读取一些值(例如用户登录信息)。

downloaded plugin "Config File Provider Plugin(ver.3.10.0)"下载的插件“配置文件提供程序插件(ver.3.10.0)”

and i create config file.我创建配置文件。在此处输入图像描述

I want to read that user info (line 2)我想阅读该用户信息(第 2 行)在此处输入图像描述

anyone have ideas ?有人有想法吗?

thank you.谢谢你。

Here is how you can use the Config File Provider with a Groovy script.以下是如何将Config File Provider与 Groovy 脚本一起使用。 First, you have to load it to your pipeline and then execute it.首先,您必须将其加载到您的管道中,然后执行它。 So for that, you have to restructure your Groovy script as well.因此,您还必须重组 Groovy 脚本。 Please check the below.请检查以下内容。

Jenkins Pipeline詹金斯管道

pipeline {
    agent any
    stages {
        stage('ConfigTest') {
                
            steps {
                configFileProvider([configFile(fileId: '078d4943-231c-4156-9b88-8334cd8a9402', variable: 'GroovyScript')]) {

                    echo " =========== Reading Groovy Script"
                    script {
                        def script = load("$GroovyScript")
                        script.setProperties()
                        echo "${USER_ID}"
                    }
                }
            }
        }
    }
}

Content of the Script脚本内容

import groovy.transform.Field

@Field def USER_ID;
  
def setProperties() {
    USER_ID = "abcd@gmail.com"
}

return this

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

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