0

Scenario:

I have a for-in loop written in groovy in Jenkinsfile. For every iteration of this loop i want to assign a value from within a shell script which runs(aws cli commond) on every iteration to a Groovy list/map variable outside the loop.

Example code snippet:

def functionName() {
  
   Groovy variable(list/map type);

   for in loop {
    
      sh """
          Groovy variable = value
      """
   
   } 
 

}

Can anyone provide an example or explain how can this be done? Any help is appreciated

Ashay Fernandes
  • 353
  • 4
  • 14
  • 2
    `groovy_variable = sh(script: "echo value", returnStdout: true).toString().trim()` – Arnaud Valmary Nov 28 '21 at 14:42
  • So the entire script should be assigned to the groovy_variable and echo only the value which I need is it? Because I have other aws cli commands running inside the script. – Ashay Fernandes Nov 28 '21 at 14:46
  • The entire script output should be assigned to only one variable (all command output) – Arnaud Valmary Nov 28 '21 at 15:47
  • Does this answer your question? [How to access Shell variable value into Groovy pipeline script](https://stackoverflow.com/questions/68748267/how-to-access-shell-variable-value-into-groovy-pipeline-script) – Gerold Broser Nov 28 '21 at 23:18
  • Yes it does, but what happens if there are multiple echo inside the script? I'm logging few error messages as well from the shell script. Is there any way I can pick one particular echo value? Please consider me a beginner in this. – Ashay Fernandes Nov 29 '21 at 03:25
  • 1
    AshayFernandes Redirect all `echo`s to a log file. `tee` the particular one to the log file (and `stdout` implicitely). See also [How to return stdout and stderr together with the status from a Jenkins Pipeline sh script step](https://stackoverflow.com/q/68967642/1744774). – Gerold Broser Nov 29 '21 at 15:39

0 Answers0