简体   繁体   中英

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. I set up a job "template.example" with most of the required settings and run this code:

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. "Emulate clean checkout by first deleting unversioned files..." and this does not get copied from the template.example project.

I tried reading the SCM strategies using

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).

I also need to set the svn username and password on each project somehow.

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. 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 .

The scm object in your code should have a setWorkspaceUpdater() method which you can use to set the SCM strategy you want.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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