Jenkins pipeline 插件:设置构建描述

问题描述 投票:0回答:4

我正在尝试用一个使用 Jenkins 管道插件的新作业替换我们当前的构建管道(目前使用旧式 Jenkins 作业拼凑在一起),并从项目存储库加载

Jenkinsfile

遗留作业所做的一件事是使用描述设置器插件设置构建描述以包括 Mercurial 哈希、用户名和当前版本,以便轻松找到构建。

有没有办法使用 Jenkins 管道插件复制/模拟这种行为?

jenkins jenkins-pipeline jenkins-workflow
4个回答
108
投票

刚刚想通了。管道作业公开一个具有可写属性的

currentBuild
全局变量。设置描述可以通过以下方式完成:

currentBuild.description = "my new description"

管道脚本中的任何位置。更多信息请参阅此 DZone 教程


43
投票

@jjst 的答案描述了如何在“脚本化管道”中设置构建描述。在声明性管道中,您可以执行相同的操作,但需要将其放置在

script { }
块内。这是取自Cloudbees 文章的完整工作示例:

pipeline {
    agent any
    stages {
        stage("1st stage") {
            steps {
                script {
                    currentBuild.displayName = "My custom build name"
                    currentBuild.description = "My custom build description"
                }
            }
        }
    }
}

6
投票

当 jjst 写下他的答案时,情况可能并非如此,但现在使用最新的 jenkins 和插件,您可以将其设置在顶部主管道之外。这意味着您不必嵌入脚本设置并执行特殊步骤等,例如

currentBuild.description = "my new description"
pipeline {...

currentBuild.description = """
blah
blah
blah
"""
pipeline {

4
投票

我不确定它有多大,但我最近发现了

buildDescription
插件,它为您提供了一种声明性方法来设置构建描述。 安装后,就像这样简单:

steps {
  buildDescription 'my build'
}

控制台将显示一步输出:

New run description is 'my build'

© www.soinside.com 2019 - 2024. All rights reserved.