Discussion:
Jenkins Pipeline - loop over multi string variable with groovy
John
2018-04-04 21:24:47 UTC
Permalink
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 jenkinsci-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/3b411f4a-56f4-4c04-a9a4-6fde13f2239e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Viacheslav Dubrovskyi
2018-04-25 14:31:06 UTC
Permalink
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... )"
}
Post by 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.
|
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
/file/to/dir1
/file/to/dir2
/file/to/dir3
But when the Loop starts, it loops against each letter.. f, i, l, e, etc....
|
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
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 jenkinsci-users+***@googlegroups.com.
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.
Justin Zhang
2018-07-30 02:47:37 UTC
Permalink
Hi Slava,

I tried your way, found that the iteration of command only works on the
first loop,

For the second loop, it only execute the string. not the command with the
string.

Not sure if you have a way to fix it ?


圚 2018幎4月26日星期四 UTC+10䞊午12:31:14Slava Dubrovskiy写道
Post by Viacheslav Dubrovskyi
Hi
commitChangeset = sh( ... ).trim() will return string. You should
commitChangeset = sh( ... ).trim().tokenize(",") where delimiter is ","
And then no need to replace "," by " " in
tfvars = commitChangeset.replaceAll("terraform.tfvars", "")
for (int i = 0; i < commitChangeset.size() ; i++) {
int index=i
sh "(cd ${commitChangeset[index]}; etc... )"
}
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
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
/file/to/dir1
/file/to/dir2
/file/to/dir3
But when the Loop starts, it loops against each letter.. f, i, l, e, etc....
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
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 jenkinsci-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c430e07b-90d5-424a-b439-d13488ff7214%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...