Skip to content
Russell Keane
Main Navigation HomePosts

Appearance

Sidebar Navigation

2025

February (1)

Dynamically setting prev and next links in Vitepress

2019

November (1)

CloudFormation template failing to deploy

2018

March (1)

Subst'ing a drive in Windows 7 / 10

January (1)

Groovy - unable to resolve class

2017

October (2)

Reordering pages in LIFERAY forms

Spring / ZUUL - Failed to introspect annotated methods

June (1)

Sourcetree stuck on POST (chunked)

March (1)

Turning off Windows 10 ads

2016

December (1)

Failure to install / update ruby gems

July (2)

PostgreSQL - Casting integer to interval

piggly trace- invalid multibyte escape

June (1)

Jenkins stuck on "Retrieving Git references..."

March (1)

Jenkins & Groovy - accessing build parameters

2015

November (2)

Creating a Jenkins slave as a Windows service

Why Jenkins?

On this page

We're using Jenkins version 1.651 with the Groovy plugin for Jenkins version 1.4.

I had real trouble getting access to the build parameters when trying to run a groovy script build step using the recommended methods of which there are many.

From the plugin documentation:

groovy
    // Retrieve parameters of the current build
    def foo = build.buildVariableResolver.resolve("FOO")
    println "FOO=$foo"

This doesn't work.

From Jenkins help:

groovy
    def thr = Thread.currentThread()
    def build = thr.executable
    // get build parameters
    def buildVariablesMap = build.buildVariables
    // get all environment variables for the build
    def buildEnvVarsMap = build.envVars

    String jobName = buildEnvVarsMap?.JOB_NAME

This also doesn't work but I believe that's because the groovy script isn't running in the same process as the Jenkins job (or so I've heard).

I also tried the Jenkins wiki that suggested:

groovy
    import hudson.model.*

    // get current thread / Executor
    def thr = Thread.currentThread()
    // get current build
    def build = thr?.executable

    // get parameters
    def parameters = build?.actions.find{ it instanceof ParametersAction }?.parameters
    parameters.each {
       println "parameter ${it.name}:"
       println it.dump()
       println "-" * 80
    }
    // ... or if you want the parameter by name ...
    def hardcoded_param = "FOOBAR"
    def resolver = build.buildVariableResolver
    def hardcoded_param_value = resolver.resolve(hardcoded_param)

    println "param ${hardcoded_param} value : ${hardcoded_param_value}"

So, to the solution...

groovy
    import hudson.model.*
    def foobar = System.getenv("FOOBAR")

It's so simple it's painful.

Pager
Previous pageCreating a Jenkins slave as a Windows service
Next pageJenkins stuck on "Retrieving Git references..."

All views expressed are my own

Copyright © 2015-present Russell Keane