Jenkins流水线Groovy脚本报错:无法解析hudson.model相关类
Jenkins流水线获取SVN版本号并传递到下游流水线的问题解决
问题场景
我正在开发Jenkins流水线A,通过Groovy脚本获取SVN仓库最新修订版本号并存入revision变量,流水线A执行成功后触发流水线B,让B将该版本号追加到POM文件的版本号标签中。但流水线A执行时出现异常。
遇到的错误
- 使用「Execute Groovy script」构建步骤时:
unable to resolve class hudson.model.StringParameterValueunable to resolve class hudson.model.ParametersAction
- 使用「Execute System Groovy script」构建步骤时:
java.lang.IndexOutOfBoundsException: index is out of range 0..-1 (index = 0)
脚本代码
import groovy.json.* import jenkins.model.* import hudson.model.* import hudson.util.* def revision = trunkRevision() def trunkRevision() { def ant = new AntBuilder() ant.exec(executable: "svn", outputproperty: "output", dir: "."){ arg(line: "info https://someURL/trunk -r HEAD")} svnInfo = ant.project.getProperty("output") def pattern = /Last\s+Changed\s+Rev:\s+(\d+)/ def matcher = (svnInfo =~ pattern) def actualRevision = matcher[0][1] def param = new hudson.model.StringParameterValue('actualRevision', actualRevision) def paramAction = new hudson.model.ParametersAction(param); def thr = Thread.currentThread(); def currentBuild = thr?.executable; currentBuild.actions.add(paramAction) return actualRevision
已尝试的解决方法
- 在「Execute Groovy script」构建步骤中运行脚本,指定Groovy版本和安装路径
- 在「Execute System Groovy script」构建步骤中运行脚本
- 安装了global-build-stats plugin等插件
所有操作均在Jenkins主节点运行,无代理节点。
问题分析与解决
1. 类无法解析的问题(Execute Groovy script)
「Execute Groovy script」是在独立Groovy环境中运行,无法直接访问Jenkins核心类(如hudson.model.StringParameterValue)。解决方式:
直接改用「Execute System Groovy script」,它运行在Jenkins的JVM中,能直接加载Jenkins核心类和插件类。
2. 数组越界错误(Execute System Groovy script)
这个错误是因为matcher[0][1]访问失败,说明SVN命令输出未匹配到预期的Last Changed Rev模式,可能原因:
- SVN命令执行失败(URL错误、权限不足、网络问题),导致
svnInfo为空或内容异常 - SVN输出语言非英文,正则表达式不匹配
解决步骤:
第一步:调试SVN命令输出
在脚本中添加打印语句,确认SVN命令是否成功执行:
svnInfo = ant.project.getProperty("output") println "SVN Info Output: ${svnInfo}" // 新增打印,查看输出内容
运行后查看构建日志,检查是否有SVN错误信息,以及输出中是否包含目标匹配串。
第二步:修复正则匹配逻辑
在访问匹配结果前,先判断匹配是否成功,避免数组越界:
def matcher = (svnInfo =~ pattern) if (matcher.find()) { def actualRevision = matcher.group(1) // 后续参数传递等逻辑 } else { throw new Exception("Failed to extract SVN revision from output: ${svnInfo}") }
如果SVN输出是中文,需要把正则改成/最后修改版本:\s+(\d+)/这类对应语言的匹配式。
第三步:可靠传递参数到下游流水线
当前给构建添加参数的方式,若要触发流水线B,更直接的方式:
- 用Jenkins「Build other projects」步骤时,直接在该步骤的参数设置中传递
actualRevision - 用Jenkinsfile流水线代码时,使用
build步骤传递参数:
build job: '流水线B', parameters: [string(name: 'actualRevision', value: actualRevision)]
优化后的完整System Groovy脚本
import jenkins.model.* import hudson.model.* def revision = trunkRevision() def trunkRevision() { def ant = new AntBuilder() // 捕获错误输出,方便排查问题 ant.exec(executable: "svn", outputproperty: "output", errorproperty: "error", dir: ".", failonerror: false) { arg(line: "info https://someURL/trunk -r HEAD") } def svnInfo = ant.project.getProperty("output") def svnError = ant.project.getProperty("error") if (svnError) { throw new Exception("SVN command failed with error: ${svnError}") } println "SVN Info Output: ${svnInfo}" // 根据实际输出语言调整正则 def pattern = /Last\s+Changed\s+Rev:\s+(\d+)/ def matcher = (svnInfo =~ pattern) if (!matcher.find()) { throw new Exception("Could not find revision in SVN output") } def actualRevision = matcher.group(1) // 给当前构建添加参数(可选,若需要在当前构建中保留该参数) def currentBuild = Thread.currentThread().executable def param = new StringParameterValue('actualRevision', actualRevision) def paramAction = new ParametersAction(param) currentBuild.actions.add(paramAction) return actualRevision }
内容的提问来源于stack exchange,提问作者Ramin B
相关产品推荐
相关产品推荐

