0

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

j.doe
  • 662
  • 4
  • 19
Abderrahmane
  • 385
  • 2
  • 3
  • 14
  • You could execute the script and have them create environment variables which you can use inside the Jenkinsfile. – Siri Jul 02 '19 at 07:20

2 Answers2

1

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']
    }
}
yong
  • 13,357
  • 1
  • 16
  • 27
  • Cant you just have them stored as environment variables? – Siri Jul 02 '19 at 07:22
  • i have bash script that contains a lot of vars, so i can't declare these vars manually, if there is a way to create pipeline env vars which will contain the values of my vars declared in the bash script i will be happy – Abderrahmane Jul 02 '19 at 11:03
  • There is no straight way can do that. One way is like what I did above. You can add `set > vars.prop` to the end of your bash script. then execute the bash script as `sh './'` – yong Jul 02 '19 at 12:51
0

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]
            }