How to set build name in Pipeline job?

Issue

I would like to set current build name and description in a Pipeline job.

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline

Resolution

Example:

node {
    currentBuild.displayName = "fooName"
    currentBuild.description = "fooDescription"
}

Replace ‘fooName’ and ‘fooDescription’ with your desired build name and description.

Have more questions? Submit a request

6 Comments

  • 2
    Avatar
    Anna Kordas

    Actually it changes the build description not build name, so the post title can be confusing. Resolution is currentBuild.displayName

    Edited by Anna Kordas
  • 0
    Avatar
    Ivan Fernandez Calvo

    We will update the article with the changes.

  • 0
    Avatar
    Badal Kotecha

    @Hernan & @Allan that does not work in declarative pipeline.

    I also tried

     

    pipeline {
        stages {
            stage("Build"){
                steps {
                    script {
                        env.currentBuild.displayName = "The name."
                        env.currentBuild.description = "The best description."
                    }
                    ... do whatever.
                }
            }
        }
    }

    But it throws NullPointerException error as shown below

    java.lang.NullPointerException: Cannot set property 'displayname' on null object
    	at org.codehaus.groovy.runtime.NullObject.setProperty(NullObject.java:80)
    	at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:197)
    	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:484)
    	at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:347)
     
    Edited by Badal Kotecha
  • 0
    Avatar
    Allan Burdajewicz

    Hernán

    In declarative you must use the script { ... } block.

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

     

  • 0
    Avatar
    Hernán Schmidt

    What is the equivalent for this in a declarative pipeline?

  • 0
    Avatar
    Allan Burdajewicz

    @Badal My script is missing the `agent` directive, the following should work as is:

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

    If it does not, please file a support request so that we can troubleshoot this further.

    Note: currentBuild is a Groovy variable, not an environment variable. So you cannot use env.

     

    Edited by Allan Burdajewicz
Please sign in to leave a comment.