Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the build name and description from a Jenkins Declarative Pipeline

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.

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...'
        }
    }       
}

Error is:

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"
       ^

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.

UPDATE

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']
        }
    }

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

like image 796
Charles Morin Avatar asked Apr 26 '17 15:04

Charles Morin


2 Answers

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.             }         }     } } 

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.

like image 197
Mateo Avatar answered Oct 14 '22 13:10

Mateo


If you want to set build name to a job from a parameter, you can use

currentBuild.displayName = "${nameOfYourParameter}". Make sure you use double quotes instead of single quotes.

Job Configuration

enter image description here

Build job with parameter

enter image description here

Build History

enter image description here

REFERENCE: How to set build name in Pipeline job?

like image 20
Carlos Andres Avatar answered Oct 14 '22 13:10

Carlos Andres