How to set the Jenkins build name based on some conditions - jenkins

I wish to set the build name based on some condition.
Eg:
if the branch name( input parameter)= development
then build name= development
if the branch name = master then build name= master.
I can able to set the build name by using build name setter plugin but i need this based on condition.

Assuming that you use Free Style jobs, then you can use the Groovy plugin to execute arbitrary groovy code which has access to the Java structures for the build and Jenkins.
Simply add an Execute system Groovy scriptstep and enter your code which determines the name, then use build.setDisplayName() to set the name.
So similar to your example, here is a name setter which sets the name depending on the value of build parameter branch_name:
if (build.buildVariableResolver.resolve("branch_name").equals('master')) {
build.setDisplayName("Master build #${build.getNumber()}")
} else if (build.buildVariableResolver.resolve("branch_name").equals('development')) {
build.setDisplayName("Development build #${build.getNumber()}")
} else {
build.setDisplayName("Other build #${build.getNumber()}")
}

if the branch name( input parameter) = development then build name=
development
Assuming you are providing the branch name as a parameter and want to set the build name as the same using the name setter plugin, you should be able to configure it.
You can use the same parameter name as environment variable in the setter plugin as ${ENV,var="my-special-branch"}

If you want to set build name to a job from a parameter, you can use
currentBuild.displayName = "${nameOfYourParameter}"
Job Configuration
Buil job with parameters
Build History
Ref: How to set build name in Pipeline job?

Related

Jenkins node select with parameter

I have two Jenkins nodes, but only one node can be ran on special parameter, how do I add restriction to the master that if the parameter was set to true the job will run on target node?
You can configure to accept a parameter that indicate if its special or not.
In jenkins groovy job just use a conditional if to check if parameter is send as special one or its normal accordingly you can create a new variable like
def Agent_Var = Special_Agent (The label of special agent)
Note - please configure label for the agents to determine which agent to use.
after setting up Agent_Var variable you can define Agent_Var label in pipeline as below -
pipeline {
agent { label "$Agent_Var " }

Jenkins Extended Choice Parameter associated to multiple environments variable

I am using in a Test Jenkins configuration the Choice Parameter in order to select the target environment against the tests are running.
Like this:
I use the selected value to pass to the maven test run as selected profile: -Dprofile=${TargetEnv}.
I would like to extend the implementation to make some additional git merge operation before the repository is build and the tests are triggered to run (I don't want to go into detail).
My question is how can I use the "Extended Choice Parameter" Jenkin plugin to have multiple environment variable set when a each choice value.
Example: In case the 'dev' is select then I would like to have two environment variables: targetEnv: qa and lowerEnvBranchName: develop-dev.
Somebody know how is possible to specify this kind of variables?

Remove Jenkins builds by name

I have Jenkins job with parameters. Every build is named by number and name that is read from parameter (#1-build1, #2-example1, #3-build2, #4-example2). Is it possible to configure Jenkins to delete jobs by name and not by how old it is. In my example, if I issue new build named #5-example3, I want #2-example1 to be removed, and not #1-build1. Is there a plugin for removing builds by some filter?
Here is a Groovy Script that can do the job. But please replace the condition as per your logic.
Jenkins.instance.getItemByFullName('temp').builds.findAll { it.number.toString().contains '<your build name pattern to delete>' }.each { it.delete() }
Please add this as a build step in your job. This will simply do as you want. If need any help, please comment here. Hope this helps.

Jenkins - Display text from a file in a parametrized build depending on previous choice

Is there any way to display an informative text from a file located in workspace, on a parametrized build step depending on an previous condition (like using active Choice plugin)?
activeChoiceReactiveParam('branch') {
description('Select the branch you are going to use')
choiceType('SINGLE_SELECT')
groovyScript {
script('["develop", "master"')
fallbackScript('"Error. No branch to select."')
}
filterable(true)
}
You can pass on text and parameters from one job to another using a properties file and inject it as environment variables in your desired job. The EnvInject plugin lets you do injection of that properties file in the desired job.

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