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

Jenkins Parallel Stage无法解析变量问题求助

Jenkins并行阶段变量解析失败的解决方案

问题原因

Groovy脚本里,单引号包裹的字符串是纯字面量,不会解析内部的Groovy变量;只有双引号(或三重双引号)包裹的字符串才会自动识别并替换变量。你修改后的并行阶段中,sh命令用了单引号,导致${configuration_template}没被Groovy解析,直接传给shell后,shell找不到这个变量就会被解析为空,最终命令变成--configuration=-en。

而原非并行阶段用的是三重双引号""" ... """,属于双引号字符串范畴,所以变量能正常替换。

解决方法

把并行阶段中sh命令的单引号改成双引号(或三重双引号)即可,以下是几种可行的修改方式:

方式1:改用双引号包裹命令

stage('Build apps') {
  dir("$new_workspace"){
    timeout(45) {
      try {
        parallel(
          "Build -en" : {
            sh "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-en --output-path=dist-en"
          },
          "Build -fr" : {
            sh "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-fr --output-path=dist-fr"
          }
        )
      } finally {
         sh 'mv dist-en/* dist-fr/* dist/fa-presentation'
       }
    }
  }
}

方式2:字符串拼接变量

如果担心双引号内部特殊字符冲突,也可以用字符串拼接的方式传递变量:

sh 'node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=' + configuration_template + '-en --output-path=dist-en'

方式3:保持三重双引号写法

和原代码风格一致,用三重双引号包裹命令:

sh """node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=${configuration_template}-en --output-path=dist-en"""

验证效果

修改后,${configuration_template}会被Groovy正确解析为你定义的${configurationPrefix}-vm,最终执行的命令会变成:

node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --configuration=xxx-vm-en --output-path=dist-en

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 07:18:19