In Jenkins can I use a parameter value in other config fields? - jenkins

I am setting up a parameterized build on my Jenkins server.
Basically I want to have the git branch name as a parameter. Then I want to use that parameter in various other fields in the job config.
I don't know if this is even possible, but I hope that it might be as it seems an obvious need.
The only docs I could find is this old wiki page
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
It shows that build parameter is available as an ENV var, but it doesn't show how to use it elsewhere in the job config.

despite being undocumented, this syntax works in job config fields
${PARAM_NAME}

Just use $PARAM_NAME anywhere in the configure.

The easiest way is to use the "Git parameter" parameter type in your config and then you can reference that simply in the Source Code Management section - see here. This assumes that the Jenkins instance has the Git parameter plugin installed.
If you don't have that option, then:
you can simply add a String parameter to the configuration and use that in you configuration, but you need to uncheck "Lightweight checkout" to avoid errors like this:
stderr: fatal: Couldn't find remote ref refs/heads/${BRANCH}
String parameter declaration:
String parameter usage:
Source of pictures

Related

How to pass parameter to Jenkins service

I have a Jenkins service which I run like sudo service jenkins start|stop. Now, I want to pass a parameter --prefix=/jenkins to this service. I tried sudo service jenkins --prefix=/jenkins but this param is ignored. How can I pass this additional param?
You should edit /etc/default/jenkins. The quick and dirty way is to find the variable JENKINS_ARGS="..." at the end of the file. Simply add --prefix=/jenkins there.
Let's say that you had:
JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT"
it should be:
JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT --prefix=/jenkins"
The nice way is to edit the variable PREFIX which should be located a few lines above, in my case it was PREFIX=/$NAME, change that to PREFIX=/jenkins and then, similar to before, you edit JENKINS_ARGS and add --prefix=$PREFIX.
You need to extend your init script. According to the service manual page,
service passes COMMAND and OPTIONS it to the init script unmodified
It depends on your distribution, but most likely your init script does not handle any additional options. If you fix that, then you will be able to pass parameters.
Having said this, the proper way to establish settings in a more permanent way is the one described by Jon S.

Parameterized job always run with default parameter

I am having troubles to force Jenkins job to always run with default parameter. Does anyone know the possible plugin to help with that case? Right now I am using extended parameter choice, but still there is no option to just run the job with default value without asking user for parameter.
Solution 1
Currently there is not a straight forward solution to run a parameterized job with default parameter using a plugin. However there is a workaround to accomplish that using the EnvInject Plugin.
As #General_Code noted:
Just add the build step, set the variable like: var1=value and then
use it using ${var1}
Solution 2
As #RejeeshChandran noted:
a more robust solution is the Parameterized Build Plugin which provides the functionality of defaults values for the parameters.
Note
Note that Parameter Defaults Options is a plugin under development which will solve exactly this request. When it is released, you will be able to set it up so your parameter will get a default value when you run it manually.
you can use this plugin Parameterized Scheduler
allow you to write a cron expression with the parameters inside like this
H(0-29)/10 * * * * % name=value; othername=othervalue
Documentations
Use This build is parameterized option in Jenkins configuration. Here you can add default values to the parameters. It will run with default value if the user doesn't change it. It is good to have configurable parameter before running the job.
For configuration details see https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build. You can have multiple parameters.
Go to your jenkins job -> Configure -> General Tab
Add all options in "Description" but the default option in "Default Value"
enter image description here

Is there a jenkins plugin to add a why-I-built-this remark, that will appear with 'started by [user]'?

When I run a manual build, I'd often like to mark it with documentation to show why I ran it. Is this feature available with a plugin?
thank you!
I would approach this by adding a build parameter as a string, as above, then use the Description Setter Plugin to set it from that parameter. We use something like this for the regex:
^\++ : BUILD DESCRIPTION : GERRIT_CHANGE_OWNER_EMAIL=([^#\s]*)\S*, BUILD_USER_EMAIL=([^#\s]*)\S*, GERRIT_BRANCH=(\S*), GIT_COMMIT=(\S{8}).*$
and this for the description:
\1\2, \4, \3
As a result we get:
jspain, 0ee3198b, master
or when it fails, we get:
Failed due to timeout.
wayvad, fc7bdf2a, master
this "Failed..." text comes from the Build Timeout Plugin
I am not aware of a plugin that can do this, and after a brief search I could not find one to do what you describe.
You can mimic this behavior by adding a string parameter in the job that takes a default value of automatically started when it's normally run, but that you can change to my reasons for this build when starting it manually.
You could then use a batch (or groovy or ) build step to print the contents of that parameter into the build log. If you do some sort of SCM checkout I'm not sure how close you can get it to print to the line that contains the username that started the job, however you can click on the view parameters button in the job build and see what was in the field quickly without having to parse the logs.
The downside of this is that parameter would have to be added to each job.

Use build timestamp in setting build description Jenkins

I have installed Description Setter Plugin but I don't know if and how I can use the BUILD_ID which in jenkins/env-vars.html/ is displayed in the format: "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss).
Does anyone know if I can use it and how?
Displaying the BUILD_ID would be the easiest way to add timestamp to the build description, but if not possible, how can I achieve that?
Thanks!
They replaced the ${BUILD_ID} variable to contain the build number instead of this timestamp (since 1.597+). See https://issues.jenkins-ci.org/browse/JENKINS-26520
There are some workarounds with other plugins like EnvInject or you just use the regexp feature of the Description Setter Plugin like this:
add execute shell blog (works for Linux)
insert command echo "date:" $(date +'%Y-%m-%d_%H-%M-%S')
Set Description Setter plugin to regexp date:(.*)
Set Description Setter plugin to description \1
If you have a fresh Jenkins version (1.6xx), you have to install the ZenTimestamp plugin and use the BUILD_TIMESTAMP variable:
You can customise the format in the global Jenkins settings:
(my solution also shows how to use a custom link as a description)
"Build Timestamp Plugin" will be the Best Answer to get the TIMESTAMPS in the Build process. Follow the below Simple steps to get the "BUILD_TIMESTAMP" variable enabled.
STEP1:
Manage Jenkins -> Plugin Manager -> Installed...
Search for "Build Timestamp Plugin".
Install with or without Restart.
STEP2:
Manage Jenkins -> Configure System.
Search for 'Build Timestamp' section, then Enable the CHECKBOX.
Select the TIMEZONE, TIME format you want to setup with..Save the Page.
USAGE:
When Configuring the Build with ANT or MAVEN,
Please declare a Global variable as,
E.G. btime=${BUILD_TIMESTAMP}
(use this in your Properties box in ANT or MAVEN Build Section)
use 'btime' in your Code to any String Variables etc..
You can use a groovy token such as:
${GROOVY,script = "String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())"}
It will add to the build description timestamp like: 2021-12-05 13:29
Note that build.getTimestampString2() would also print the timestamp, but according to UTC (in my timezone it's two hours earlier): 2021-12-05T11:29:09Z

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