3
votes

According to the documentation in https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.wrapper.MavenWrapperContext.buildName

Following code should update build name in Build History in Jenkins jobs:

// define the build name based on the build number and an environment variable
job('example') {
  wrappers {
    buildName('#${BUILD_NUMBER} on ${ENV,var="BRANCH"}')
  }
}

Unfortunately, it is not doing it.

Is there any way to change build name from Jenkins Job DSL script? I know I can change it from Jenkins Pipeline Script but it is not needed for me in this particular job. All I use in the job is steps.

steps {
  shell("docker cp ...")
  shell("git clone ...")
  ...
}

I would like to emphasise I am looking for a native Jenkins Job DSL solution and not a Jenkins Pipeline Script one or any other hacky way like manipulation of environment variables.

3
Possible duplicate of How to customize Jenkins build name?Michael
The answer you have pointed to is related to Jenkins Pipeline scripts or has some hacky ways of changing the build name. I was hoping for a solution supported by Jenkins Job DSL script natively.firen

3 Answers

3
votes

I have managed to solve my issue today. The script did not work because it requires build-name-setter plugin installed in Jenkins. After I have installed it works perfectly. Unfortunately, by default jobdsl processor does not inform about missing plugins. The parameter enabling that is described here https://issues.jenkins-ci.org/browse/JENKINS-37417

1
votes

Here's a minimal pipeline changing the build's display name and description. IMHO this is pretty straight forward.

pipeline {

    agent any

    environment {
        VERSION = "1.2.3-SNAPSHOT"
    }

    stages {
        stage("set build name") {
            steps {
                script {
                    currentBuild.displayName = "v${env.VERSION}"
                    currentBuild.description = "#${BUILD_NUMBER} (v${env.VERSION})"
                }
            }
        }
    }
}

It results in the following representation in Jenkins' UI: Changed display name is shown in build history; Stage view shows the display name

1
votes

setBuildName("your_build_name") in a groovyPostBuild step may do the trick as well. Needs Groovy Postbuild Plugin.