简体   繁体   中英

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.

downloaded plugin "Config File Provider Plugin(ver.3.10.0)"

and i create config file.在此处输入图像描述

I want to read that user info (line 2)在此处输入图像描述

anyone have ideas ?

thank you.

Here is how you can use the Config File Provider with a Groovy script. 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. 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

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.

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