Jenkins : Groovy Plugin Example

Example to show some variables available to theĀ system Groovy script

println "build.inspect() = " + build.inspect()
println "build.dump() = " + build.dump()
println "listener.inspect() = " + listener.inspect()
println "listener.dump() = " + listener.dump()

println "environment=" + build.getEnvironment(listener).dump()
println "JENKINS_HOME=" + build.getEnvironment(listener).get('JENKINS_HOME')

println "myVar=$myVar"
println "myVar=${myVar}"

This shows how to get a parameter if your job is parameterized

import jenkins.model.Jenkins
import hudson.model.Run
import hudson.model.ParametersAction

static def getParameter(parameters, paramName) {
    // The first instance found by parameters.find that matches paramName will cause the value of that instance to be returned
    parameters.find { == paramName }?.value

static def getParameters(Run build) {
    build?.allActions?.find { it instanceof ParametersAction }?.parameters;

println getParameter(getParameters(build), "myParameter")

An example of finding all pipeline jobs. It requires the pipeline plugin to be installed.

import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.job.WorkflowRun

(Jenkins.get().items?.findAll {
    it instanceof WorkflowJob
}?.findAll {".*_Pipeline")
}).each { WorkflowJob job ->
    job.builds.each { WorkflowRun build ->