Jenkins声明性管道中上游多分支作业的上次成功构建修订版

时间:2019-04-17 07:05:54

标签: jenkins jenkins-pipeline

我想获得最新的上游作业成功构建的构建修订。上游作业是多分支作业。

到目前为止,我正在生成上游作业名称列表作为触发器。但是我似乎找不到合适的调用方法。

import jenkins.model.Jenkins

def upstreamPackages = ['foo', 'bar']
def upstreamJobs = upstreamPackages.collect { "${it}-multibranch/master" }.join(',')

pipeline {
    agent none

    triggers {
        upstream(upstreamProjects: upstreamJobs,
             threshold: hudson.model.Result.SUCCESS)
    }

    stages {
        stage('test'){
            steps{
                script {
                    upstreamJobs.each {
                        println it
                        job = Jenkins.instance.getItem(it)
                        job.getLastSuccessfulBuild()

                        revision = job.getLastSuccessfulBuild().changeset[0].revision
                        println revision
                    }
                }
            }
        }
    }
}

这将导致item的空对象。正确的方法是什么?

更新1 发现Jenkins脚本控制台和此comment之后,我设法提出以下建议:

import jenkins.model.Jenkins
import hudson.plugins.git.util.BuildData

def upstreamPackages = ['foo', 'bar']
def upstreamJobsList = upstreamPackages.collect { "${it}-multibranch/master" }.join(',')

@NonCPS
def resolveRequirementsIn(packages){
    BASE_URL = 'git@github.com:myorg'
    requirementsIn = ''

    packages.each { pkg ->
        revision = getLastSuccessfulBuildRevision("${pkg}-multibranch")
        requirementsIn <<= "-e git+${BASE_URL}/${pkg}.git@${revision}#egg=${pkg}\n"
    }

    println requirementsIn
    return requirementsIn
}

@NonCPS
def getLastSuccessfulBuildRevision(jobName){
    project = Jenkins.instance.getItem(jobName)
    masterJob = project.getAllItems().find { job -> job.getName() == 'master' }

    build = masterJob.getLastSuccessfulBuild()
    return build.getAction(BuildData.class).getLastBuiltRevision().sha1String
}


pipeline {
    agent { label 'ci_agent' }

    triggers {
        upstream(upstreamProjects: upstreamJobsList,
             threshold: hudson.model.Result.SUCCESS)
    }

    stages {
        stage('Get artifacts'){
            steps{
                script{
                    requirementsIn = resolveRequirementsIn upstreamPackages
                    writeFile file: 'requirements.in', text: requirementsIn
                }

            }
        }
    }
}

抛出错误:

an exception which occurred:
    in field org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript.script
    in object org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript@56d1724
    in field groovy.lang.Closure.delegate
    in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@27378d57
    in field groovy.lang.Closure.delegate
    in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@6e6c3c4e
    in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@5d0ffef3
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@5d0ffef3
Caused: java.io.NotSerializableException: 
org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject

1 个答案:

答案 0 :(得分:0)

问题在于Jenkins的Pipeline DSL要求所有分配的对象都是可序列化的。

Jenkins.instance.getItem(jobName)返回不可序列化的WorkflowMultiBranchProjectJenkins.instance.getItem(jobName).getItem('master')对象WorkflowJob都不是。

因此,我始终将调用链放到需要用链式方法调用替换变量分配的调用链上,并提出以下解决方案。

def upstreamPackages = ['foo', 'bar']
def upstreamJobsList = upstreamPackages.collect { "${it}-multibranch/master" }.join(',')

def String requirementsInFrom(packages){
    final BASE_URL = 'git@github.com:myorg'
    requirementsIn = ''

    packages.each{ pkg ->
    revision = Jenkins.instance.getItem("${pkg}-multibranch")
        .getItem('master')
        .getLastSuccessfulBuild()
        .getAction(BuildData.class)
        .getLastBuiltRevision()
        .sha1String

        requirementsIn <<= "-e git+${BASE_URL}/${pkg}.git@${revision}#egg=${pkg}\n"
    }

    return requirementsIn.toString()
}