Jenkins Parameterized Trigger Plugin passing value as "VALUE" - jenkins

I am using "Parameterized Trigger Plugin" to trigger child job. I am passing predefined parameter as NUMBER=$VERSION_NUMBER and child job failed saying FILE_NAME."12345" do not exist. I went to location and found property file is there and realized that child job is searching properties file as FILE_NAME."12345"(FILE_NAME.$NUMBER) and actual name of file is FILE_NAME.12345, So root cause seems like that pligin is passing value with "".
Please suggest if there is a way to pass value without "".
Thanks !!

try using ${NUMBER} instead of $NUMBER

Related

Issue with Generic Webhook Trigger plugin in Jenkins

I am trying to use Generic Webhook Trigger plugin in Jenkins to trigger build in case any PR is raised on my GitHub repo. For starters I defined a variable "current_status" mapping it to "action" field within the json payload to be received from GitHub. While the build is getting triggered on raising a PR but the value for current_status is coming as null. The content-type for my GitHub webhook is "application/json"
The GitHub payload generated against the PR event has action field in it :
"action": "opened",
But when I try to print this variable using println "${params.current_status}" in my pipeline, the value is printed as null.
Also when I try to execute a step based on the value of the variable using
when {
expression { return params.current_status == "opened" }
}
the stage is skipped even though the value as per the action in the GitHub payload is "opened"
For debugging the issue when I selected the option to print the contributed variables in the job log I could see value of the current_status value as opened
But when I refer this variable in my pipeline its value comes out to be null somehow.
As a workaround made my pipeline parmeterized, using the same name for the variable as the one defined in the Generic Webhook Trigger Plugin section (current_status) and then referred to it within my Jenkinsfile and it worked.(the value for the variable reflected the value received in the json payload from GitHub).
Finally I found the solution. For anyone who might be facing the same issue you can refer to the variable defined within the Generic Webhook Trigger plugin directly as a Groovy variable. In my case I tried to the use the variable current_status directly without referrring it via params and it worked as expected.
The other approach of defining the variable via parmeterized pipeline might be helpful wherein we would want to run the build manually.

Jenkins Pass custom variable to downstream jobs

I want to pass a custom variable from Job A to Job B. I have tried achieving this using the "Parameterized Trigger" plugin but I didnt work for me.
I am doing it the following way:
On Job A:
execute shell --> export VAR=1
echo $VAR --> is returning 1
Trigger parameterized build on other projects:
PARAM=${VAR}
On JobB:
I have selected this project is parameterized and declared a variable as PARAM. But when I do execute shell --> echo ${PARAM} it returns be ${VAR} instead of 1.
Am I missing anything here ? Any pointers please ? Thanks in advance!!
That's due to the lifetime of VAR is limited within the Execute shell step. If you want variable cross step, even cross from Build to Post Action, you can output the variable to a file in Key = Value pattern, then read it back in Parameterized Trigger
I found a neat way to pass custom variables to downstream job here:
https://sathishpk.wordpress.com/2016/03/01/how-to-passget-parameters-from-shell-command-into-jenkins-other-places/
It worked for me!!

Cannot receive parameter from Script content to Predefined Parameter in Jenkins

I would like to pass hostname parameter which is declared at Script Content to Predefined Parameter in Trigger/calls on build other project where my child project will receive parameter from the parent project.My code looks like this in Script Content:
`machine_name="$(hostname)"`
So in order to pass my parameter to the child project I declared:
host_name=${machinename}
in Script Content .But when I check in my child project it display as ${machinename} which is not I want .Can someone tell me what am I missing or what step that I done wrong or is there any way to perform this ?
Try using ENv Inject plugin, all you need to do is below:
Your script should contain below step:
machine_name="$(hostname)" > inject.txt
Now use Inject environment variables build step and in
property file path give inject.txt
what's the use?
By this step, now you machine_name variable holds the hostname value throughout the job.
Next, in your Parameter in Trigger/calls on build other project
host_name=${machine_name}
And use the same variable in child job.
I think there is no need for multiple assignments above, but still you can try this.
'_' was missing. host_name=$machine_name.
you cannot set or pass the parameter value from the script o/p. either make it as env variable using groovy or set in to property file & read.

Inject environment variables plugin is passing value as "12345"

I am using "Inject environment variables" Jenkins plugin to pass values to child jobs. Property file name is config.Today_date(eg. config.0403) and it has two parameters in it 1. change list(eg. 175444) 2. today's date(eg. 04032014). In child job Properties file path is config.${Today_date}($Today_date is predefined parameter in parent job).
First issue is: it error out saying config."Today_date" do not exist. I went to location and found property file is there and realized that child job is searching properties file as config."Today_date"(config."0403")and actual name of file is config.today_date
Second issue is: if i put property file name as config.0403(today_date) in Properties file path, Then it successfully locate the file but where ever it use parameters from this file, it use them as "0403" & "175444"
Please advise how i can solve "" issue at both locations.
Thanks !!

How to store last value of parameter in parameterized job as a default value for next build in Jenkins?

I have been using Jenkins for a few weeks and I have one small problem. I can't find any plugin or solution for storing the last value of a parameter in a parametrized job as a default value for the next build.
For example:
My parameter takes build version (1.0.0.01) in the first build. In the next build it will be changed to 1.0.0.02, but I want to have a 1.0.0.01 in the default value field as a hint.
Does anybody have a solution or advice?
The Persistent Parameter Plugin is exactly what you are looking for!
You just need to download it from the official Jenkins repository and install it, no need for any additional setup.
Then on your job, you just need to add a "Persistent Parameter" in order to have default values used and saved between builds.
You can add a System groovy build step to your job (or maybe a post build Groovy step) using the Jenkins API to directly modify the project setting the default parameter value.
Here is some code that may be useful to get you started:
import hudson.model.*
paramsDef = build.getParent().getProperty(ParametersDefinitionProperty.class)
if (paramsDef) {
paramsDef.parameterDefinitions.each{ param ->
if (param.name == 'FOO') {
println("Changing parameter ${param.name} default value was '${param.defaultValue}' to '${param.defaultValue} BAR'")
param.defaultValue = "${param.defaultValue} BAR"
}
}
}
Have a look at the class ParameterDefinition in the Jenkins model.
You probably need to modify the default param value based on the current build executing. Some code to get that would look like this:
def thisBuildParamValue = build.buildVariableResolver.resolve('FOO')
The Extended Choice Parameter plugin provides this capability by using default parameter values from a properties file. A default parameter can be selected from a specified property key and this key can be programmatically modified in your current build. I would then use a groovy script in the current build to set the value of the default property key for the next build.
As an example you would have an Extended Choice Parameter whose default value is defined by a properties file version.properties with keys as follows:
versions=1.0.0.02, 1.0.0.01, 1.0.0.00
default.version=1.0.0.02
The parameter definition would include:
Property File=version.properties
Property Key=versions
Default Property File=version.properties
Default Property Key=default.versions
The GUI for your parameter in the next build would show a selection list with 1.0.0.02 selected by default. This feature is also very useful for pipeline builds where you would want the parameters of a downstream build stage to be set by an earlier build.
The only drawback to this approach might be that the parameter UI will be a drop-down selection. You may opt to have a single value in the versions property key so not to confuse your users.
Similar to thiagolr's answer, but for those of you using pipelines! It appears the persistent-parameter-plugin doesn't work for those using pipeline 2.0. But there is a patched version at https://github.com/ashu16815/persistent-parameter-plugin which seems to work for me.
Clone it locally:
git clone https://github.com/ashu16815/persistent-parameter-plugin.git
Build it:
mvn clean install
Install it in Jenkins:
1) Navigate to Jenkins > Manage Jenkins > Manage Plugins
2) Click Advanced tab
3) Scroll down to Upload Plugin
4) Click Choose file and select the persistent-parameter.hpi in the target directory of the maven build above
Now it should persist.

Resources