Abderrahmane
Abderrahmane

Reputation: 453

Is there any way to pass variables from bash script to Jenkinsfile without using extra plugins

I'm trying to use variables declared in bash script in my Jenkinsfile (jenkins pipeline) without using extra plugins like EnvInject plugin please help, any idea will be appreciated

Upvotes: 0

Views: 246

Answers (2)

Dillip Kumar Behera
Dillip Kumar Behera

Reputation: 289

I have done it with something like this, you can store the variables inside Shell into a file inside workspace and then you are out of shell block, read the file in groovy to load the key value pair into your environment

Something like:

            def env_file = "${WORKSPACE}/shell_env.txt"
            echo ("INFO: envFileName = ${env_file}")
            def read_env_file = readFile env_file
            def lines = read_env_file.readLines()
            lines.each { String line ->
                def object = line.split("=")
                env.object[0] = object[1]
            }

Upvotes: 0

yong
yong

Reputation: 13712

you need to output those variables to a file like Property/Yaml file. Then use pipeline step readProperties / readYaml to read into Map in Jenkinsfile.

steps {
    sh'''
      ...

      AA=XXX
      BB=YYY

      set > vars.prop
    '''

    script {
        vars = readProperties file: 'vars.prop'

        env << vars // merge vars into env

        echo 'AA='+ env['AA']
    }
}

Upvotes: 1

Related Questions