arrays - 詹金斯管道将字符串推送到数组
问题描述
我在詹金斯有一个脚本:
stage('test') {
steps {
script {
def listCatalog = sh script: "ls src/examplecatalog", returnStdout: true
def arrayExample=[]
arrayExample+=("$listCatalog")
echo "${arrayExample}"
}
}
}
arrayExample
返回[catalog_1 catalog_2 catalog_3]
但它不是数组我认为它是字符串。我需要这样的数组:
['catalog_1', 'catalog_2', 'catalog_3']
.
如何在 Jenkins 中将字符串推送/附加到空数组?
解决方案
Jenkins DSL将返回一个字符串,您必须使用来自 groovy String 类 apish
的 split() 将其转换为数组,如下所示
node {
//creating some folder structure for demo
sh "mkdir -p a/b a/c a/d"
def listOfFolder = sh script: "ls $WORKSPACE/a", returnStdout: true
def myArray=[]
listOfFolder.split().each {
myArray << it
}
print myArray
print myArray.size()
}
结果将是
在示例中,我使用了一种方法将元素添加到数组中,但是您可以通过多种方法将元素添加到数组中,如下所示
所以在你的例子中,它将是
stage('test') {
steps {
script {
def listCatalog = sh script: "ls src/examplecatalog", returnStdout: true
def arrayExample=[]
listCatalog.split().each {
arrayExample << it
}
echo "${arrayExample}"
}
}
}
推荐阅读
- javascript - 我如何将 attr 从 choisen 选项标签更改为值?
- logging - NLog存档文件保留原始文件名
- javascript - 谷歌地图 API:使用 javascript 和 html 接受相同半径的搜索地址
- php - php: sql-injection > 无法使用prepared statement时如何防止?
- office365 - 在 Outlook for Mac 中发送邮件后扩展属性消失
- angular - 如何在 AngularFire 中使用 Google Analytics?
- python - 如何从python中的timedelta对象获取分钟和秒(mm:ss)
- javascript - 如何仅在构建中排除打字稿中的特定文件?
- android - 使用 url 下载图像并保存到内部存储
- bluetooth-lowenergy - 如何将数据从 esp32(BLE)发送到(与 arduino 接口的 HM-10)?