Hi

commitChangeset = sh( ... ).trim() will return string. You should transform it to list, for example:

commitChangeset = sh( ... ).trim().tokenize(",") where delimiter is ","

And then no need to replace "," by " " in

|tfvars = commitChangeset.replaceAll("terraform.tfvars", "")|

And then you can use cycle for iterate list:
for (int i = 0; i < commitChangeset.size() ; i++) {
    int index=i
    sh "(cd ${commitChangeset[index]}; etc... )"
}



05.04.2018 00:24, John пишет:
I'm trying to loop over a var that contains multiple strings (file paths), and then run a shell script against each one. But for some reason everything i try loops over every single letter of the string. Here is my code:

|
def call(Map config) {

  node('terraform-slave') {
    cleanWs()
    checkout scm

    stage('Plan') {
      commitChangeset = sh(returnStdout: true, script: 'git diff-tree --no-commit-id --name-only -r HEAD').trim()
    }
    stage('EchoToVerify') {
      tfvars = commitChangeset.replaceAll("terraform.tfvars", "")
      echo tfvars
    }
    stage('Loop') {
      loop(tfvars)
    }
  }
}

def loop(list) {
  list.each {
    sh "(cd ${it}; cat terraform.tfvars)"
  }
}

|


If you see the EchoToVerify stage, that works fine. Multiple file paths are echo'ed, like so:

/file/to/dir1
/file/to/dir2
/file/to/dir3

But when the Loop starts, it loops against each letter.. f, i, l, e, etc....

The only way I have been able to get it work is is by doing this:

|
def loop(list) {
  for (dir in [list]) {
    sh "(cd ${it}; cat terraform.tfvars)"
  }
}
|

However it only works when there is one value in the variable. It seems to want to iterate over ${it} immediately, before going to the next shell command (cat terraform.tfvars). It seems like it tries to "cd" to the next directory immediately.

Thanks for the help!




--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>. To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3b411f4a-56f4-4c04-a9a4-6fde13f2239e%40googlegroups.com <https://groups.google.com/d/msgid/jenkinsci-users/3b411f4a-56f4-4c04-a9a4-6fde13f2239e%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
WBD,
Viacheslav Dubrovskyi

--
You received this message because you are subscribed to the Google Groups "Jenkins 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/624688ff-7f85-0e9f-8547-e841be7e983c%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to