从 Jenkins 声明性管道设置构建名称和描述

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

我想从 Jenkins 声明性管道设置构建名称和描述,但找不到正确的方法。我尝试在管道之后使用环境括号,在代理括号中使用节点括号等。我总是遇到语法错误。

我的 Jenkinsfile 的最后一个版本是这样的:

pipeline {  
    stages {
        stage("Build") {
            steps {
                echo "Building application..."
                bat "%ANT_HOME%/bin/ant.bat clean compile"
                currentBuild.name = "MY_VERSION_NUMBER"
                currentBuild.description = "MY_PROJECT MY_VERSION_NUMBER"
            }
        }
        stage("Unit Tests") {
            steps {
                echo "Testing (JUnit)..."
            echo "Testing (pitest)..."
                bat "%ANT_HOME%/bin/ant.bat run-unit-tests"
            }
        }
        stage("Functional Test") {
            steps {
                echo "Selenium..."
            }
        }
        stage("Performance Test") {
            steps {
                echo "JMeter.."
            }
        }
        stage("Quality Analysis") {
            steps {
                echo "Running SonarQube..."
                bat "%ANT_HOME%/bin/ant.bat run-sonarqube-analysis"
            }
        }
        stage("Security Assessment") {
            steps {
                echo "ZAP..."
            }
        }
        stage("Approval") {
            steps {
            echo "Approval by a CS03"
            }
        }
        stage("Deploy") {
            steps {
                echo "Deploying..."
            }
        }
    }
    post {      
        always {
            junit '/test/reports/*.xml'
        }
        failure {
            emailext attachLog: true, body: '', compressLog: true, recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider']], subject: '[JENKINS] MY_PROJECT build failed', to: '...recipients...'
        }
        success {
            emailext attachLog: false, body: '', compressLog: false, recipientProviders: [[$class: 'DevelopersRecipientProvider']], subject: '[JENKINS] MY_PROJECT build succeeded', to: '...recipients...'
        }
    }       
}

错误是:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 11: Expected a step @ line 11, column 5.
                currentBuild.name = "MY_VERSION_NUMBER"
       ^

WorkflowScript: 12: Expected a step @ line 12, column 5.
                currentBuild.description = "MY_PROJECT MY_VERSION_NUMBER"
       ^

理想情况下,我希望能够从 build.properties 文件或 Jenkins 构建日志中读取 MY_PROJECT 和 MY_VERSION_NUMBER。任何有关该要求的指导也将不胜感激。

更新

根据我在下面得到的答案,以下方法有效:

stage("Build") {
    steps {
        echo "Building application..."
        bat "%ANT_HOME%/bin/ant.bat clean compile"

        script {
            def props = readProperties  file: 'build.properties'
            currentBuild.displayName = "v" + props['application.version']
        }
    }

现在,构建版本是通过读取 build.properties 文件在管道期间自动设置的。

jenkins jenkins-pipeline
2个回答
92
投票

我想这会达到你想要的效果。 我能够在

script
块内完成此操作:

pipeline {
    stages {
        stage("Build"){
            steps {
                script {
                    currentBuild.displayName = "The name."
                    currentBuild.description = "The best description."
                }
                ... do whatever.
            }
        }
    }
}

script
是一种逃离声明式管道的逃生口。 可能有一种声明性的方法可以做到这一点,但我找不到它。 还有一点注意。 我认为你想要
currentBuild.displayName
而不是
currentBuild.name
在 Jenkins 全局变量的文档中,我没有在 currentBuild 下看到 name 属性。


17
投票

如果您想通过参数为作业设置构建名称,您可以使用

currentBuild.displayName = "${nameOfYourParameter}"
确保使用双引号而不是单引号

作业配置

enter image description here

使用参数构建作业

enter image description here

创造历史

enter image description here

参考:如何在管道作业中设置构建名称?

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