问题描述
在用于管理多个作业的管道作业的Groovy代码中,是否可以读取在另一个作业配置中定义的参数的默认值?
也就是说,在管道作业A中,我想读取作业B中尚未安排的参数P的默认值。 (这将影响在作业A中如何构建B的请求。)
def val =一些随机前缀+
ReadJobParamDefault(job:'B',参数:'paramName')+
-some-random-suffix
def b = build(job:'B',
description:'Some text',
parameters:[[$ class:'StringParameterValue',
name:'paramName',
value:val]])
对于ReadJobParamDefault是否存在实际的内容?
解决方案 div> 我想可以将一些Groovy类存储到共享库中()做一些类似于
的内容
public static String ReadJobParamDefault(jobName,String parameter){
de f job = Hudson.instance.getJob(jobName)
String ret = null; ($!
)(job!= null&&& parameter_!= null&&& parameter.trim().length()> 0){
job.getProperties()。values()。每个{
if(it instanceof hudson.model.ParametersDefinitionProperty){
if(it.getParameterDefinition(parameter)!= null){
ret = it.getParameterDefinition(parameter)。
getDefaultParameterValue()。getValue();
}
}
}
}
return ret;
}
Within the Groovy code for a pipeline job which manages multiple jobs, is it possible to read the default value of a parameter defined in the configuration of another job?
That is, within pipeline job A, I want to read the default value of param P in job B, which hasn't been scheduled yet. (This will affect how the request to build B is constructed in job A.)
def val = "some-random-prefix" +
ReadJobParamDefault(job: 'B', parameter: 'paramName') +
"-some-random-suffix"
def b = build(job: 'B',
description: 'Some text',
parameters: [[$class: 'StringParameterValue',
name: 'paramName',
value: val]])
Is there something real for ReadJobParamDefault?
解决方案 I guess that it could be possible to store some Groovy class into a shared library (https://jenkins.io/doc/book/pipeline/shared-libraries/) doing something like
public static String ReadJobParamDefault(jobName, String parameter) {
def job = Hudson.instance.getJob(jobName)
String ret = null;
if (job != null && parameter != null && parameter.trim().length() > 0) {
job.getProperties().values().each {
if(it instanceof hudson.model.ParametersDefinitionProperty) {
if (it.getParameterDefinition(parameter) != null) {
ret = it.getParameterDefinition(parameter).
getDefaultParameterValue().getValue();
}
}
}
}
return ret;
}
这篇关于Jenkins Pipeline Groovy:从另一个工作中读取默认参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!