繁体   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