jenkins pipeline def new string parameter

◇◆丶佛笑我妖孽 提交于 2021-02-08 07:27:31

问题


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"

I have tried the following method:

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:

Scripts not permitted to use new hudson.model.StringParameterValue

after changing the line:

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

to:

subnet = '255.255.255.0'

I got the error:

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

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

without the subnet it's working

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

jenkins version: 2.19.4


回答1:


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

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




回答2:


By default, the sandbox the pipeline job executes in is very restricted. You have to override the security restrictions when you find them. Go to "Manage Jenkins" and "In-process Script Approval". You should see in the list the last violation that occurred. Select it for approval and rerun your script.



来源:https://stackoverflow.com/questions/41088936/jenkins-pipeline-def-new-string-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!