slacy
slacy

Reputation: 11763

How to customize Jenkins build name?

When I run a job in Jenkins, each build is given a name that shows in the UI in Build History that's basically the current date and time.

I'd like to be able to put in build parameters there so that I can see in the build history which branches have been built and when.

I've searched around for plugins to do this, but I haven't been able to find any. Is there one?

Upvotes: 46

Views: 90114

Answers (9)

Mickael Rahmine
Mickael Rahmine

Reputation: 447

For a declarative pipeline you can use the groovy variable : currentBuild in a script{} block.

.displayName To set a new name, .description To set a description

pipeline {
    agent any
    stages {
        stage("Build"){
            steps {
                script {
                    currentBuild.displayName = "The name."
                    currentBuild.description = "The best description."
                }
            }
        }
    }
}

NOTE:

To use Jenkins Pipeline, you will need:

Jenkins 2.x or later (older versions back to 1.642.3 may work but are not recommended)

Pipeline plugin, which is installed as part of the "suggested plugins" (specified when running through the Post-installation setup wizard after installing Jenkins).

From : https://support.cloudbees.com/hc/en-us/articles/220860347-How-to-set-build-name-in-Pipeline-job- and : https://www.jenkins.io/doc/book/pipeline/getting-started/#:~:text=params.MY_PARAM_NAME.-,currentBuild,-May%20be%20used

Upvotes: 3

Sanjay Bhatia
Sanjay Bhatia

Reputation: 97

You can use Build Name and Description Setter plugin(https://plugins.jenkins.io/build-name-setter/) and pass the build name as String parameter enter image description here enter image description here

Upvotes: 0

judovana
judovana

Reputation: 417

Fork of text-finder-plugin, https://github.com/judovana/text-finders-plugin can set display name based on line in console output or other build artifacts

Upvotes: 0

abhishekrvce
abhishekrvce

Reputation: 185

To modify the default display Name use currentBuild.displayName = "#${BUILD_NUMBER}, branch ${BRANCH}"

Upvotes: 4

Christopher Orr
Christopher Orr

Reputation: 111555

Sounds like the Build Name Setter plugin.

But if you're using Pipeline, you can do something like this:

currentBuild.description = "#${BUILD_NUMBER}, branch ${BRANCH}"

Upvotes: 43

nharward
nharward

Reputation: 101

Caveat: this only works in *nix environments. For individual shell steps you can execute <command> instead as:

/usr/bin/env JOB_NAME="Old JOB_NAME: ${JOB_NAME}" <command>

Assuming your project is called "myproject", <command> would see the JOB_NAME environment variable as "Old JOB_NAME: myproject"

Upvotes: 0

Leo
Leo

Reputation: 1703

This plugin "Build name setter plugin" may help you. As a source for build name you can use a text file on a disk or an environment variable also you can combine the plugin with such plugin as EnvInject

Upvotes: 0

saver
saver

Reputation: 400

It is also possible to set build name "manually", using Jenkins Groovy plugin. Just follow these steps:

  1. Generate a new build name. If you are going to perform it in the separate job step, you may need to save it to properties file, for example with name "newVersion" and perform an "Inject environment variables" step with Jenkins EnvInject plugin.
  2. Next step - run a System Groovy script:

    def build = Thread.currentThread().executable
    assert build
    def newBuildName = build.getEnvironment().get('newVersion')
    try {
        if (newBuildName) build.displayName = newBuildName
        println "Build display name is set to ${newBuildName}"
    } catch (MissingPropertyException e) {}
    

As you can see, we are using a build.displayName variable here. Its value is a visible build name in Jenkins.

Upvotes: 10

farialima
farialima

Reputation: 317

[replying to Patrice M.'s comment above, just I don't have enough reputation to comment]:

The Build Name Setter plugin can express a variety of variables, including environment variables, when used in conjunction with the Token Macro plugin. Furthermore, build parameters are also available as environment variables; so, for example, if your build has a parameter "MYPARAM", then you can simply use it (assuming you have installed the Token Macro plugin) in the build name like this:

Built with parameter MYPARAM: ${ENV, var="MYPARAM"}

Upvotes: 19

Related Questions