Jenkinsfile 中的循环问题

Issue with loop in Jenkinsfile

我的 Jenkinsfile 中有一个变量,其中包含 URL 的列表,我希望能够 运行 覆盖它们。当我将变量 $URL 传递给函数时,出现错误:

No such property: $URL for class: groovy.lang.Binding 

但是,我可以用 sh 来回应这个变量。

pipeline {
        agent any
        environment {
            URL="https://www.aaa.com," \
            + "https://www.bbb.com," \
            + "https://www.ccc.com"
        }
        stages {
            stage ('A') {
                //...
            }
            stage ('B') {
                //...
            }
            stage ('C') {
                steps {
                    script {
                        sh 'echo $URL'
                        funcion($URL)
                    }
                }
            }
        }
    }



    def funcion(URL) {
        sh "echo Going to echo a list"
        for (int i = 0; i < URL.size(); i++) {
            sh "echo ${URL[i]}"
        }
    }

可能是什么问题?

您应该将此变量传递给 funcion() 方法,如

function(URL)

而不是

funcion($URL)

美元符号 $ 仅在 GString 内使用,当您想要插入变量时。例如

#!groovy

def name = "Joe"
println "My name is $name"

结果

My name is Joe

您可以在 Groovy 文档中阅读有关字符串插值的更多信息 - http://groovy-lang.org/syntax.html#_string_interpolation