firen
firen

Reputation: 1424

How to set build name in Jenkins Job DSL?

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.

Upvotes: 3

Views: 6077

Answers (3)

lenkovi
lenkovi

Reputation: 1798

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

Upvotes: 1

firen
firen

Reputation: 1424

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

Upvotes: 3

Michael
Michael

Reputation: 2683

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

Upvotes: 2

Related Questions