Error encountered with jenkins properties step - jenkins

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

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 configure 2 jenkins pipeline trigger with environment variable

I have to pass parameter to a timeTrigger function in jenkins pipelineTrigger, but it's not taking that parameter. I have to schedule 2 builds, one for Dev every 8 hours, other for staging every 12 hours.
Question1: Can I trigger 2 builds this way?
Question2: How to pass parameter to specify environment?
See Code below
environmentParam = ['dev', 'qa', 'stg']
originParam = ['Automatic', 'Manual']
if (env.BRANCH_NAME == "master") {
properties([
[$class : 'ParametersDefinitionProperty',
parameterDefinitions:
[
[$class : 'ChoiceParameterDefinition',
choices : environmentParam.join('\n'),
description: 'Environment to run the Integration tests',
name : 'environment'
],
[$class : 'ChoiceParameterDefinition',
choices : originParam.join('\n'),
description: 'The execution of this job was Automatic or Manual',
name : 'origin'
]
]
],
pipelineTriggers(
[
[
$class: 'TimerTrigger', spec: 'H */12 * * *', environment: 'stg'
],
[
$class: 'TimerTrigger', spec: 'H */8 * * *', environment: 'dev'
]
]
),
disableConcurrentBuilds()
])
}
above code is not taking up environment, it is only triggering second entry in pipelineTriggers, not both. :-(
Answer for Questions 1: No, you cannot do it now.
The properties will be set to the jenkins job (config.xml) when pipeline script is executed. Therefore if multiple time trigger is not supported in normal jenkins job, your script will no work as expected as well.
Please execute it and check the jenkins job configuraion and config.xml to understand the result.
Suggestion
Change your solution in another logic (if it is still 8 & 12 hours)
trigger your job to run every 4 hours
keep both choice parameters.
try to have some logical check in steps to run the different stage in different time.
i think you'll have a better time if you create either two or three build plans instead of one.
if you created two, there would be one for dev and one for staging, and they would each have their own trigger and do the appropriate deployment.
if you had three, there would be one (let's call it the "deployment" build plan) that did deployments and that took an TARGET_ENVIRONMENT parameter or similar (with value dev or stg). The other two would have the triggers and they would each call the deployment build plan to do the actual work. you can call another build plan from a Jenkinsfile like this:
build job: 'deployment', parameters: [string(name: 'TARGET_ENVIRONMENT', value: 'dev')]

Jenkins error while setting build parameters from Jenkinsfile

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

COPYARTIFACT_BUILD_NUMBER_SUFFIX in workflow jobs

Does the COPYARTIFACT_BUILD_NUMBER_SUFFIX work on expected lines in workflow/pipeline jobs?
copy artifact command am using is
step([$class: 'CopyArtifact', filter: '*.rpm', fingerprintArtifacts: true, projectName: 'test_pkg', resultVariableSuffix: 'testsuffix', selector: [$class: 'ParameterizedBuildSelector', parameterName: 'testparam'], target: 'test_packages'])
when i do
println COPYARTIFACT_BUILD_NUMBER_TESTSUFFIX
am getting the below error
groovy.lang.MissingPropertyException: No such property: COPYARTIFACT_BUILD_NUMBER_TESTSUFFIX for class: WorkflowScript
I am also having the same problem. What I (and I guess KishorePP too) want to achieve is, to retrieve the BuildNumber from the build that generated the artifacts.
But the environment variable is somehow not existent or differently named.
The plugin I use is this: https://wiki.jenkins-ci.org/display/JENKINS/Copy+Artifact+Plugin
As in source Code of https://github.com/jenkinsci/copyartifact-plugin/blob/master/src/main/java/hudson/plugins/copyartifact/CopyArtifact.java#L739 we can see that there the Variable is pushed into the env map.
Try doing a printenv in your shell script.

Resources