簡體   English   中英

如何在Jenkins-groovy腳本中傳遞聲明為`def`的變量

[英]How to pass a variable declared as `def` inside Jenkins-groovy script

我試圖將變量sampleArray傳遞給groovyScript的 groovyScript activeChoiceReactiveParam不幸的是,生成的 xml 沒有選擇這些值。 我在這個操場上試過http://job-dsl.herokuapp.com以及真正的 Jenkins 並沒有成功。 我的意思是sampleArray的值沒有被復制。 請告訴我如何實現

job("try-to-pass-array") {
    def sampleArray = ["one","two","three","four"]
    description("this is to test a element type")
    keepDependencies(false)
    parameters {
        activeChoiceReactiveParam('NUMBERS') {
            description('Choose numbers for which build has to be generated')
            choiceType('MULTI_SELECT')
            groovyScript {
                script('return $sampleArray')
                fallbackScript('"fallback choice"')
            }
        }
    }
    disabled(false)
    concurrentBuild(false)
    steps {
        shell('''
              echo $NUMBERS
              ''')
    }
}

您沒有使用正確的字符串插值。 jenkins(聲明性管道)文檔有一個很好的例子。

def username = 'Jenkins'
echo 'Hello Mr. ${username}'
echo "I said, Hello Mr. ${username}"

會導致:

Hello Mr. ${username}
I said, Hello Mr. Jenkins

因此,如果您想傳遞變量的值,請始終使用"和 NOT '

tl;dr script("return ${sampleArray}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM