从Jenkins声明性管道设置构建名称和描述 [英] Set the build name and description from a Jenkins Declarative Pipeline

查看:94
本文介绍了从Jenkins声明性管道设置构建名称和描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Jenkins声明性管道设置构建名称和描述,但是找不到正确的方法.我尝试在管道之后使用环境支架,在代理支架中使用节点支架,等等.我总是会收到语法错误.

I would like to set the build name and description from a Jenkins Declarative Pipeline, but can't find the proper way of doing it. I tried using an environment bracket after the pipeline, using a node bracket in an agent bracket, etc. I always get syntax error.

我的Jenkinsfile的最新版本如下:

The last version of my Jenkinsfile goes like so:

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.对此要求的任何指导也将不胜感激.

Ideally, I'd like to be able to read MY_PROJECT and MY_VERSION_NUMBER from the build.properties file, or from the Jenkins build log. Any guidance about that requirement would be appreciated as well.

更新

根据我下面的回答,以下工作有效:

Based on the answer I had below, the following worked:

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文件,可以在管道过程中自动设置构建版本.

Now the build version is automatically set during the pipeline by reading the build.properties file.

推荐答案

我认为这可以满足您的要求.我能够在script块内完成该操作:

I think this will do what you want. I was able to do it inside a script block:

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

该脚本有点像逃脱声明,可以摆脱声明式管道.可能有一种声明式的方法可以执行,但我找不到.还有一个音符.我想您想要的是currentBuild.displayName而不是currentBuild.name在Jenkins全局文档中,我在currentBuild下没有看到name属性.

The script is kind of an escape hatch to get out of a declarative pipeline. There is probably a declarative way to do it but i couldn't find it. And one more note. I think you want currentBuild.displayName instead of currentBuild.name In the documentation for Jenkins globals I didn't see a name property under currentBuild.

这篇关于从Jenkins声明性管道设置构建名称和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆