簡體   English   中英

如何將變量傳遞給在 Jenkins 管道參數中執行的 Groovy 腳本?

[英]How to pass variables into Groovy script executed in Jenkins pipeline parameter?

我有一個 consul 密鑰 AAA/BBB/測試密鑰,如“1、2、3”,AAA/CCC/測試密鑰,如“4、5、6”等。

我在幾個作業之間有一個共享的 Jenkinsfile。

我不想為每項工作制作 Jenkinfile。 我想按作業名稱訪問密鑰,但無法正常工作。

如果我在 URL 中硬編碼密鑰,它會起作用,例如

node('master') {
properties([parameters([
    [   $class: 'ChoiceParameter',
        name: 'GROUPS',
        description: 't2',
        randomName: 't3',
        script: [
            $class: 'GroovyScript', 
            fallbackScript: [
                classpath: [], sandbox: false, script: ''
            ],
            script: [   
                classpath: [], sandbox: false, script:
                '''
                    def text = new URL('http://consul.local:8500/v1/kv/AAA/BBB/test-key?raw').getText()
                    return text.split(",").collect{ (it=~/\\d+|\\D+/).findAll() }.sort().collect{ it.join() } as List      
                '''
            ]
        ],
        choiceType: "PT_RADIO", //PT_SINGLE_SELECT,PT_MULTI_SELECT,PT_RADIO,PT_CHECKBOX
        filterable: true, 
        filterLength: 1
    ]
])])
}

但是,當我嘗試在 URL 中使用env.JOB_NAME時,它不起作用:

node('master') {
properties([parameters([
    [   $class: 'ChoiceParameter',
        name: 'GROUPS',
        description: 't2',
        randomName: 't3',
        script: [
            $class: 'GroovyScript', 
            fallbackScript: [
                classpath: [], sandbox: false, script: ''
            ],
            script: [   
                classpath: [], sandbox: false, script:
                '''
                    def text = new URL('http://consul.local:8500/v1/kv/AAA/'+ env.JOB_NAME + '/test-key?raw').getText()
                    return text.split(",").collect{ (it=~/\\d+|\\D+/).findAll() }.sort().collect{ it.join() } as List      
                '''
            ]
        ],
        choiceType: "PT_RADIO", //PT_SINGLE_SELECT,PT_MULTI_SELECT,PT_RADIO,PT_CHECKBOX
        filterable: true, 
        filterLength: 1
    ]
])])
}

如何訪問 Groovy 腳本定義的 choice 參數內的環境變量?

如果要將env.JOB_NAME傳遞給腳本內容,則必須將'''替換為"""並使用${env.JOB_NAME}引用變量。像這樣:

        script: [   
            classpath: [], sandbox: false, script:
            """
                def text = new URL('http://consul.local:8500/v1/kv/AAA/${env.JOB_NAME}/test-key?raw').getText()
                return text.split(",").collect{ (it=~/\\d+|\\D+/).findAll() }.sort().collect{ it.join() } as List      
            """
        ]

是的,將 ''' 更改為 """ 后,我的問題得到了解決。也可以使用 ${abc}

感謝@szymon-stepniak 但 ${env.JOB_NAME} 在 ChoiceParameter 中不起作用

下一個代碼運行良好

node('master') {
properties([parameters([
    [   $class: 'ChoiceParameter',
        name: 'GROUPS',
        description: 't2',
        randomName: 't3',
        script: [
            $class: 'GroovyScript', 
            fallbackScript: [
                classpath: [], sandbox: false, script: ''
            ],
            script: [   
                classpath: [], sandbox: false, script:
                """
                    def build = Thread.currentThread().toString()
                    def regexp= ".+?/job/(.*?)/build "
                    def match = build  =~ regexp
                    def jobName = match[0][1].replaceAll("/job/", "/") as String
                    def text = new URL('http://consul.local:8500/v1/kv/${jobName}/test-key?raw').getText()
                    return text.split(",").sort() as List      
                """
            ]
        ],
        choiceType: "PT_RADIO", //PT_SINGLE_SELECT,PT_MULTI_SELECT,PT_RADIO,PT_CHECKBOX
        filterable: true, 
        filterLength: 1
    ]
])])
}

暫無
暫無

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

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