Jenkins error while setting build parameters from Jenkinsfile - jenkins

I am trying to set build parameter (String and password param) from the Jenkinsfile but I am getting following error and the build fails.
Caused by: java.lang.UnsupportedOperationException: PasswordParameterDefinition as a class hudson.model.ParameterDefinition could mean either hudson.model.PasswordParameterDefinition or com.michelin.cio.hudson.plugins.passwordparam.PasswordParameterDefinition
at org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:419)

Copy/pasted from https://issues.jenkins-ci.org/browse/JENKINS-18141:
In the example above, the DSL tries to find a subclass of hudson.model.ParameterDefinition named PasswordParameterDefinition. In your installation there are two classes named PasswordParameterDefinition, one defined by Jenkins itself and one provided by the Mask Passwords Plugin. The DSL can't decide which to use, so it generates the error.
If you have installed the Mask Passwords Plugin, you can use nonStoredPasswordParam to create a password parameter:
https://jenkinsci.github.io/job-dsl-plugin/#path/job-parameters-nonStoredPasswordParam

If you are calling your class as:
[$class: 'PasswordParameterDefinition', defaultValue: '', description: 'Vpn password', name: 'Psw']
try with this:
[$class: 'hudson.model.PasswordParameterDefinition', defaultValue: '', description: 'Vpn password', name: 'Psw']

Related

In Jenkins pipeline, how to set a value for an environment variable when loading a shared library?

I have a multi-branch pipeline that uses a Jenkinsfile to load a shared library defined in my system configuration.
#Library("my-shared-library") _
import com.company.exa.builders.BaseBuilder
import com.company.exa.builders.EdiBuilder
import hudson.model.*
buildNumbers = getBuildNumbers() // Function not shown, but it works
properties ([
disableConcurrentBuilds(),
[$class: 'jenkins.model.BuildDiscarderProperty',
strategy: [$class: 'LogRotator',
numToKeepStr: '50',
artifactNumToKeepStr: '20']],
parameters ([
choiceParam(name: "VERSION_CHOICE",
choices: buildNumbers,
description: "Version from Builds"),
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
description: "Passed-in version. Note this will override VERSION_CHOICE."),
booleanParam(name: "UPLOAD_ARTIFACTS",
defaultValue: false,
description: "Upload artifacts to file servers?"),
choiceParam(name: "DEBUG_LEVEL",
choices: ["0", "1", "2", "3"],
description: "Debug level; 0=less verbose, 3=most verbose")
])
])
When I run it clicking Scan Multibranch Pipeline Now, I get
00:00:01.018 Loading library my-shared-library
00:00:01.019 Attempting to resolve maser from remote references...
00:00:01.019 > git --version # timeout=10
00:00:01.023 > git --version # 'git version 2.17.1'
00:00:01.023 using GIT_SSH to set credentials Jenkins Master SSH
00:00:01.028 > git ls-remote -h -- git#bitbucket.org:cfouts-kmha/kmha-infrastructure.git # timeout=10
00:00:01.546 Found match: refs/heads//master revision a1bc1e273b41c4e892d7c25814d0f2a1c261f7e5
00:00:01.546 ERROR: Checkout failed
00:00:01.546 java.lang.IllegalArgumentException: Null value not allowed as an environment variable: VERSION_PASSEDIN
00:00:01.546 at hudson.EnvVars.put(EnvVars.java:379)
00:00:01.546 at hudson.model.StringParameterValue.buildEnvironment(StringParameterValue.java:59)
...complaining that variable VERSION_PASSEDIN is null. I've tried setting the VERSION_PASSEDINvariable to just "" in the following locations to no avail...
The multi-branch pipeline's Folder properties
The multi-branch pipeline's parent folder properties
In the Jenkinsfile itself
In the System configuration global properties
Any clues on how to fix this? I have a feeling it's something obvious that I'm not seeing.
Note that if I run the job with a branch's "Build with parameters" link, the job runs fine.
You have a chicken-and-egg problem when trying to set default value for parameters, e.g. here:
stringParam(name: "VERSION_PASSEDIN",
defaultValue: env.BRANCH_NAME,
To run your pipeline, Jenkins needs to figure out the value of the parameters. However, to figure out the value of the parameters, Jenkins needs to run your pipeline. See the problem?
To overcome this problem, here's what happens: Jenkins assumes the value of nothing for both VERSION_CHOICE and VERSION_PASSEDIN. In the first case, it's an empty choice; in the second, it's null.

Jenkins - Unable to access the parameter sent from one build pipeline in another build pipeline using "build job" command

I have a pipeline project say "A" which is string parameterized, and I am trying to call another build say "B" which is also string parameterized, using the command as follows :
build job: 'B', parameters: [[$class: 'StringParameterValue', name: 'tagg', value: "$env.tag"]]
The target is used to pass the parameter which was taken as input from A and assign use it in B.I tried to receive the parameter echo "$env.tagg" which gave null, echo "$tagg" gave no such parameter found error.
So how do I receive the parameters sent from A in B.
I think you need to let Jenkins know what env.tag is, try something like:
build job: 'B', parameters: [[$class: 'StringParameterValue', name: 'tagg', value: String.ValueOf($env.tag)]]

How to pass choice parameter to call a job inside jenkins pipeline

How can I pass choice parameters for the downstream job when called inside a stage in the jenkins pipeline?
I tried the below solutions but none worked:
stage('build job') {
steps{
script{
build job: 'test',
parameters: [
choice(choices: "option1\noption2\noption3\n", description: '', name: 'choiceParam')
]
}
}
}
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is using symbol ‘choice’
Tried these as well:
parameters:
[
[$class: 'ChoiceParameterValue', name: 'choiceParam', value: "1\n\2\n3\n"],
]
fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is named ChoiceParameterValue
I actually want to pass the choice parameter as a build parameter like "$choiceParam" for value so that I can just update the jenkins job configuration instead of always updating the values in the pipeline script
Can someone please help me with this
Thanks
When you are building a job via the Build step, you are kicking it off so you need to have "selected" a value.
In this instance you would pass in the desired 'String' choice. Not a list of choices. i.e. "1"
We create our list of params and then pass that in. So: our current job has these input params:
choice(name: 'ENV', choices: product, description: 'Env'),
choice(name: 'ENV_NO', choices: envParams(product), description: 'Env No'),
We pass these downstream by setting them:
List<ParameterValue> newParams = [
new StringParameterValue('ENV', params.ENV),
new StringParameterValue('ENV_NO', params.ENV_NO),
]
build(job: "job", parameters: newParams, propagate: false)

jenkins pipeline def new string parameter

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
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 !
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.

Error encountered with jenkins properties step

In my Jnekinsfile, I have the following steps:
properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'StringParameterDefinition', defaultValue: 'master', name: 'apiBranchName']]]])
sh "ruby ./build/script '${apiBranchName}'"
It works sometimes but in some other time it raises:
Groovy.lang.MissingPropertyException: No such property: apiBranchName for class: groovy.lang.Binding
Any idea?
I found the solution. I have several steps that require different parameters. So originally I define only those needed properties for each step. (for example, test step requires apiBranchName and deploy requires target. So I define only apiBranchName in tests and only target in deploy). That caused the issue.
Once I just define them all on each step it works out fine

Resources