If you are running a step in a project then there is an environment variable JOB_NAME
which in groovy you can access
String jobName = System.getenv('JOB_NAME')
If you want it post build then check this answer How to get specific information about the current build project in Jenkins with Groovy?
As per the additional question, to get access to the Jenkins Job name at the setup stage you need to use the EnvInject plugin which supports groovy - it needs to return a map
println "currentJob -->${currentJob}"
println "currentBuild --> ${currentBuild}"
[currentJob: currentJob, currentBuild: currentBuild]
This is the output when I use a shell job to echo the new environment
Started by user anonymous
[EnvInject] - Loading node environment variables.
[EnvInject] - Preparing an environment for the build.
[EnvInject] - Keeping Jenkins system variables.
[EnvInject] - Keeping Jenkins build variables.
[EnvInject] - Evaluation the following Groovy script content:
println "currentJob -->${currentJob}"
println "currentBuild --> ${currentBuild}"
[currentJob: currentJob, currentBuild: currentBuild]
currentJob -->hudson.model.FreeStyleProject@5ffe31a7[_xxx]
currentBuild --> _xxx #6
[EnvInject] - Injecting contributions.
Building on master in workspace /var/lib/jenkins/workspace/_xxx
[_xxx] $ /bin/sh -xe /tmp/hudson3812821436584477615.sh
+ echo _xxx #6
_xxx #6
+ echo hudson.model.FreeStyleProject@5ffe31a7[_xxx]
hudson.model.FreeStyleProject@5ffe31a7[_xxx]
Finished: SUCCESS
You need to do some string manipulation to separate the job and build