Home > Blockchain >  How to read value from config file (groovy script) in Jenkins?
How to read value from config file (groovy script) in Jenkins?

Time:07-12

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. enter image description here

I want to read that user info (line 2) enter image description here

anyone have ideas ?

thank you.

CodePudding user response:

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 = "[email protected]"
}

return this
  • Related