如何在Jenkins Pipeline中跨作业传递变量entaClient至字符串参数
Hey there, let's fix that variable interpolation issue in your Jenkins Pipeline!
The core problem here is how Groovy handles string interpolation—only double-quoted strings will replace ${variable} with its actual value, while single-quoted strings treat everything as literal text. Your current code uses single quotes for taskFilterName and tasksGroup, so ${entaClient} isn't being substituted. Also, there's a missing closing quote in your tasksGroup parameter that would cause a syntax error.
Here's the corrected version of your stage:
stage('Restart the Enta API') { steps { script { if (params.hotReload.contains('Hot Reload')) { build(job: 'Hot Reload (utility)', parameters: [ string(name: 'region', value: params.region), string(name: 'cluster', value: params.cluster), string(name: 'taskFilterName', value: "enta-${entaClient}-dev-api-"), string(name: 'tasksGroup', value: "enta-${entaClient}-dev-api-a,enta-${entaClient}-dev-api-b"), string(name: 'containerInstanceIdsToStartOn', value: params.containerInstanceIdsToStartOn) ] ) } } } }
What changed:
- Swapped single quotes for double quotes on
taskFilterNameandtasksGroupvalues. This triggers Groovy's string interpolation, so${entaClient}will be replaced with the actual value of your variable. - Fixed the missing closing quote in
tasksGroup—your original code cut off mid-string, which would have caused the pipeline to fail immediately. - Cleaned up the formatting a bit to make the parameter list easier to read (always a good practice for maintainability!).
Quick check:
Make sure entaClient is properly defined and accessible in this part of the pipeline. If it's set in an earlier stage or script block, confirm it's not scoped to a local context that this stage can't reach. If entaClient might be empty, you could add a quick validation step to avoid generating invalid filter/group names, but that's up to your specific use case.
内容的提问来源于stack exchange,提问作者eekfonky

