詹金斯管道定义新的字符串参数 [英] jenkins pipeline def new string parameter

查看:72
本文介绍了詹金斯管道定义新的字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有管道的参数化作业. 例如:预定义的字符串参数: IP 我正在尝试在管道中定义新的String,以便在调用另一个构建作业"时将其用作新参数

I have a parameterized job with pipeline. for example: Predefined String Parameter: IP I'm trying to define a new String in the pipeline in order to use it as a new parameter when I'm calling to another "build job"

我尝试了以下方法:

import hudson.model.*
node('master'){
if(ipaddr =='192.168.1.1'){
    def parameter = new StringParameterValue("subnet", '255.255.255.0') //not working
    echo parameter //not working
}

stage ('Stage A'){
    build job: 'jobA', parameters:
 [
  [$class: 'StringParameterValue', name: 'ip', value: ip],
  [$class: 'StringParameterValue', name: 'subnet', value: subnet] //not working
 ]
}
}

这样,它不起作用,我得到了错误:

this way it's not working and I get the error:

不允许脚本使用新的hudson.model.StringParameterValue

Scripts not permitted to use new hudson.model.StringParameterValue

更改行后:

def parameter = new StringParameterValue("subnet", '255.255.255.0') 

收件人:

subnet = '255.255.255.0'

我得到了错误:

groovy.lang.MissingPropertyException:无此类属性:的子网掩码 类别:groovy.lang.Binding.

groovy.lang.MissingPropertyException: No such property: subnetmask for class: groovy.lang.Binding.

我无法使用预定义参数 ip 和新参数 subnet

I can't call to a new job with the predefined parameter ip and the new parameter subnet

没有子网,它可以正常工作

关于如何在管道中定义新的String参数的任何想法?

any idea of how can I define new String parameter in the pipeline?

詹金斯版本:2.19.4

jenkins version: 2.19.4

推荐答案

如果只避免实例化StringParameterValue,则可以使它正常工作,因为正如David M. Karr提到的管道沙箱非常严格.相反,只需在调用作业时使用简单变量即可,如下所示:

You can have it working if you just avoid instantiating StringParameterValue because as David M. Karr mentionned pipelines sandbox is pretty restrictive. Instead, just use your simple variable when calling your job, like this :

def subnet = ""
if(ipaddr == '192.168.1.1') {
    subnet = '255.255.255.0'
    echo subnet
}

stage ('Stage A'){
    build job: 'jobA', parameters:
    [
        [$class: 'StringParameterValue', name: 'ip', value: ipaddr],
        [$class: 'StringParameterValue', name: 'subnet', value: subnet]
    ]
}

这很简单,StringParameterValue参数期望传递字符串,因此,只要传递字符串值,就可以了!

It's pretty simple, StringParameterValue params expect String to be passed, so as long as you pass string values you should be just fine !

这篇关于詹金斯管道定义新的字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆