61

I'm trying to replace our current build pipeline, currently hacked together using old-school Jenkins jobs, with a new job that uses the Jenkins pipeline plugin, and loads a Jenkinsfile from the project repository.

One thing that the legacy job did was set the build description to include the Mercurial hash, username and current version using the Description setter plugin, so that builds are easy to find.

Is there a way to replicate/emulate this behaviour with the Jenkins pipeline plugin?

jjst
  • 2,631
  • 2
  • 22
  • 34
  • There is no way to do it in declarative pipeline. See this q & a: https://support.cloudbees.com/hc/en-us/articles/220860347-How-to-set-build-name-in-Pipeline-job- – apanzerj May 03 '18 at 01:38
  • 1
    Related: https://stackoverflow.com/q/43639099/357774. – Noyo Jul 17 '18 at 08:42
  • 2
    Now we just need a nice way to get regex matching strings out of the console.log ... – MarkHu Aug 01 '19 at 21:36

4 Answers4

103

Just figured it out. The pipeline job exposes a currentBuild global variable with writable properties. Setting the description can be done with:

currentBuild.description = "my new description"

anywhere in the pipeline script. More information in this DZone tutorial.

jjst
  • 2,631
  • 2
  • 22
  • 34
  • 14
    To do this in declarative pipeline use the script { ... } block https://jenkins.io/doc/book/pipeline/syntax/#script – Morgan Christiansson May 25 '17 at 09:51
  • 4
    @jjst Do you know how to set "Job Description" in pipeline ? – Jerald Sabu M Nov 20 '17 at 10:58
  • 3
    This not necessarily correct because this is not the way to do it in declarative pipeline. Using a script tag is basically escaping pipeline. – apanzerj May 03 '18 at 01:36
  • 1
    it's also possible to change the `displayName` too: https://qa.nuxeo.org/jenkins/pipeline-syntax/globals#currentBuild –  Oct 01 '18 at 16:40
  • I was trying to update the build number and stumble upon your post on updating description. Wish had seen your post earlier, tried so many things from overriding environment variable BUILD_DISPLAY_NAME to using withEnv. I was able to update the values and even print the new values but on Jenkins UI itself the build number was not getting updated. Tried your solution and it worked like a charm! – Vikas Apr 30 '19 at 05:50
41

The answer from @jjst describes how to set the build description in "scripted pipelines". In declarative pipelines you can do the same, but need to place it inside a script { } block. Here a complete working example taken from comments on a Cloudbees article:

pipeline {
    agent any
    stages {
        stage("1st stage") {
            steps {
                script {
                    currentBuild.displayName = "My custom build name"
                    currentBuild.description = "My custom build description"
                }
            }
        }
    }
}
t0r0X
  • 4,212
  • 1
  • 38
  • 34
6

This may not have been the case when jjst wrote his answer but now with the latest jenkins and plugins you can set this outside the main pipeline at the top. This means you dont have to embed script setting and have special steps etc eg

currentBuild.description = "my new description"
pipeline {...

or

currentBuild.description = """
blah
blah
blah
"""
pipeline {
krad
  • 1,321
  • 1
  • 15
  • 12
  • Does this also work for `currentBuild.displayName`? We are using scripted pipelines, so this doesn't apply to us and I can't test it. I expect others would want to know. – willkil Dec 14 '18 at 19:31
  • Could you please specify what version "latest jenkins" meant at the time of your post? We are stuck on 2.138.3 for reasons i don't want to get in to. – Max Cascone Jul 03 '19 at 17:32
  • I was using the current version from their ubuntu repo at the time as we tend to auto upgrade and rollback (small team spread to thin) if we find an issue so l would check the release dates. – krad Jul 04 '19 at 21:56
  • You can even use parameters and global environment variables when setting the description/displayName outside of the pipeline. But it doesn't seem to work for variables defined in the pipeline 'environment' block. – kapex Feb 07 '20 at 13:23
3

I'm not sure how old it is, but I recently discovered the buildDescription plugin that gives you a declarative method to set the build description. Once installed, it's as easy as:

steps {
  buildDescription 'my build'
}

The console will show a step output: New run description is 'my build'

Max Cascone
  • 648
  • 9
  • 25