Reputation: 1219
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?
Upvotes: 14
Views: 50140
Reputation: 2490
def jobName = env.JOB_NAME
works fine for me on 2.176 Enterprise rolling.
This has the added benefit of being whitelisted so you can use it a locked down Jenkins.
Upvotes: 0
Reputation: 11
this works for me within a script pipeline's script block
${env.getEnvironment().get('JOB_NAME')}
Upvotes: 1
Reputation: 10002
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:
Upvotes: 1
Reputation: 89
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
Upvotes: 7
Reputation: 176
Groovy Dynamic parameters do not have access to the usual environment variables that the rest of the jenkins job is privy to.
Here is a working hack to get the job name:
def build = Thread.currentThread().toString()
def regexp= ".+?/job/([^/]+)/.*"
def match = build =~ regexp
def jobName = match[0][1]
Upvotes: 13
Reputation: 9075
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
Upvotes: 5