You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何从Groovy为带空格的Jenkins参数赋值?

Fixing Jenkins Groovy Script Parameter Passing with Spaces in Parameter Names

Got it, let's tackle this problem head-on. The issue here is almost certainly related to how Groovy parses the parameter name with spaces—if you're not treating it as a proper string literal, Jenkins won't recognize the parameter correctly. Here's how to fix it, depending on how you're triggering the job in your Groovy script:

1. For Jenkins Pipeline Scripts

If you're using the build step in a Pipeline, the fix is straightforward: wrap the parameter name in single or double quotes to ensure Groovy treats it as a single string.

Wrong (causes the issue):

// Groovy tries to parse "Release" and "Context" as separate variables, not a single parameter name
build job: 'YourTargetJob', parameters: [string(name: Release Context, value: 'Production Build')]

Correct:

// Quoting the parameter name tells Groovy to pass it as a single, space-inclusive string
build job: 'YourTargetJob', parameters: [string(name: 'Release Context', value: 'Production Build')]

2. For Jenkins Script Console Groovy (Direct API Calls)

If you're using Jenkins' core Java API to trigger builds (e.g., from the script console), you just need to pass the space-inclusive parameter name directly to the StringParameterValue constructor as a quoted string.

Working Example:

import hudson.model.StringParameterValue
import hudson.model.ParametersAction

// Get the target job
def targetJob = Jenkins.instance.getItemByFullName('YourTargetJob')

// Define parameters with the spaced name (quoted as a string)
def jobParams = [
    new StringParameterValue('Release Context', 'Staging Deployment')
]

// Trigger the build with the parameters
def buildFuture = targetJob.build(new ParametersAction(jobParams))
println "Triggered build #${buildFuture.number} with 'Release Context' parameter"

3. If You're Using a Map to Dynamically Generate Parameters

If you're building parameters from a map (common for dynamic workflows), make sure the map keys are quoted strings that include the space. Groovy will preserve the space when converting the map to parameter values:

import hudson.model.StringParameterValue
import hudson.model.ParametersAction

def paramMap = [
    'Release Context': 'Hotfix Build',
    'Another Parameter With Spaces': 'Sample Value'
]

// Convert map to Jenkins parameter objects
def paramsList = paramMap.collect { paramName, paramValue ->
    new StringParameterValue(paramName, paramValue)
}

// Trigger the job
Jenkins.instance.getItemByFullName('YourTargetJob').build(new ParametersAction(paramsList))

Why This Works

Jenkins fully supports parameter names with spaces—your original issue was a Groovy syntax problem. When you omit quotes around Release Context, Groovy interprets it as two separate identifiers (Release and Context) instead of a single string. By quoting the parameter name, you tell Groovy to pass the exact spaced string to Jenkins, which then matches it to your job's Release Context parameter.

If you're still running into issues, double-check that:

  • Your target job actually has a parameter named exactly Release Context (case-sensitive, no extra spaces)
  • You're using a supported Jenkins version (most modern versions 2.x+ handle spaced parameters without issues)

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

火山引擎 最新活动