如何在 Jenkins 中使用 Groovy 脚本设置颠覆工作区格式?

Posted

技术标签:

【中文标题】如何在 Jenkins 中使用 Groovy 脚本设置颠覆工作区格式?【英文标题】:How to set subversion workspace format with a Groovy script in Jenkins? 【发布时间】:2018-07-17 22:47:08 【问题描述】:

我正在尝试以编程方式更改 SubversionSCM 插件的 Subversion 工作区格式 (Img)。当然,我一直在尝试使用 groovy 脚本,但我找不到任何方法。

我能够通过在 Groovy 控制台中运行此脚本来检索当前格式:

import jenkins.model.*

def inst = Jenkins.getInstance()
def desc = inst.getDescriptor("hudson.plugins.git.GitSCM")
desc =inst.getDescriptor("hudson.scm.SubversionSCM")

println(desc.getWorkspaceFormat())

这会打印出正确的 31。它是“svnkit”的“ISVNWCDb”接口中找到的“WC_FORMAT_18”成员的值。你可以在插件here的git仓库中看到它被使用了。

搜索颠覆插件的文档我找不到任何设置它的方法,也找不到SubversionSCM descriptor中的任何公共方法。

有没有办法以编程方式配置该设置。我更喜欢 groovy 脚本,但目前什么都可以。

【问题讨论】:

【参考方案1】:

我在同样的事情上苦苦挣扎,发现了一些似乎可行的方法。 正如您已经发现的那样,SubversionSCM 描述符上没有工作空间格式的设置器,但工作空间格式作为私有字段存在。

这似乎对我有用:

def required_format = 31 //31 is SVN 1.8 WF.
svn_desc = instance.getDescriptor("hudson.scm.SubversionSCM")
if(svn_desc.getWorkspaceFormat() != required_format)

    Field wf = desc.getClass().getDeclaredField("workspaceFormat")
    wf.setAccessible(true) //Private field -- make it public
    wf.set(desc, required_format)
    wf.setAccessible(false) //And make it private again

这在带有 Subversion 插件 2.10.6 的 Jenkins 2.122 中有效

【讨论】:

以上是关于如何在 Jenkins 中使用 Groovy 脚本设置颠覆工作区格式?的主要内容,如果未能解决你的问题,请参考以下文章

如何在同一个 Jenkins 节点中加载另一个 groovy 脚本?

Jenkins 在 groovy 脚本中检索主动选择参数

如何在 Jenkins Email-Ext 中引用 groovy 脚本

如何在 Jenkins/Hudson 中以编程方式设置环境变量?

如何使用 groovy 模板在 Jenkins 中发送电子邮件?

Jenkins上的Groovy:Plot插件