在声明式 Jenkinsfile 中设置管道描述

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

背景

我们使用声明性 Jenkinsfile 脚本开发 Jenkins 管道。例如,如果我们想在管道上设置日志轮换策略,我们可以使用

options
指令。

options {
    buildDiscarder( logRotator( numToKeepStr: "30" ) )
}

运行管道后,我们将在 Jenkins 的配置屏幕上看到以下日志轮换策略

我们从不对 GUI 进行修改(例如手动配置日志轮换),而应该由 Jenkinsfile 驱动

问题

我们想使用 Jenkinsfile 设置管道的描述。但是,我们没有看到任何名为

description
的闭包。经过一些研究,我们确实找到了一些方法来设置构建的描述(见下文),但没有找到管道本身的描述。

        steps {
            script {
                this.setDescription("123")
                currentBuild.displayName = "The name."
                currentBuild.description = "The best description."
            }
        }

我们如何使用 Jenkinsfile 中的指令设置管道的描述?

jenkins groovy jenkins-pipeline
1个回答
0
投票

要在 Jenkinsfile 中设置管道描述,理论上,您可以尝试

properties
步骤,假设您安装了 Project Description Setter Jenkins 插件。
properties
步骤允许您使用
org.jenkinsCi.plugins.projectDescriptionSetter.DescriptionSetterWrapper
.

定义作业属性,其中包括通过插件对管道本身的描述。
pipeline {
    agent any
    options {
        buildDiscarder(logRotator(numToKeepStr: "30"))
    }
    properties([
        [class: 'org.jenkinsCi.plugins.projectDescriptionSetter.DescriptionSetterWrapper', description: 'That is the pipeline description.']
    ])
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post {
        // Post actions if any
    }
}

但是

jenkinsci/project-description-setter-plugin
PR #1 对这种方法表示怀疑:

我怀疑你真的想使用 Pipeline 中的这个插件。最好只做像 Groovy Post-Build 那样的事情,并允许语句将描述设置为某个字符串。如果你想阅读文件那是你的事。

Groovy Post-Build
插件允许用户在作业的构建后步骤中执行 Groovy 脚本,然后可以修改构建或项目,包括设置构建或项目描述。

pipeline {
    agent any
    options {
        buildDiscarder(logRotator(numToKeepStr: "30"))
    }
    stages {
        // Your build stages
    }
    post {
        always {
            script {
                // Assuming "this" is a reference to the job
                def job = Jenkins.instance.getItemByFullName(env.JOB_NAME)
                // Set the description of the job (pipeline)
                job.description = 'This is the pipeline description.'
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.