在Jenkins管道中将字符串转换为Groovy中的映射

时间:2019-07-05 02:42:29

标签: jenkins groovy jenkins-pipeline

我在Jenkins管道中有一个参数,看起来像

PROPERTY1=1\nPROPERTY2=1

当我在groovy playground中进行测试时,以下常规代码似乎可以正常工作

def original = "PROPERTY1=1\nPROPERTY2=1"

def map = 
    original
    .split('\n')
    .collectEntries { entry ->
        def pair = entry.split('=')
        [(pair.first()):pair.last()]
    }

println(map)    
println("PROPERTY1: " + map.get("PROPERTY1"))
println("PROPERTY2: " + map.get("PROPERTY2"))

我什至无法在Jenkins管道脚本中的新行上分割字符串

node {
    def ext_props = params.get('external_properties') ?: "PROPERTY1=0"
    println("ext_props: " + ext_props)
    println("Split String: " + ext_props.toString().split('\n').join(","))
    // def props = ext_props.split('\n').collectEntries { entry -> def pair = entry.split('='); [(pair.first()):pair.last()] }
    // println(props)
}

输出:

[Pipeline] {
[Pipeline] echo
ext_props: PROPERTY1=1\nPROPERTY2=1
[Pipeline] echo (hide)
Split String: PROPERTY1=1\nPROPERTY2=1
[Pipeline] } 

我想念什么?

1 个答案:

答案 0 :(得分:0)

您正在使用哪个版本的Jenkins? 在我的环境中,我可以看到预期的正确输出。

node {
    def ext_props = params.get('external_properties') ?: "PROPERTY1=0\nPROPERTY2=1"
    println("ext_props: " + ext_props)
    println("Split String: " + ext_props.toString().split('\n').join(","))
    // def props = ext_props.split('\n').collectEntries { entry -> def pair = entry.split('='); [(pair.first()):pair.last()] }
    // println(props)
}

我可以看到如下输出:

[Pipeline] {
[Pipeline] echo
ext_props: PROPERTY1=0
PROPERTY2=1
[Pipeline] echo
Split String: PROPERTY1=0,PROPERTY2=1
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS