68

Is there a way to get the jobname for the current build in jenkins and pass it as a parameter to an ant build script?

1
  • 1
    For me, both JOB_NAME and PROJECT_NAME return a longer version of the job name that includes the path from jenkins root. What is the short way?
    – djangofan
    Jan 13, 2020 at 23:11

5 Answers 5

102

Jenkins sets some environment variables such as JOB_NAME (see here) for more details on the variables set.

You can then access these in ant via ${env.JOB_NAME}.

Edit: There's also a little howto for environment variables on the same page here.

0
20

A similar issue, I was looking for job name for shell script.

In the 'Execute shell' > 'Command' textbox, both the below worked for me:

echo $JOB_NAME  
echo "$JOB_NAME"
3
  • 1
    I ran echo $JOB_NAME on ubuntu box having jenkins installed but getting blank output Mar 20, 2017 at 11:34
  • # echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/bin/phpunit:/var/lib/jenkins Mar 20, 2017 at 11:39
  • 1
    I was talking about, within Jenkins, not outside the jenkins. Open Jenkins, create a job, then In the 'Execute shell' > 'Command' textbox, write the above two, both will work. Job mentioned above is within the Jenkins, not environment variable outside jenkins. Mar 20, 2017 at 17:43
2

You may set special variable for that based on global variable. Simple:

THEJOB="${JOB_NAME.substring(JOB_NAME.lastIndexOf('/') + 1, JOB_NAME.length())}"

Now $THEJOB is your job name

1
  • nice and clean way to get just the name and not the whole Jenkins folder structure in one line, thanks =)
    – pelos
    Jan 20, 2022 at 16:53
2

If you can run any Job, you can easily go to the Build section of that job and go to environment variables and see all the information there.

enter image description here

1
  • 1
    I don't have this entry - checked on two different Jenkins installations. May be you need a plugin for this? Please clarify.
    – Adrian W
    Jan 24 at 18:04
0

Nowadays there is an environment variable JOB_BASE_NAME which contains the last component of JOB_NAME.

For example: if JOB_NAME contains Cool_Jobs/Very_Cool_Jobs/The_Coolest then JOB_BASE_NAME will just contain The_Coolest

1
  • How can you print out Very_Cool_Jobs ? The use case is that if this is a multibranch job located within a folder then Cool_Jobs if the Folder name Very_Cool_Jobs is the Job name and The_Coolest is the Branch name, so printing Job name becomes very tricky. Apr 15 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.