42

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?

Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
slacy
  • 11,397
  • 8
  • 56
  • 61
  • Terminology overload: Current date & time is displayed in the "Description" field, not the "Name" which defaults to incrementing number. See the [Description Setter](https://wiki.jenkins-ci.org/display/JENKINS/Description+Setter+Plugin) plugin, or Pipeline DSL's `currentBuild.description` property. – MarkHu May 24 '16 at 16:52

9 Answers9

40

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}"
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
  • 7
    But this plugin can't express the build parameters, or generally speaking script env variables that you may set. But it can read a host env variable (e.g. '${HOSTNAME}'), and a properties file from the workspace. So, one not-so-gracious workaround is to define e.g. a shell script as a build step, and write the parameters 'name=values' to an arbitrary properties file (e.g. : echo "MY_PARAM=${MY_PARAM}" > params.properties. The only thing is that the build name setter plugin evaluates the expression once too early, and finally at the end only (so the build must end for you to see the result). – Patrice M. Oct 08 '13 at 16:51
  • Has anyone found a better alternative to this? The name I want to set can easily be calculated from existing properties (with some pretty simple string manipulation) but I can't see any way of doing that outside of a shell script which outputs to a property file, which is then injected. The main pitfall is that the job can fail before it gets to the inject step! – Constantinos Apr 11 '17 at 13:10
  • 1
    @Constantinos I updated my answer to cover using a Pipeline job, which makes this way easier than in a Freestyle job. – Christopher Orr Apr 11 '17 at 13:25
19

[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"}
farialima
  • 217
  • 2
  • 6
10

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.

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
saver
  • 400
  • 4
  • 12
  • 5
    Very cool. The only downside as a "user" is having the speed-bump of an "admin approval" on your Groovy script. With the *Groovy Postbuild* plugin, the syntax is a little different. Example: `manager.build.displayName = "#" + manager.build.number + ' ' + manager.envVars["desc_var"];` --and for the description: `manager.build.description = "Groovy Postbuild: " + manager.build.number` – MarkHu Jan 08 '16 at 02:38
  • Considering the dependencies of Build Name Setter plugin, I think this is the best we can achieve without much headache – PravyNandas Jan 06 '21 at 17:55
4

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

abhishekrvce
  • 185
  • 3
  • 9
  • 1
    For a Jenkins declarative pipeline one can add `script { currentBuild.displayName = ${VARIABLE} }` to change the build name. – rdrmntn Oct 10 '18 at 12:44
2

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

Mickael Rahmine
  • 372
  • 1
  • 7
0

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

Leo
  • 1,683
  • 2
  • 20
  • 25
0

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"

nharward
  • 101
  • 1
  • 2
0

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

judovana
  • 418
  • 2
  • 6
0

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

Sanjay Bhatia
  • 47
  • 2
  • 11
  • You will not have access to these settings from Jenkins web interface if you are running your job as a single pipeline. Settings must be configured from the Jenkinsfile only. – Dave Feb 14 '22 at 12:22