You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkinsfile参数默认值无法获取env.BUILD_NUMBER实际值问题

Fix: Jenkins Pipeline Parameter Default Value Shows Literal "env.BUILD_NUMBER" Instead of Actual Build Number

Got it, let's break down what's happening here and how to fix it quickly.

The Root Cause

Your issue is that in the parameters block of your declarative pipeline, wrapping ${env.BUILD_NUMBER} in double quotes tells Jenkins to treat it as a literal string rather than evaluating the variable. Declarative DSL doesn't automatically interpolate variables inside double quotes in the parameters section the way you'd expect in regular Groovy code.

The Fix

All you need to do is remove the double quotes around the default value and directly reference the env.BUILD_NUMBER variable. Since env.BUILD_NUMBER is already a string, no extra conversion is needed:

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

While you're at it, there's another small issue in your CopyArtifact step: you're using single quotes for '${JOB_NAME}', which also won't interpolate. Switch to using the env.JOB_NAME variable directly (or double quotes if you prefer):

projectName: env.JOB_NAME,

Full Modified Jenkinsfile

Here's the complete pipeline with both fixes applied:

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: env.JOB_NAME, 
                        filter: "*.tar.gz", 
                        fingerprintArtifacts: true, 
                        selector: [$class: 'SpecificBuildSelector', buildNumber: "${params.DEPLOY_BUILD_NUMBER}"] 
                    ]); 
                } 
            } 
        } 
    } 
    post { 
        success { 
            script { currentBuild.displayName = "#${BUILD_NUMBER}" } 
        } 
    } 
}

Why This Works

  • By removing the double quotes from defaultValue, Jenkins now references the actual env.BUILD_NUMBER variable value that's assigned when the pipeline initializes, instead of treating it as a static string.
  • Using env.JOB_NAME in the CopyArtifact step ensures Jenkins uses the current job's name correctly, avoiding any interpolation failures from single-quoted strings.

内容的提问来源于stack exchange,提问作者rp346

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:57:52