简体   繁体   English

如何将变量传递给在 Jenkins 管道参数中执行的 Groovy 脚本?

[英]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.我有一个 consul 密钥 AAA/BBB/测试密钥,如“1、2、3”,AAA/CCC/测试密钥,如“4、5、6”等。

I have a shared Jenkinsfile between several jobs.我在几个作业之间有一个共享的 Jenkinsfile。

I do not want to make Jenkinfile per each job.我不想为每项工作制作 Jenkinfile。 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如果我在 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
    ]
])])
}

However, when I try to use env.JOB_NAME inside the URL, it does not work:但是,当我尝试在 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
    ]
])])
}

How can I access env variables inside the choice parameter defined with the Groovy script?如何访问 Groovy 脚本定义的 choice 参数内的环境变量?

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:如果要将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      
            """
        ]

Yes after changing ''' to """,my issue got resolved. And also by using ${abc}是的,将 ''' 更改为 """ 后,我的问题得到了解决。也可以使用 ${abc}

Thanks @szymon-stepniak but ${env.JOB_NAME} does not work in ChoiceParameter感谢@szymon-stepniak 但 ${env.JOB_NAME} 在 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
    ]
])])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将参数传递给jenkins管道中的powershell脚本 - How to pass a parameter to a powershell script in a jenkins pipeline 如何使用 Groovy 集成 Jenkins 管道作业并传递动态变量? - How to integrate Jenkins pipeline jobs and pass dynamic variables using Groovy? 带有未定义变量的Jenkins Pipeline / Groovy Script - Jenkins Pipeline / Groovy Script with undefined variables 将jenkins管道中的参数传递给内部groovy脚本 - Pass parameters in jenkins pipeline to internal groovy script 如何在Jenkins管道中将groovy变量传递给powershell? - How to pass groovy variable to powershell in Jenkins pipeline? Jenkins:如何在管道中运行Groovy脚本 - Jenkins : how to run groovy script in pipeline 如何为 Jenkins 管道有效地开发 groovy 脚本? - How to develop groovy script effectively for Jenkins pipeline? 将参数传递到 Jenkins 管道中的 Powershell 脚本 - Pass a parameter into a Powershell script in Jenkins Pipeline Groovy:Jenkinsfile:无法将带空格的参数传递给 jenkins 管道中的 shell 脚本 - Groovy: Jenkinsfile: Unable to pass argument with space to shell script in jenkins pipeline Jenkins 声明式管道:如何从“主动选择反应引用参数的 Groovy 脚本”调用函数? - Jenkins Declarative Pipeline: How to call functions from "Active choices reactive reference parameter's Groovy script"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM