簡體   English   中英

如何從“執行系統Groovy腳本”中調用位於jenkins從屬服務器中的批處理/常規腳本?

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

問題:我有一個groovy腳本,通過該腳本可以獲取SVN變更集列表。 我可以在Execute system Groovy腳本上執行此操作,因為我可以訪問幫助我進行更改集的Hudson對象。 現在,我只想簽出從屬計算機上設置的更改。 我准備了一個批處理腳本(位於從屬服務器中),並試圖通過更改集的SVN URL逐個傳遞這對我不起作用來進行調用。

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中的命令行控制台? 請幫我

某種事情正在觸發您的Jenkins工作-您輪詢SVN存儲庫,或者您有SVN觸發器,如此處所述

通過兩種方式,您都可以通過配置

  • 源代碼管理:Subversion
  • 退房策略:盡可能使用“ 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()

但是在此階段,文件已經在您的構建機器上! 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.")
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM