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?
5 Answers
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"
-
1I 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
-
1I 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
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
-
nice and clean way to get just the name and not the whole Jenkins folder structure in one line, thanks =)– pelosJan 20, 2022 at 16:53
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.
-
1I don't have this entry - checked on two different Jenkins installations. May be you need a plugin for this? Please clarify.– Adrian WJan 24 at 18:04
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
-
How can you print out
Very_Cool_Jobs
? The use case is that if this is a multibranch job located within a folder thenCool_Jobs
if the Folder nameVery_Cool_Jobs
is the Job name andThe_Coolest
is the Branch name, so printing Job name becomes very tricky. Apr 15 at 21:30