如何在 Jenkins Job DSL 中设置构建名称?

How to set build name in Jenkins Job DSL?

根据 https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.wrapper.MavenWrapperContext.buildName

中的文档

以下代码应在 Jenkins 作业的构建历史记录中更新构建名称:

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

不幸的是,它没有这样做。

有没有办法从 Jenkins Job DSL 脚本更改构建名称? 我知道我可以从 Jenkins Pipeline Script 更改它,但在这项特定工作中我不需要它。我在工作中使用的只是 steps.

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

我想强调的是,我正在寻找一种原生的 Jenkins Job DSL 解决方案,而不是 Jenkins Pipeline Script 或任何其他 hacky 方式,如操作环境变量。

这是更改构建的显示名称和描述的最小管道。恕我直言,这非常简单。

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})"
                }
            }
        }
    }
}

它在 Jenkins 的 UI 中产生以下表示:

我今天已经设法解决了我的问题。 该脚本不起作用,因为它需要在 Jenkins 中安装 build-name-setter 插件。安装后它完美运行。 不幸的是,默认情况下 jobdsl 处理器不会通知丢失的插件。此处描述的参数启用 https://issues.jenkins-ci.org/browse/JENKINS-37417

setBuildName("your_build_name")groovyPostBuild 步骤中也可以解决问题。 需要 Groovy Postbuild 插件。