rp346
rp346

Reputation: 7068

Jenkinsfile access variable inside parameter

I am trying to create Parameter in Jenkinsfile which is default to BUILD_NUMBER

pipeline {
    agent { label 'windows' }

    options {
        copyArtifactPermission("${JOB_NAME}");
    }

    parameters {
        string(
            name: 'DEPLOY_BUILD_NUMBER',
            defaultValue: "${env.BUILD_NUMBER}",
            description: 'Fresh Build and Deploy OR Deploy Previous Build Number'
        )
    }

    stages {
        stage ('Build') {
            steps {
                sh '''
                   echo "Building Project"
                   Echo "Packaging into tar.gz"
                '''
            }
            post {
                success {
                    archiveArtifacts artifacts: '*.tar.gz'
                }
            }
        }
        stage ('Deploy') {
            steps {
                echo "Deploying...."
                echo "${params.DEPLOY_BUILD_NUMBER}"
                echo "${env.BUILD_NUMBER}"
                script {
                    step ([$class: 'CopyArtifact',
                         projectName: '${JOB_NAME}',
                         filter: "*.tar.gz",
                         fingerprintArtifacts: true,
                         selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.DEPLOY_BUILD_NUMBER}"]
                         ]);
                }
            }
        }
    }
    post {
        success {
            script {
                currentBuild.displayName = "#${BUILD_NUMBER}"
            }
        }
    }
}

But it's actually printing not the build no

[Pipeline] echo
Deploying....
[Pipeline] echo
env.BUILD_NUMBER
[Pipeline] echo
144

What I need to change to get actual env.BUILD_NUMBER value when I refer to params.DEPLOY_BUILD_NUMBER.

Upvotes: 1

Views: 1971

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28864

You can resolve this by referring to the build number as env.BUILD_NUMBER in the parameters. Given your example code, it would look like:

parameters {
  string(
    name: 'DEPLOY_BUILD_NUMBER',
    defaultValue: "${env.BUILD_NUMBER}",
    description: 'Fresh Build and Deploy OR Deploy Previous Build Number'
  )
}

Note that env.BUILD_NUMBER is cast as a String (which is fine for what you want anyway), but if you need to do arithmetic operations on it for example, then you would need to do something like:

parameters {
  string(
    name: 'DEPLOY_BUILD_NUMBER',
    defaultValue: "${env.BUILD_NUMBER.toInteger() + 1}",
    description: 'Fresh Build and Deploy OR Deploy Previous Build Number'
  )
}

This would recast the env.BUILD_NUMBER to an Integer, add one to it, and then the string() specification in the parameter would cast it back to a string.

Now to actually use the DEPLOY_BUILD_NUMBER parameter, you need to refer to it from within the params map, like params.DEPLOY_BUILD_NUMBER.

steps {
  echo "${params.DEPLOY_BUILD_NUMBER}"
  script {
    step ([$class: 'CopyArtifact',
      projectName: '${JOB_NAME}',
      filter: "*.tar.gz",
      fingerprintArtifacts: true,
      selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.DEPLOY_BUILD_NUMBER}"]
    ]);
  }
}

Upvotes: 2

Related Questions