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

Jenkins共享库循环调用windows方法出现NoSuchMethodError错误求助

Jenkins循环调用自定义共享库方法报错解决方案

问题场景

我在Jenkins共享库vars/codeSigning.groovy中定义了如下签名方法:

void windows(String filePath) {
    String signFile = 'sign_windows.ps1'
    writeFile(file: signFile, text: libraryResource('sign_windows.ps1'))
    File signFilePath = new File(signFile)
    withCredentials([string(credentialsId: 'SM_API_KEY', variable: 'SM_API_KEY'), string(credentialsId: 'SM_CLIENT_CERT_PASSWORD', variable: 'SM_CLIENT_CERT_PASSWORD')]) {
        echo "Signing file: ${filePath}..."
        powershell label: "Signing file ${filePath}", script: """./${signFilePath} -FilePath "${filePath}" -CertificateName "COMPANY Incorporated" """
    }
}

单独调用该方法时完全正常:

stage('CTC - Pre Build Stage') {
   steps {
      script {
         codeSigning.windows('pathtofile')
      }
   }
}

但在循环遍历findFiles返回的文件列表时调用,Jenkins抛出错误:

java.lang.NoSuchMethodError: No such DSL method 'windows' found among steps [....] or globals [codeSigning, ...]

报错的Jenkinsfile代码:

stage('Sign SharedScripts') {
   steps {
      unstash 'SharedScripts'
      script {
         def filesToSign = []
         def foundFiles = findFiles(glob: "scripts/**/*")
         foundFiles.each { filename ->
            if ("${filename}".endsWith(".ps1")) {
               filesToSign.add(filename)
            }
         }
         filesToSign.each { fileToSign ->
            codeSigning.windows(fileToSign)
         }
      }
   }
}

问题原因

findFiles返回的是File对象(具体为hudson.FilePath实例),而非字符串类型。循环中直接将File对象传给codeSigning.windows(),但该方法仅定义了接收String类型参数的版本,Jenkins无法找到匹配的方法重载,因此抛出找不到方法的错误。

单独调用时传入的是字符串路径,类型匹配,所以可以正常执行。

解决方案

方案1:在调用时转换为字符串路径

修改循环中的调用代码,通过File对象的path属性获取字符串路径:

stage('Sign SharedScripts') {
   steps {
      unstash 'SharedScripts'
      script {
         def filesToSign = []
         def foundFiles = findFiles(glob: "scripts/**/*")
         foundFiles.each { filename ->
            // 直接用name属性判断后缀,更简洁
            if (filename.name.endsWith(".ps1")) {
               filesToSign.add(filename)
            }
         }
         filesToSign.each { fileToSign ->
            // 传入File对象的path属性(字符串类型)
            codeSigning.windows(fileToSign.path)
         }
      }
   }
}

方案2:扩展共享库方法,兼容File对象

在vars/codeSigning.groovy中添加接收File对象的方法重载,或者直接修改原方法兼容两种类型:

方式A:添加方法重载

// 原有的String参数方法保留
void windows(String filePath) {
    // 原代码不变
}

// 添加接收File对象的重载方法
void windows(File filePath) {
    // 调用原方法,传入转换后的字符串路径
    windows(filePath.path)
}

方式B:修改原方法兼容多种类型

void windows(def filePath) {
    // 自动判断参数类型,转换为字符串路径
    String path = filePath instanceof File ? filePath.path : filePath
    String signFile = 'sign_windows.ps1'
    writeFile(file: signFile, text: libraryResource('sign_windows.ps1'))
    File signFilePath = new File(signFile)
    withCredentials([string(credentialsId: 'SM_API_KEY', variable: 'SM_API_KEY'), string(credentialsId: 'SM_CLIENT_CERT_PASSWORD', variable: 'SM_CLIENT_CERT_PASSWORD')]) {
        echo "Signing file: ${path}..."
        powershell label: "Signing file ${path}", script: """./${signFilePath} -FilePath "${path}" -CertificateName "COMPANY Incorporated" """
    }
}

这样无论传入字符串还是File对象,方法都能正常执行。

内容的提问来源于stack exchange,提问作者Rasmus Østergaard Kjær Voss

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 01:20:15