J4N
J4N

Reputation: 20733

How to set the BuildNumber of a jenkins descriptive pipeline from a service?

I've tried to accomplish this: https://www.quernus.co.uk/2016/08/12/global-build-numbers-in-jenkins-multibranch-pipeline-builds/ in order to have a unique build number accross all the branches.

//...
stages {        
    stage('Initialization started'){
        steps{
            env.BUILD_ID = 'http://Energy-JobSrv2.vm.dom/api/buildnumber'.ToURL().text
            currentBuild.displayName = "#" + env.BUILD_ID
            echo "Job parameters:\n\t- ROOT_FOLDER: ${params.ROOT_FOLDER}\n\t- Build X86: ${params.buildX86}\n\t- Build X64: ${params.buildX64}\n\t- Commit Version changes: ${params.commitVersionChanges}\n\t- Setup Version: ${params.version}.${env.BUILD_NUMBER}\n\t- Setup Configuration: ${params.setupConfiguration}\nCurrent repository: ${workspace}"                 
        }
    }
    //...
}
//...

But I think it's not done for Jenkins Descriptive pipeline files, because when I try to run it, I get this:

[Bitbucket] Build result notified
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 17: Expected a step @ line 17, column 5.
                env.BUILD_ID = 'http://Energy-JobSrv2.vm.dom/api/buildnumber'.ToURL().text
       ^

WorkflowScript: 18: Expected a step @ line 18, column 5.
                currentBuild.displayName = "#" + env.BUILD_ID

What is the equivalent with Jenkins descriptive pipeline files?

Upvotes: 1

Views: 239

Answers (1)

SmartTom
SmartTom

Reputation: 821

You have to encapsulate your commands with script :

//...
stages {        
    stage('Initialization started'){
        steps{
            script{
                env.BUILD_ID = 'http://Energy-JobSrv2.vm.dom/api/buildnumber'.ToURL().text
                currentBuild.displayName = "#" + env.BUILD_ID
            }
            echo "Job parameters:\n\t- ROOT_FOLDER: ${params.ROOT_FOLDER}\n\t- Build X86: ${params.buildX86}\n\t- Build X64: ${params.buildX64}\n\t- Commit Version changes: ${params.commitVersionChanges}\n\t- Setup Version: ${params.version}.${env.BUILD_NUMBER}\n\t- Setup Configuration: ${params.setupConfiguration}\nCurrent repository: ${workspace}"                 
        }
    }
    //...
}
//...

Upvotes: 2

Related Questions