27
Nov

current build number in Jenkins declarative pipeline

pipeline {
  environment {
    FOO = "BAR"
    BUILD_NUM_ENV = currentBuild.getNumber()
    ANOTHER_ENV = "${currentBuild.getNumber()}"
  }

  agent any

  stages {
    stage("Environment") {
      steps {
        sh 'echo "FOO is $FOO"'
        // returns 'FOO is BAR'

        sh 'echo "BUILD_NUM_ENV is $BUILD_NUM_ENV"'
        // returns 'BUILD_NUM_ENV is X' depending on the build number
      }
    }
  }
}