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

Jenkins Groovy动态创建变量失败问题排查

Jenkins动态赋值后变量为null的排查与解决

问题现象

静态赋值方式下,validating - line0阶段能正确输出image_properties.line0的内容;但通过循环动态创建变量并赋值时,该阶段打印image_properties.line0为null,需要实现动态创建变量并跨阶段传递值。

相关代码

全局变量声明(位于pipeline{}之前)

def (image_properties, branch) = [ [image_tags:[]], null ]

静态赋值脚本(可正常运行)

stage('Read and assign value Groups') {
    steps{
        script {
            def build_properties = readProperties file: "file.txt"
            image_properties.line0 = build_properties.line0
        }
    }
}

stage('validating  - line0') {
    steps{
        script {
             container("${opts.container}") {
                 println("Content: ${image_properties.line0}")
             }
        }
    }
}

动态赋值脚本(存在问题)

stage('Read and assign value Groups') {
    steps{
        script {
            TotalCountline = sh ( script: 'cat file.txt|wc -l', returnStdout: true).trim().toInteger()
            def build_properties = readProperties file: "file.txt"
            for(i=0;i<TotalCountModelGroup;i++) {
                def var1="image_properties.line$i"
                def var2="build_properties.line$i"
                println (var1 + "="+ var2)
                var1 = var2
            }
        }
    }
}

stage('validating  - line0') {
    steps{
        script {
             container("${opts.container}") {
                 println("line Content: ${image_properties.line0}")
             }
        }
    }
}

file.txt内容

line0=[acme01, acme02, acme03, acme04]
line1=[acme11, acme12, acme13, acme14]
line2=[acme21, acme22, acme23, acme24]
line3=[acme31, acme32, acme33, acme34]

问题原因

动态赋值脚本的核心错误在于:

  • def var1="image_properties.line$i"只是定义了一个字符串变量,并非引用image_properties对象的属性;
  • var1 = var2只是给这个字符串变量重新赋值,完全没有对全局的image_properties对象进行修改,自然在后续阶段取不到值。

Groovy中,直接用字符串无法直接操作对象的属性,必须通过对象的属性操作方法或Map的键值对方式来动态修改。

解决方案

修改动态赋值阶段的代码,通过以下两种方式实现动态赋值:

方式1:使用Groovy对象的setProperty方法

stage('Read and assign value Groups') {
    steps{
        script {
            // 修正变量名不一致问题:TotalCountModelGroup替换为实际行数变量TotalCountline
            def TotalCountline = sh ( script: 'cat file.txt|wc -l', returnStdout: true).trim().toInteger()
            def build_properties = readProperties file: "file.txt"
            for(i=0;i<TotalCountline;i++) {
                def propName = "line$i"
                // 动态设置image_properties的属性
                image_properties.setProperty(propName, build_properties.getProperty(propName))
            }
        }
    }
}

方式2:利用Map的键值对操作(推荐)

因为image_properties本质是一个Map,直接通过键名赋值更简洁:

stage('Read and assign value Groups') {
    steps{
        script {
            def TotalCountline = sh ( script: 'cat file.txt|wc -l', returnStdout: true).trim().toInteger()
            def build_properties = readProperties file: "file.txt"
            for(i=0;i<TotalCountline;i++) {
                def key = "line$i"
                image_properties[key] = build_properties[key]
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 09:52:48