2013-03-02 37 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.") 
    } 
}