如何从Jenkins管道中的for循环中获取字符串值?

jei2mxaa  于 2023-08-03  发布在  Jenkins
关注(0)|答案(1)|浏览(94)
def loopCount = No_Of_TEST.toInteger()

for (int i = 1; i <= TEST; i++) {
    def paramInput = input(
        id: "paramInput-${i}",
        message: "Enter TEST${i} Value",
        parameters: [
            string(name: "Control_IP_${i}", defaultValue: '10.0.0.0', description: "Control IP")
                            
        ]
    )
}

字符串
我已经试过了

sh "echo  ${params.Control_IP_${i}}"


也试过:

env.Control_IP = params["Control_IP_${i}"]

echo "${env.Control_IP}"


但是在echo之后得到的是null。

nkoocmlb

nkoocmlb1#

每次重新指定paramInput变量params-存储初始参数而不是动态env-仅环境变量
你可以使用hack:

int TEST = 3

Map inputParams = [:]
for (int i = 1; i <= TEST; i++) {
    def paramInput = input(
        id: "paramInput-${i}",
        message: "Enter TEST${i} Value",
        parameters: [
            string(name: "Control_IP_${i}", defaultValue: '10.0.0.0', description: "Control IP"),
            // string(name: "Control_IP_${i}2", defaultValue: '10.0.0.0', description: "Control IP")
        ]
    )

    // if several params passed it will be map
    if (paramInput instanceof Map) {
      paramInput.each { name,value ->
        inputParams[name] = value
      }
    // owerwise string 
    } else {
      // if you have only one param you can remove entire if and have only this line
      inputParams["Control_IP_${i}"] = paramInput
    }
}

// to display all 
for (int i = 1; i <= TEST; i++) {
  println(inputParams["Control_IP_${i}"])
}

字符串
取决于你的管道inputParams不能在函数中解析,如果你有任何,所以你需要添加

import groovy.transform.Field

@Field
Map inputParams = [:]


如果您希望使用全局变量

相关问题