I am working on a pipeline which requires a bash script to be executed in order to get some information from an API. I then need to get these values back into the Jenkinsfile to use them later. The big problem here is the multiple variables, as if it was one, I could just capture it through STDOUT.
The link to a solution commented here: How to set environment variables in Jenkins?, fails, as the answer has been deleted.
The solutions here: Is there any way to pass variables from bash script to Jenkinsfile without using extra plugins, use an extra file, and I would like to avoid that as much as possible. That answer is also 4 years old and I am unsure if there have been any features added to Jenkins that would allow for such transfer of variables.
            Asked
            
        
        
            Active
            
        
            Viewed 29 times
        
    0
            
            
         
    
    
        PythonSnek
        
- 542
- 4
- 21
1 Answers
1
            you are trying to to that in a tricky way, i dont think anyone from jenkins will implement this (transfer var from script to jenkins without tmp file creation)
ps you can use workarounds with stdout like:
pipeline {
  agent any
  stages {
    stage('mainstage') {
      steps {
        script {
          // ---- test.sh content ----
          // #!/bin/bash
          // my_first_variable="contentoffirstvar"
          // my_second_variable="contentofsecondvar"
          // echo "my_first_variable=$my_first_variable;my_second_variable=$my_second_variable"
          // -------
          def VARIABLES = sh (
              script: '/var/jenkins_home/test.sh',
              returnStdout: true
          ).trim().split(";")
          VARIABLES.each { var ->
              def (var_name, var_value) = var.split('=')
              env."${var_name}" = var_value
          }
        }
      }
    }
  }
}
as point for improvement you can add pattern like
===
my-var1=value1
my-var2=value2
===
and split + find by pattern as you want
 
    
    
        Ruslan
        
- 444
- 3
- 8
- 
                    What I ended up doing was variables separated by | and then in Jenkins splitting by | – PythonSnek Jul 29 '23 at 00:18