2

The code below displays a list of all jobs in hudson:

 def projectlist = hudson.model.Hudson.instance
 projectlist.getItems(hudson.model.Project).each
 {
     job ->println(job.displayName)
 }

How can I get the name of the current job only?

davdomin
  • 1,219
  • 6
  • 18
  • 38
Rarm
  • 101
  • 1
  • 2
  • 9

3 Answers3

4

With the next code you can get the Job Name in a Execute system Groovy script

this.binding.build.project.name
davdomin
  • 1,219
  • 6
  • 18
  • 38
  • 1
    it works! , but still i am trying to get the value in a Dynamic Parameter with no luck – Rarm Sep 12 '13 at 14:11
2

You can do this in groovy code within a groovy build step - https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin

String jobName = System.getenv('JOB_NAME')

But it will also be available directly in any step in the job

e.g. shell

 echo "$JOB_NAME"

Taken from here How to get the job name on a groovy dynamic parameter in Jenkins

Community
  • 1
  • 1
KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
  • agreed. returns null – Andrew Gray Jun 22 '16 at 00:36
  • In a "Execute system Groovy script" task I see System.getenv('JOB_NAME') returns null. ```this.binding.build.project.name``` works (after ```import hudson.model.*```). Depending on where the groovy is called from (which plugin) you will see a different level of API parameters set or exposed so I'm guessing for ```System.getenv('JOB_NAME')``` YMMV depends on how early in job it is called and from what plugin it is called (as well as other things such as jenkins version, solar flare activity, . . . ) – gaoithe Aug 08 '18 at 15:08
1

If you're using Groovy script within "Env Inject", you can get current build and current job by:

currentJob.getName()
currentBuild.toString()
Noam Manos
  • 15,216
  • 3
  • 86
  • 85