Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the job name on a groovy dynamic parameter?

Tags:

jenkins

groovy

I need get the name job in this example, I have a code for get the previous value of the build parameter with that code

jenkins.model.Jenkins.instance.getItem("nameOfJob").lastBuild.getBuildVariables().get("NameOfParameter");

The name of job now is hard coded, I need get this name will be the name of the current job. How can I do it?

like image 668
davdomin Avatar asked Sep 03 '13 20:09

davdomin


2 Answers

Following works for me from the uno-choice plugin parameter Groovy script (means before the build is bound):

def job = this.binding.jenkinsProject

If you're interested in the job name, then:

def jobName = this.binding.jenkinsProject.name
like image 62
valdemon Avatar answered Oct 11 '22 04:10

valdemon


Firstly be sure you use "Execute system Groovy script" step (not "Execute Groovy script").

The simplest answer for getting the job name is:

String JOB_NAME = build.project.name;

And just use JOB_NAME like you would in a shell script.

But since you actually need a job instance then use:

build.project.lastBuild.getBuildVariables().get("NameOfParameter");

BUT, to get parameters for current build (actually running at the moment) simply use:

// string
// (bool is also a string -- "true"/"false" string)
def someStr = build.buildVariableResolver.resolve("someBuildParam");
// mapping to int
def someInt = build.buildVariableResolver.resolve("someBuildParam") as int;

Class names in Jenkins documentation are a bit tricky, so here are some tips:

  • Any job is an AbstractProject.
  • Any build is an AbstractBuild.
like image 35
Nux Avatar answered Oct 11 '22 03:10

Nux