2013-03-02 386 views
1

问题: 我有一个groovy脚本,使用它我得到SVN更改集列表。我在上执行此操作执行系统Groovy脚本,因为我可以访问Hudson对象来帮助我获取更改集。现在我只想在我的奴隶机器上结账。我准备了一个批处理脚本(位于slave),并尝试通过从变更集中逐个传递SVN URL来调用该脚本,这对我来说并不合适。如何从“Execute system Groovy script”调用位于jenkins slave中的batch/groovy脚本?

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

问题: - 有什么办法来解决上述问题? - 至少我可以将更改集中的SVN URL传递给jenkins中的命令行控制台吗? 请帮我

回答

0

有什么东西触发你的詹金斯的工作 - 你轮询您的SVN仓库或者你有一个SVN触发作为descriped here

在这两种方式,你将配置

  • 源代码,管理开始你的工作:颠覆
  • 退房策略:使用“SVN更新”尽可能

然后你开始你的Groovy系统脚本:

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

但是在这个阶段,文件是al准备在你的机器上! changeSet包含已经在svn更新中的所有项目。所以只需使用路径来处理它们。例如,您可以使用以下方式为每个更改的文件启动Jenkins作业:

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