简体   繁体   中英

How to call batch/groovy script located in jenkins slave from “Execute system Groovy script”?

Problem: I have a groovy script using which I get list of SVN change set. I execute this on Execute system Groovy script , because I has access to Hudson objects that helps me getting change set. Now I want to checkout only that change set on my slave machine. I prepared a batch script (located in slave) and tried to call that by passing the SVN URL from change set one by one which is not working for me.

import hudson.model.*
import hudson.util.*
import hudson.scm.*

// work with current build
def build = Thread.currentThread()?.executable
def changeItems = build.changeSet.items
def changes = []
changes += changeItems as List
changes.each { item ->
println("changes")
item.paths.each{ fileEntry ->
fileEntry.value ---->Pass this to script so It can be checked out in slave m/c.
}
}

Question : -- Is there any way to solve the above issue? -- At least can I pass the SVN URL from change set to command line console in jenkins? please help me

Something is triggering your Jenkins job - you poll your SVN repository or you have a SVN trigger as descriped here .

In both ways you start your job with a configured

  • Source-Code-Management: Subversion
  • Check-out Strategy: use 'svn update' as much as possible

Then you start your Groovy system script:

import hudson.model.*
import hudson.util.*
import hudson.scm.*

// work with current build
// (does only work as groovy system script, not in the Jenkins shell)
def build = Thread.currentThread()?.executable

// for testing, comment the line above and uncomment the job line
// and one of the build lines - use specific build (last build or build by number)
//def job = hudson.model.Hudson.instance.getItem("<your job name>") 
//def build = job.getLastBuild()   
//def build = job.getBuildByNumber(162)   

// get ChangesSets with all changed items
def changeSet= build.getChangeSet()
def items = changeSet.getItems()

But at this stage the files are already on your build machine! The changeSet contains all items which have been in the svn update. So just use the path to process them. For example you could start a Jenkins job per changed file with:

void startJenkinsJob(jobName, param)
{
    // Start another job
    def job = Hudson.instance.getJob(jobName)
    def anotherBuild
    try {
        def params = [
            new StringParameterValue('StringParam', param),
        ]
        def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
        println "Waiting for the completion of " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
        anotherBuild = future.get()
    } catch (CancellationException x) {
        throw new AbortException("${job.fullDisplayName} aborted.")
    }
    println HyperlinkNote.encodeTo('/' + anotherBuild.url, anotherBuild.fullDisplayName) + " completed. Result was " + anotherBuild.result

    // Check that it succeeded
    build.result = anotherBuild.result
    if (anotherBuild.result != SUCCESS && anotherBuild.result != UNSTABLE) {
        // We abort this build right here and now.
        throw new AbortException("${anotherBuild.fullDisplayName} failed.")
    }
}

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