Jenkins build number changing to data and time - jenkins

I want to change the build number format (e.g. #10) to the date/time format YYYYMMDD-SSMMHH, e.g., 20160812-062713.
I have tried the build-name-setter and version number plugins, which did not help me adding date and time. Is there any option to do this?

The Build Timestamp Plugin can produce an environment variable ${BUILD_TIMESTAMP} that can be read and applied by the Build Name Setter Plugin.

Related

Inserting timestamp to folder name Jenkins

I want to rename file in Jenkins so that a file name would contain a timestamp. I installed zentimestamp plugin and tried to use it:
SET ts=${BUILD_TIMESTAMP}
ren file_name.zip file_name_%ts%.zip
but what i get is
file_name_${BUILD_TIMESTAMP}.zip
I got Date and Time Pattern in Global Properties set to: yyyyMMddHHmm.
Can someone please explain me how to use BUILD_TIMESTAMP properly?
It looks like your code is from a Microsoft Batch file? If that's the case you'd reference BUILD_TIMESTAMP like this:
ren file_name.zip file_name_%BUILD_TIMESTAMP%.zip
BUILD_TIMESTAMP should be an environment variable if it's being injected correctly by the plugin. How you reference environment variables changes depending on what scripting solution is being used (e.g. batch, bash, ant, gradle, etc).

Jenkins Extended Choice Parameter - using the values

I'm new to Jenkins so this is probably an easy one. I have the Extended Choice Parameter plugin installed. I'm using the Multi Select parameter type to pick from a list of servers (SERVER1,SERVER2,SERVER3) I've set Source for Value, Default Value, and Value Description.
I save it, and it looks great. I can pick any or all servers for the build. Now for the big question.. how do I use these values in the build? Basically I have a step in the build that can take in a comma separated list that is called by a shell command:
d:\python\deploy.py?serverlist=$blah
What do I put in for $blah to use that list of servers?
Just to be clear, if I'm on command line I would do the following:
d:\python\deploy.py?serverlist=SERVER1,SERVER2,SERVER3
I'm sure it's something simple but I just cannot find it in the docs or an example.
We could get the servers list like this
d:\python\deploy.py?serverlist=$SERVERLIST
or this on Windows
d:\python\deploy.py?serverlist=%SERVERLIST%
To see the list of environment variables which we could you, try this URL (change localhost by your Jenkins URL, TEST by the job name, 10 by the build number)
https://localhost:8080/job/TEST/10/injectedEnvVars/
UPDATE to #sniperd's edition:
This URL will shows us the parameters list in the Job:
http://localhost:8080/job/TEST/59/parameters/

Build number of previous build number

I am trying to create a Jenkins pipeline job which will check the status of previous before it trigger the execution. But I am not able to find the url to previous build number. I tried constructing url using BUILD_NUMBER-1 option but not working because BUILD_NUMBER is not a integer. Could someone help me to find the url to previous build ?
You can make use of the currentBuild global variable. You can see all global variables at http://jenkins-url/pipeline-syntax/globals.
The number can be retrieved from it. You can see all of the whitelisted calls on RunWrapper.
number
build number (integer)
So, in your pipeline you could do currentBuild.number.
If there is a previous build, you could also use previousBuild and use currentBuild.previousBuild.number. Keep in mind that previousBuild can be null.

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