3

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.

firen
  • 1,384
  • 2
  • 14
  • 32
  • Possible duplicate of [How to customize Jenkins build name?](https://stackoverflow.com/questions/12221799/how-to-customize-jenkins-build-name) – Michael Nov 24 '18 at 06:29
  • 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 Nov 26 '18 at 22:22

3 Answers3

3

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

firen
  • 1,384
  • 2
  • 14
  • 32
2

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

Michael
  • 2,443
  • 1
  • 21
  • 21
  • https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.jobs.FreeStyleJob.steps script seams to not be part of steps element – firen Dec 04 '18 at 15:34
  • @firen you linked to a job DSL documentation page. My example is in pipeline DSL and `script` is a valid step (see https://jenkins.io/doc/pipeline/steps/pipeline-model-definition/#-script-%20run%20arbitrary%20pipeline%20script) – Michael Dec 04 '18 at 15:38
1

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

lenkovi
  • 1,708
  • 2
  • 11
  • 16