简体   繁体   English

Jenkins使用groovy脚本在作业上设置了SCMCheckoutStrategy

[英]Jenkins set SCMCheckoutStrategy on jobs using groovy script

Migrating from TeamCity to Jenkins, I am using a groovy script on the jenkins console to set up all my jobs. 从TeamCity迁移到Jenkins,我在jenkins控制台上使用了一个时髦的脚本来设置我的所有工作。 I set up a job "template.example" with most of the required settings and run this code: 我使用大多数必需的设置来设置作业“ template.example”,然后运行以下代码:

void createJobs() {
    def jenkins = jenkins.model.Jenkins.getInstance()
    def template = jenkins.getItem("template.example")
    if (!template) {
        println("FATAL - template job not found")
        return
    }

    def projects = [ "projectname" : "svn://hostname/path" ]
    projects.each() { key, svn ->
        def jobid = "${key}.checkin"
        println "\nCreating job: ${jobid}"
        try {
            def job = jenkins.copy(template,jobid)
            job.scm = new hudson.scm.SubversionSCM(svn)
            job.save()
            println("Created job: ${jobid} ${svn}")
        } catch (Exception ex) {
            println("Failed to create job: ${jobid}\n    "+ex.toString())
        }
    }
}

My question is, I want the SCM Checkout Strategy to be something other than the default "use svn update as much as possible", ie. 我的问题是,我希望SCM结帐策略不是默认的“尽可能多地使用svn更新”。 "Emulate clean checkout by first deleting unversioned files..." and this does not get copied from the template.example project. “通过首先删除未版本控制的文件来模拟干净的结帐...”,并且不会从template.example项目中复制该内容。

I tried reading the SCM strategies using 我尝试使用以下方法阅读SCM策略

def strategies = jenkins.scm.SCMCheckoutStrategyDescriptor.all()
strategies.each() { k -> println("strategy: "+k.getDisplayName()

but this only returns one strategy "Default". 但这只会返回一个策略“默认”。 Where are the other strategies, and how can I set the one I need on my projects (there are over 100 of them so doing this manually is not an option). 其他策略在哪里,以及如何在项目中设置所需的策略(其中有100多个,因此手动执行此操作不是一种选择)。

I also need to set the svn username and password on each project somehow. 我还需要以某种方式在每个项目上设置svn用户名和密码。

An alternative approach might be to modify the svnURL on the copied example job.scm instead of replacing it, this may have the advantage of keeping the configured username+password as well. 一种替代方法是修改复制的示例job.scm上的svnURL而不是替换它,这可能还具有保留配置的用户名和密码的优点。 But how to do it in code? 但是,如何在代码中做到这一点呢?

Thanks, Ed. 谢谢,爱德。

The SCM strategy you are looking to change actually resides within the hudson.scm.SubversionSCM object as a hudson.scm.subversion.WorkspaceUpdater . 您要更改的SCM策略实际上作为hudson.scm.subversion.WorkspaceUpdater驻留在hudson.scm.SubversionSCM对象中。

The scm object in your code should have a setWorkspaceUpdater() method which you can use to set the SCM strategy you want. 代码中的scm对象应具有setWorkspaceUpdater()方法,可用于设置所需的SCM策略。

The SCM strategies are subclasses of WorkspaceUpdater: http://grepcode.com/search/usages?id=repo1.maven.org$maven2@org.jvnet.hudson.plugins$subversion@2.0.0@hudson$scm$subversion@WorkspaceUpdater&start=0&type=type&k=d SCM策略是WorkspaceUpdater:子类WorkspaceUpdater: http WorkspaceUpdater: //grepcode.com/search/usages? WorkspaceUpdater: repo1.maven.org$maven2@org.jvnet.hudson.plugins$subversion@2.0.0@hudson$scm$subversion@ WorkspaceUpdater&开始= 0&类型=类型&K = d

Ran into this problem myself when trying to copy builds for new code releases. 尝试复制新代码发布的版本时,自己遇到此问题。 Went through a lot of metaClass info to find the correct method for setting this behavior. 浏览了许多metaClass信息,以找到设置此行为的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM