Sunday, September 4, 2022

[SOLVED] Using array in Jenkins curl request

Issue

I have a Jenkins job (scripted pipeline) which have curl request inside "sh" block. This is an API call to Github organization, I need to use it for some repositories (about 15) in this organization. I can create 15 lines of curl requests, but I wonder if it's possible to pass repositories as arrays and add maybe a loop. Something like this:

def REPOS = ['repo1', 'repo2'] sh """ curl -d "mydata" -X POST 'www.website/api/{REPOS}' """

But I don't understand how to make it work with array and loop considering that I have Groovy + sh + curl


Solution

This will work for you.

steps {
    script {
      def repos = ['repo1', 'repo2']
      repos.each() {
      sh """ 
       echo $it
       echo "CURl here for ${it}"
       curl -d "mydata" -X POST 'https://www.website/api/${it}' 
      """
      }
    }

  }


Answered By - ycr
Answer Checked By - Mary Flores (WPSolving Volunteer)