简体   繁体   English

如何从“执行系统Groovy脚本”中调用位于jenkins从属服务器中的批处理/常规脚本?

[英]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. 问题:我有一个groovy脚本,通过该脚本可以获取SVN变更集列表。 I execute this on Execute system Groovy script , because I has access to Hudson objects that helps me getting change set. 我可以在Execute system Groovy脚本上执行此操作,因为我可以访问帮助我进行更改集的Hudson对象。 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. 我准备了一个批处理脚本(位于从属服务器中),并试图通过更改集的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.
}
}

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? -至少我可以将更改集的SVN URL传递给jenkins中的命令行控制台? please help me 请帮我

Something is triggering your Jenkins job - you poll your SVN repository or you have a SVN trigger as descriped here . 某种事情正在触发您的Jenkins工作-您轮询SVN存储库,或者您有SVN触发器,如此处所述

In both ways you start your job with a configured 通过两种方式,您都可以通过配置

  • Source-Code-Management: Subversion 源代码管理:Subversion
  • Check-out Strategy: use 'svn update' as much as possible 退房策略:尽可能使用“ svn更新”

Then you start your Groovy system script: 然后,您启动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()

But at this stage the files are already on your build machine! 但是在此阶段,文件已经在您的构建机器上! The changeSet contains all items which have been in the svn update. changeSet包含svn更新中的所有项目。 So just use the path to process them. 因此,只需使用路径即可处理它们。 For example you could start a Jenkins job per changed file with: 例如,您可以使用以下命令为每个更改的文件启动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