从Jenkins声明式流水线中设置构建名称和描述

54

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

我的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 文件来自动设置。

2个回答

90

我认为这个可以实现你想要的功能。 我能够在 script 块中完成它:

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

该脚本是一种跳出声明性Pipeline的紧急措施。可能有一种声明性的方法来实现,但我找不到。还有一点需要注意。我认为你想使用currentBuild.displayName而不是currentBuild.name。在Jenkins全局变量的文档中,我没有看到currentBuild下的name属性。


声明式的方式是编写一个纯 Groovy 脚本在外部文件或共享库中,并将其作为步骤调用,从而消除了脚本块的需要。 - Max Cascone

17

如果你想从参数中设置构建名称到一个任务,你可以使用

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

任务配置

enter image description here

使用参数构建作业

enter image description here

构建历史

enter image description here

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


2
作为额外的说明,我遇到了麻烦,因为我使用的是单引号字符串(例如:'${NumeroCambio}')。显然,这只适用于双引号字符串。 - Colin Basnett
1
那是我忘记的一点。我更新了答案。谢谢,Colin! - Carlos Andres
@CarlosAndres,你用的是哪个插件来实现这个漂亮的绿色球和勾选符号? - Arbab Nazar
1
据我所知,你可以使用currentBuild.displayName = nameOfYourParameter代替currentBuild.displayName = "${nameOfYourParameter}" - msa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接