[英]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.