Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the Jenkins build name based on some conditions

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.

like image 573
soundararajan.c Avatar asked Feb 11 '17 04:02

soundararajan.c


Video Answer


2 Answers

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()}")
}
like image 85
Jon S Avatar answered Sep 28 '22 06:09

Jon S


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.

parameter config section

You can use the same parameter name as environment variable in the setter plugin as ${ENV,var="my-special-branch"}

setter plugin section

like image 25
Chandan Nayak Avatar answered Sep 28 '22 08:09

Chandan Nayak