Search code examples
jenkinsgroovyjenkins-pipeline

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, e.g.

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?


Solution

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