如何在Jenkins Groovy Pipeline中创建和循环遍历字符串ArrayList [英] How to create and loop over an ArrayList of strings in Jenkins Groovy Pipeline

查看:1202
本文介绍了如何在Jenkins Groovy Pipeline中创建和循环遍历字符串ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我正在尝试遍历Jenkins Groovy Pipeline脚本中的字符串ArrayList(使用脚本化Pipeline语法).让我为您列出整个问题".

我从一串用空格分隔的文件系统位置开始:"/var/x/var/y/var/z ...",就像这样.我遍历此字符串,将每个字符添加到临时string中.然后,当我到达一个空格时,将该临时字符串添加到数组中并重新启动.这是一些显示我如何执行此操作的代码:

def full_string = "/var/x /var/y /var/z"
def temp = ""
def arr = [] as ArrayList
full_string.each {
    if ( "$it" == " " ) {
      arr.add("$temp")           <---- have also tried ( arr << "$temp" )
      temp = ""
    } else {
      temp = "$temp" + "$it"
    }
}
 // if statement to catch last element

看到的问题是,如果我以后遍历数组,它决定遍历每个单独的字符,而不是遍历整个/var/x字符串.

/p>

我是Groovy的新手,因此在构建此管道时一直在学习.如果有帮助,请使用Jenkins 2.190.1版.我浏览了SO和Groovy文档,以及Jenkins上的管道语法文档.似乎找不到我一直在寻找的东西.我确定我的解决方案不是最优雅或最有效的解决方案,但是在尝试从中获得最大性能之前,我将首先了解它的工作原理.

我发现了这个问题,但同样没有帮助:动态地将元素添加到ArrayList在Groovy中.

编辑:我正在尝试将旧公司的c-shell构建脚本转换为Jenkins Pipelines.我的初始字符串是在我们所有节点上都可用的环境变量,我也需要在管道内部使用该变量.

TL; DR-我需要能够从字符串中用空格分隔的值创建一个数组,然后能够遍历所述数组,并且每个元素"都是完整的字符串,而不是单个字符,这样我可以正确运行管道步骤.

解决方案

尝试在Jenkins脚本控制台(your.jenkins.url.yourcompany.com/script)中运行它:

def full_string = "/var/x /var/y /var/z"
def arr = full_string.split(" ")
for (i in arr) {
  println "now got ${i}"
}

结果:

now got /var/x
now got /var/y
now got /var/z

As stated in the title, I'm attempting to loop over an ArrayList of strings in a Jenkins Groovy Pipeline script (using scripted Pipeline syntax). Let me lay out the entire "problem" for you.

I start with a string of filesystem locations separated by spaces: "/var/x /var/y /var/z ... " like so. I loop over this string adding each character to a temp string. And then when I reach a space, I add that temp string to the array and restart. Here's some code showing how I do this:

def full_string = "/var/x /var/y /var/z"
def temp = ""
def arr = [] as ArrayList
full_string.each {
    if ( "$it" == " " ) {
      arr.add("$temp")           <---- have also tried ( arr << "$temp" )
      temp = ""
    } else {
      temp = "$temp" + "$it"
    }
}
 // if statement to catch last element

See, the problem with this is that if I later go to loop over the array it decides to loop over every individual char instead of the entire /var/x string like I want it to.

I'm new to Groovy so I've been learning as I build this pipeline. Using Jenkins version 2.190.1 if that helps at all. I've looked around on SO and Groovy docs, as well as the pipeline syntax docs on Jenkins. Can't seem to find what I've been looking for. I'm sure that my solution is not the most elegant or efficient, but I will settle for understanding how it works first before trying to squeeze the most performance out of it.

I found this question but this was similarly unhelpful: Dynamically adding elements to ArrayList in Groovy.

Edit: I'm trying to translate old company c-shell build scripts into Jenkins Pipelines. My initial string is an environment variable available on all our nodes that I also need to have available inside the Pipeline.

TL;DR - I need to be able to create an array from space separated values in a string, and then be able to loop over said array and each "element" be a complete string instead of a single char so that I can run pipeline steps properly.

解决方案

Try running this in your Jenkins script console (your.jenkins.url.yourcompany.com/script):

def full_string = "/var/x /var/y /var/z"
def arr = full_string.split(" ")
for (i in arr) {
  println "now got ${i}"
}

Result:

now got /var/x
now got /var/y
now got /var/z

这篇关于如何在Jenkins Groovy Pipeline中创建和循环遍历字符串ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆