3

I currently have a Jenkins DSL script defining my release pipeline(below). I was wondering if there is a way to automatically increase the version number of the released artifact for every build, so that I can publish it to Nexus with a new unique version number, rather than having to set it manually. I have used a plugin in the past that does this automatically with regular Jenkins jobs but I can't figure out how to do so with the pipeline script.

node('master') {
       //input 'Proceed?'

       stage 'Checkout'
              checkout scm

       stage 'Build'
              dir('./collector') {
                     sh "./gradlew clean build -Penv=${ENVIRONMENT}"
              }

       stage 'Flyway Migrate'
              dir('./database') {
                     sh "./gradlew flywayMigrate -i -Penv=${ENVIRONMENT}"
              } 

       stage 'Run cucumber Tests'
              dir('./collector') {
                     sh "./gradlew cucumber -Penv=${ENVIRONMENT}"
              }

       stage 'Publish artifact to Nexus'
              dir('./collector') {

                withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'NEXUS_PASSWORD',
                    usernameVariable: 'NEXUS_USERNAME', passwordVariable: 'NEXUS_PASSWORD']]) {
                            sh ("./gradlew publish -Pnexus_username=" + env.NEXUS_USERNAME+" -Pnexus_password=" + env.NEXUS_PASSWORD+ " -Penv=${ENVIRONMENT}--stacktrace")
                     }       


                  }

           //stage ''
    }
daspilker
  • 8,154
  • 1
  • 35
  • 49
user1712258
  • 117
  • 3
  • 7
  • 1
    We just use a YYYYmmDDHHMM timestamp of when the build started + the short hash of our git revision. Now if you want to have an atomic sequential number, you're going to need a place to keep it. Maybe put it in a DB & read/update as needed – Jason De Arte Jun 27 '16 at 05:46
  • I decided to place a file under version control and increment the minor version value per successful build. Not the nicest way to do it but it achieves the desired result. – user1712258 Jun 27 '16 at 12:04
  • You can use $BUILD_NUMBER and it will increase automatically, but I am not sure if it is a good practice – agusgambina Oct 31 '17 at 21:03

2 Answers2

0

You can use the VersionNumberPlugin in your pipeline to set the version number in a highly configurable way.

Dumb example:

script{
    version = VersionNumber(versionNumberString: '1.0.${BUILDS_ALL_TIME}')
}
István Rábel
  • 462
  • 4
  • 13
0

You can just use ${env.BUILD_NUMBER} variable in your pipeline.

Based on this answer, you can set it explicitly in gradle commandline as

-Pversion=${env.BUILD_NUMBER} (but need some pre-configuration, see the answer)

biruk1230
  • 3,042
  • 4
  • 16
  • 29