Skip to content

Instantly share code, notes, and snippets.

@solvingj
Last active June 14, 2020 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solvingj/94927c0dd13fbf2cb397e7d82b2e0a22 to your computer and use it in GitHub Desktop.
Save solvingj/94927c0dd13fbf2cb397e7d82b2e0a22 to your computer and use it in GitHub Desktop.
Iterable withIndex behavior discrepancy on Jenkins
// Run this in a Jenkins step
String jsonStr = "['zero', 'one', 'two']"
JSONArray jsonArray = new JsonSlurper().parseText(jsonStr) as JSONArray
jsonArray.withIndex().each{Object str, Integer idx -> currentBuild.echo("${idx.toString()} : ${str}")}
// OUTPUT from jenkins
// [Pipeline] echo
// null : [zero, 0]
// [Pipeline] echo
// null : [one, 1]
// [Pipeline] echo
// null : [two, 2]
// Run this locally in a groovy file or unit test
String jsonStr = "['zero', 'one', 'two']"
JSONArray jsonArray = new JsonSlurper().parseText(jsonStr) as JSONArray
jsonArray.withIndex().each{Object str, Integer idx -> println("${idx.toString()} : ${str}")}
// OUPUT Local
// 0 : zero
// 1 : one
// 2 : two
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment