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

Jenkins Pipeline共享库列表遍历输出异常问题排查

问题原因

你在Groovy代码里直接把pushDestination列表插值到shell命令中时,Groovy的List对象会调用默认的toString()方法,输出格式为[元素1, 元素2]。shell的for循环会把这个带方括号的整体当成单一变量,因此只会循环一次,输出整个列表内容,而非逐个遍历元素。

修复方案

将Groovy列表转换成shell可识别的空格分隔字符串,用join(' ')方法处理列表即可:

修改后的shell代码段:

container('buildah') {
    sh """
        for currentDestination in ${pushDestination.join(' ')}; do
            echo "currentDestination: \$currentDestination"
        done
    """
}
原理说明

pushDestination.join(' ')会把列表元素用空格拼接成字符串,比如firstDestination secondDestination,此时shell的for循环能正确识别每个元素作为单独迭代项,最终输出你期望的结果:

currentDestination: firstDestination
currentDestination: secondDestination

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 06:58:13