Jenkins Groovy snippets

Материал из noname.com.ua
Перейти к навигацииПерейти к поиску

Upstream Job (вызвовшая текущую)

node() {
    stage('downstream') {
        def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
        echo upstream?.shortDescription
    }
}

Получение тектовых параметров от билда (например в downstream job что бы не передавать их

import hudson.model.*
import hudson.maven.*
import hudson.tasks.*
import jenkins.model.Jenkins
import hudson.maven.reporters.*
import hudson.plugins.emailext.*

def job_name = "some-existing-job-name"
def build_number = 12345 // existing build

def getParametersFromBuild(jobName, buildNumber, parameterType) {
    // Parameter type is class, possible values are
    // StringParameterValue
    // TextParameterValue
    def job_parameters = [:]
    // Get Job class by fullName
    //println(jobName)
    def job_instance = Jenkins.getInstance().getItemByFullName(jobName, hudson.model.Job.class)
    //println("job: " + job_instance +  "\n")
    //println(job_instance.getClass().getName())

    // Get build instance using build number
    // build nummbaer can be obtained from upstream job
    job_build_instance = job_instance.getBuildByNumber(buildNumber)
    //println("build_instance:" + job_build_instance)
    //println(job_build_instance.getAllActions().getClass().getName())
    job_all_build_instance_parameters_action = job_build_instance.getAllActions().findAll{ it instanceof ParametersAction }
    //println("job_all_build_instance_parameter_actions: " + job_all_build_instance_parameters_action)
    //println(job_all_build_instance_parameters_action.getClass().getName())

    //job_build_instance_parameters_action is list of classes 
    job_all_build_instance_parameters_action.each { parameters_action ->
        //println("parameters_action: " + parameters_action)
        //println(parameters_action.getClass().getName())
        // Get all parameters from ParametersAction
        all_parameters = parameters_action.getAllParameters()
        // list all parameters
        //all_parameters.each { generic_parameter ->
            //println(generic_parameter)
            //println(generic_parameter.getClass())
            //println(generic_parameter.getClass().getName())
        
        //}
        if (parameterType == "string") {
            all_parameters.findAll{ it instanceof hudson.model.StringParameterValue }.each { string_parameter ->
                //println("string_parameter: " + string_parameter)
                //println(string_parameter)
                //println(string_parameter.toString())
                //str = string_parameter.toString()
                //println(str)
                string_parameter_name = string_parameter.getName()
                string_parameter_value = string_parameter.getValue()
                job_parameters[string_parameter_name] = string_parameter_value
                //println(string_parameter.getDefinition())
                //println("------------------")

            
            }
        } else if (parameterType == "text") {
            all_parameters.findAll{ it instanceof hudson.model.TextParameterValue }.each { text_parameter ->
                // Not implemented
                println("text_parameter: " + text_parameter)
                println(text_parameter)
            
            }
        } else {
            return [:]
        }
    }
    return job_parameters
}

upstream_job_text_parameters = getParametersFromBuild(job_name, build_number, "string")
println(upstream_job_text_parameters)
upstream_job_text_parameters.each { k, v -> 
    println("=====")
    println("KEY=   " + k)
    println("VALUE= " + v)
    println("=====")
}