Jenkins为什么无法使用可扩展选择/系统常规选择参数找到作业对象?

时间:2018-11-05 21:44:51

标签: jenkins groovy jenkins-plugins jenkins-pipeline

我跟随Groovy to list all jobs获得了我的工作名称列表,并得到了我工作的确切“全名”,即...

Microservice/build/feature%2Fdev-26387-split-micro-deploy

现在我要尝试获取此工作的所有成功内部版本号。返回值仅用于调试以查看我要返回哪个值,并且我正在返回一个带有数字“ 43”的列表,这意味着getJobs()函数返回了null,这意味着它无法找到工作对象。为什么?

import hudson.model.*

BUILD_JOB_NAME = "Microservice/build/feature%2Fdev-26387-split-micro-deploy"

def getJobs() {
    def hi = Hudson.instance
    return hi.getItems(Job)
}

def getBuildJob() {
    def buildJob = null
    def jobs = getJobs()
    (jobs).each { job ->
        if (job.fullName == BUILD_JOB_NAME) {
            // WHY IS THIS NOT WORKING???
            buildJob = job
        }
    }
    return buildJob
}

def getAllBuildNumbers(Job job) {
     try {
        def buildNumbers = []
        (job.getBuilds()).each { build ->
            def status = build.getBuildStatusSummary().message
            if (status.contains("stable") || status.contains("normal")) {
                buildNumbers.add(build.number)
            }
        }
        /// return buildNumbers
        return ["44"]
    }
    catch (Throwable t) {
        return ["45"]
    }
}

def buildJob = getBuildJob()
if (buildJob == null) {
    return ['43']
}
return getAllBuildNumbers(buildJob)

1 个答案:

答案 0 :(得分:1)

改为使用return hi.getAllItems(Job),它应该可以工作。另外,确保将您的BUILD_JOB_NAME设置为正确的作业名称。要检查作业名称,请在Jenkins-> Manage Jenkins->脚本控制台中运行Hudson.instance.getAllItems(Job).each { println(it.fullName) }