shelljenkinsjenkins-pipelinejenkins-groovygroovyshell

How To Pass a Groovy Array to Shell Script in Jenkins


I need to get the array that I have in my groovy Script and pass it shell Script for further Calculation in the Shell Script

I have tried multiple was but I am not getting the array passed to Shell Script.

templates = ["Temp1","Temp2","Temp3"]
templateCount = templates.size()
sh """
  count = ${templateCount} 
  temp = ${templates}
  for (( i=0; i < count; i++ ))
  do
    echo "Template Name = " ${temp[i]}
   done
"""

Solution

  • Try one of the following options.

    script {
        def templates = ["Temp1","Temp2","Temp3"]
        sh """
            for v in ${templates.join(' ')}
            do
                echo \$v
            done
        """
    
        // Option 2
        for(tmp in templates) {
            sh "echo ${tmp}"
        }
    }