0

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?

Community
  • 1
  • 1
Dejell
  • 13,947
  • 40
  • 146
  • 229

1 Answers1

2

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.

  • BUILD_NUMBER is an environment variable available in any job. How to read it is written in the code I posted, if using the groovy scriptler plugin. The code you posted is only usable from the build flow plugin. Basically each plugin has its own api exposed, therefore you have a different syntax. – carlo.bongiovanni Nov 20 '14 at 07:48
  • Thank you. I may express my question more. The build has build parameters - I need to retrieve them. is it considered as build environment variables? – Dejell Nov 20 '14 at 08:20
  • I am looking for a way to iterate over all the environment dynamically and inject them – Dejell Nov 20 '14 at 08:45
  • Ok, I extended the answer so you can now read whatever you want :). To iterate over all of them, you can just take the object that you read with groovy and do what you want (that's trivial). – carlo.bongiovanni Nov 20 '14 at 09:20