[英]Global Variables in Jenkins Shared libraries
我有一個使用共享庫的Jenkinsfile。 我想創建一個全局變量,該變量可在共享庫的所有函數中使用,類似於params
對象。 但是我總是以
groovy.lang.MissingPropertyException: No such property: pipelineParams for class: groovy.lang.Binding
按照此准則 ,我在Jenkinsfile
定義一個Field
:
import org.apache.commons.io.FileUtils
@groovy.transform.Field
def pipelineParams
library identifier: 'pipeline-helper@master', retriever: modernSCM(
[$class: 'GitSCMSource',
remote: 'https://bitbucket/scm/jenkins/pipeline-helper.git',
credentialsId: 'bitbucket.service.user'
])
defaultCiPipelineMSBuild {
nodes = 'TEST-NODES' /* label of jenkins nodes*/
email = 'example@example.com' /* group mail for notifications */
msbuild = 'MSBUILD-DOTNET-4.6' /* ms build tool to use */
}
然后在defaultCiPipelineMSBuild
,設置pipelineParams
def call(body) {
// evaluate the body block, and collect configuration into the object
pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
...
稍后,我調用一個函數buildApplication
,它希望使用該變量:
def msBuildExe = tool pipelineParams.msbuild
您沒有嘗試自己創建一個全新的管道參數,而是嘗試將變量添加到可以在共享庫中使用的已經可用的env
參數中嗎?
env.param_name = "As per your requirement"
也可以通過共享庫中的env.param_name
或env[param_name]
訪問
正如@Dillip所建議的,即使對於非字符串也可以使用env
。 如果作為環境變量存儲的對象是列表或映射,則應反序列化
所以我稍微改變了管道代碼以將地圖存儲為環境變量
def call(body) {
// evaluate the body block, and collect configuration into the object
pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
env.pipelineParams = pipelineParams
pipeline {
...
管道已序列化,因此在讀取時返回String
{ nodes=TEST-NODES,email=example@example.com,msbuild=MSBUILD-DOTNET-4.6 }
因此,為了使用它必須反序列化
def pipelineParams =
// Take the String value between the { and } brackets.
"${env.pipelineParams}"[1..-2]
.split(', ')
.collectEntries { entry ->
def pair = entry.split('=')
[(pair.first()): pair.last()]
}
//use map
pipelineParams.msbuild
您可以將反序列化添加到函數中,以便也可以在其他地方使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.