詹金斯管道将字符串推送到数组
jenkins pipeline push string to array
我在 Jenkins 中有一个脚本:
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 中 push/append 将字符串转换为空数组?
sh
Jenkins DSL 将 return 一个字符串,您必须使用 groovy String class api 中的 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()
}
结果将是
在示例中,我使用了一种方法将元素添加到数组,但是您可以通过多种方法将元素添加到数组,例如 this
所以在你的例子中,它将是
stage('test') {
steps {
script {
def listCatalog = sh script: "ls src/examplecatalog", returnStdout: true
def arrayExample=[]
listCatalog.split().each {
arrayExample << it
}
echo "${arrayExample}"
}
}
}
我在 Jenkins 中有一个脚本:
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 中 push/append 将字符串转换为空数组?
sh
Jenkins DSL 将 return 一个字符串,您必须使用 groovy String class api 中的 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()
}
结果将是
在示例中,我使用了一种方法将元素添加到数组,但是您可以通过多种方法将元素添加到数组,例如 this
所以在你的例子中,它将是
stage('test') {
steps {
script {
def listCatalog = sh script: "ls src/examplecatalog", returnStdout: true
def arrayExample=[]
listCatalog.split().each {
arrayExample << it
}
echo "${arrayExample}"
}
}
}