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

Jenkinsfile并行构建Docker镜像时列表转Map注入dir块报错如何解决?

问题原因

报错是由变量名与Jenkins Pipeline内置DSL方法冲突导致的:
你在遍历目录列表时,将迭代变量命名为了dir,而Jenkins原生提供了用于切换工作目录的dir()内置步骤。在闭包内部执行dir("${dir}")代码时,Groovy会优先将dir解析为你定义的字符串类型迭代变量,而非内置的dir步骤,尝试把字符串作为方法调用就会抛出对应异常。

解决方法

将迭代变量的名称修改为不与内置方法冲突的名称即可,例如修改为dirName,修改后的完整可运行Jenkinsfile如下:

#!groovy 

// Iterate over data_pipeline directory
// Store all directory pipelines if Dockerfile found in the variable map

def dirMap = [
    "docker_hub_image_metadata",
    "github_metrics",
    "google_sheets_sync",
    "jira_syn",
    "sfdc_get_license_keys_from_clouds",
    "sfdc_sync",
    "sfdc_sync_reports_to_snowflake",
    "snowflake_oppty_history",
    "snowflake_telemetry_tasks",
    "support_analytics",
    "sync_ee_license_from_s3"
] 

def builds = [:]
dirMap.each { dirName ->
    builds << [
        "${dirName}": { ->
            node {
                stage('Build') {
                    sh "echo 'Building Images for ${dirName}...'"
                    dir("${dirName}") {
                        def image = docker.build("mirantiseng/${dirName}")
                    }
                }
                stage('Test') {
                    sh "echo 'Testing Images for ${dirName}...'"
                }
                if (currentBuild.currentResult == 'SUCCESS') {
                    stage('Push') {
                        sh "echo 'Pushing Images for ${dirName}...'"
                    }
                }
            }
        }
    ]
}

parallel(builds)

注:额外修正了原代码中Puhing的拼写错误为Pushing


内容的提问来源于stack exchange,提问作者R. Barrett

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 20:45:05