Dejell
Dejell

Reputation: 14337

Retrieve all jenkins parameters to iterate in groovy code

Based on this answer, I would like to iterate over all the properties in jenkins job and replace with their value in the job in build step.

e..g.

file = new File("folder/path/myfile.cs")
fileText = file.text;
fileText = fileText.replaceAll("parameter", "parametervalue");
file.write(fileText);

I saw an example to resolve one param like this:

build.buildVariableResolver.resolve("myparameter")

But I need

  1. To iterate over all of them
  2. To do it in Build step (after pulling from Source Code) so in the current build.

How can I do it?

Upvotes: 0

Views: 2361

Answers (1)

carlo.bongiovanni
carlo.bongiovanni

Reputation: 141

The following is a sample groovy code that you can use with the scriptler plugin, that I suggest to use. This code can correctly read the Environment variables that are set in any Jenkins job:

import jenkins.model.Jenkins

def thr = Thread.currentThread()
def build = thr?.executable
def env = build.getEnvironment()
def buildNumber = env.get("BUILD_NUMBER")

In case you want to read the Build Parameters (you have a parameterized build and need access to those variables), you need to use the previous code and add the following:

def bparams = build.getBuildVariables()
def myBuildParam = bparams.get("MY_BUILD_PARAMETER")

after you read them, you can write them to your file, in a build step occurring after the git cloning.

Upvotes: 2

Related Questions