简体   繁体   中英

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

I have a consul keys AAA/BBB/test-key like '1,2,3', AAA/CCC/test-key like '4,5,6' and others.

I have a shared Jenkinsfile between several jobs.

I do not want to make Jenkinfile per each job. I want to access keys by job name but I can't make it working.

It works if I hardcode key in the URL, eg

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
    ]
])])
}

However, when I try to use env.JOB_NAME inside the URL, it does not work:

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
    ]
])])
}

How can I access env variables inside the choice parameter defined with the Groovy script?

If you want to pass env.JOB_NAME to the script content you have to replace ''' with """ and refer to the variable with ${env.JOB_NAME} . Something like this:

        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      
            """
        ]

Yes after changing ''' to """,my issue got resolved. And also by using ${abc}

Thanks @szymon-stepniak but ${env.JOB_NAME} does not work in ChoiceParameter

Next code works well

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
    ]
])])
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM