How to get the jobname from jenkins - jenkins

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?

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.

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"

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

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.

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

Related

How to put JOB_NAME excluding folder into an environment variable in Jenkins?

I want to put the name of the currently executing Jenkins job into an environment variable for use later in my pipeline, without the folder name. I'm assuming I need something like :
withEnv(['JOB_BASE_NAME=JOB_NAME.split('/').last()']) {
echo "Job base name: ${JOB_BASE_NAME}"
}
but I get an error:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
unclassified method java.lang.String div java.lang.String
In Jenkins documentation, you have the §Using environment variables section which mentions:
The full list of environment variables accessible from within Jenkins Pipeline is documented at localhost:8080/pipeline-syntax/globals#env, assuming a Jenkins master is running on localhost:8080
If you follow the link you can find that JOB_BASE_NAME is already provided OOTB by Jenkins so this is exactly what you want.
JOB_BASE_NAME -
Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo".
I worked it out. In case anyone finds it useful:
def jobBaseName = "${env.JOB_NAME}".split('/').last()
echo "Job Name (excl. path): ${jobBaseName}"
might be too late, but thought would help someone in need for a simpler solution using bash.
${JOB_NAME%%/*}
Example:
I have a project created with name: poc_jenkins_pipeline
If i try accessing default jenkins variable ${JOB_NAME}
it returns value poc_jenkins_pipeline/develop i.e. <project name>/<branch name>
By using % operator in bash ${JOB_NAME%%/*} returns value poc_jenkins_pipeline
Reference Link - https://qa.nuxeo.org/jenkins/pipeline-syntax/globals
If you want the name of Jenkins job without the folder name you can use:
def job = "${JOB_BASE_NAME}"

Jenkins - How to read the environment variables in groovy post build step

I am trying to read the environment variables in Groovy Postbuild step. I am able to read the values of parameters passed to builds but unable to read the values of parameters which are set in one of my Execute Windows batch command.
In one example of my Execute Windows batch command I do this:
SET custom_param=myValue
if I use ${custom_param} in other jenkins steps/jobs, it gets my value. So I am sure it has the value. I just can't get it in groovy script
I have tried followings to do so, none of them have worked:
manager.envVars['custom_param']
build.buildVariableResolver.resolve('custom_param')
build.getEnvironment(listener).get('custom_param')
Any help here would be great
(Assuming you're not running your script in groovy sandbox)
Try the bellow:
build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["YOUR_ENV_VAR"]
Groovy Post build step run as separate process. It has access to environment as normal JVM process.
You could use EnvInject plugin as a a build step. Subsequent steps in build will able to read this via normal environment access (System.env in your groovy script)
When you set some custom variables in your "Windows command batch" step, these variables are available only during this Jenkins step.
Once Jenkins move on the next step, your variables are lost...
If you want to set some variables permanently, you can try to use the SETX command:
What is the difference between setx and set in environment variables in Windows?

Passing variable from shell script to jenkins

I trigger a shell script from Jenkins, This scripts get date and export it as a environment(Linux) variable $DATE. I need to use this $DATE inside same Jenkins job. I made job as parameter build. Created a string parameter as DATE value as DATE=$DATE. But it is not working.
Please suggest !!
You mention that you are exporting a DATE environment variable in an shell script, which is presumably being started via an "Execute shell" step.
The problem is, once the shell step has completed, that environment is gone — the variables will not be carried over to subsequent build steps.
So when you later try to use the $DATE value — whether in another build step, or as a parameter to another job — that particular environment variable will no longer exist.
What you can do instead is use the EnvInject plugin to export environment variables during a build. Variables set up using this plugin will be available to all subsequent build steps.
For example, you could write the DATE to a properties field in one build step:
echo DATE=$(date +%Y-%m-%d) > env.properties
Then you can add an "Inject environment variables for your job" build step, and enter env.properties in the "Environment Properties File Path" field.
That way, the DATE variable (and anything else in that properties file) will be exported and will be visible to the rest of the build steps.
You could use an assignment statement and sh's returnStdout to get the value in Jenkins without having to write to a properties file.
foo = sh(
returnStdout: true,
script: 'date'
)
Then later on in the Jenkinsfile you can use $foo like any other variable.
EDIT: This is for a pipeline job, not a freestyle job.
I had the same issue.
The solution that worked for me was:
env.ABC=bat(returnStdout: true,
script: ''' #echo off echo abc ''').trim()
The .trim() and #echo off is important if you want to reuse the variable in another batch script.

Need to get jenkins build number

In my Jenkins job configure section I am calling an expect script to run.I need to get the current build number value in a variable in the expect script. is it possible to get the build number in a variable?
example. in my Jenkins job i am calling an expect script sample_text.exp in which i need to get current build number in a variable Build_no
is it possible?
yes, it is possible. There is environment variable BUILD_NUMBER. What you mean by "Jenkins job configure section"? In "Execute Windows batch command" in (build section) you can do:
echo %BUILD_NUMBER%
to get this number in a different variable:
set "BUILD_NO=%BUILD_NUMBER%"
echo %BUILD_NO%
Very much possible. You can use the environment variable, BUILD_NUMBER for that in execute shell section. To get a list of all the environment variables that one can use, go to
<your_jenkins_url>/env-vars.html
For example:
http://my-jenkins-box:8080/env-vars.html
Logic that I spoke off in my below comment
if [ -z ${JOB_URL} ]; then
echo "*************************** This is not a Jenkins build ***********"
# Do your stuff here :)
else
echo "**************************** This is a jenkins build so maven project is built before this script is executed via Jenkins ***************************"
fi
Also, remember that if you are running your script with sudo, these environment variables will not be available then

How to pass parameters to Hudson job's shell commands

I have a Hudson job that execute shell script on a remote server.
Its shell command is:
/usr/bin/deployWar.sh ${warfileName}
I marked this build as parameterized, and added a string parameter:
name: warFileName
default value: none
description: name of war file
When I run it, the parameter gets assigned, but it get passed into the shell script.
Parameterized Build Jenkins plugin documentation states that
all the environment variables added by parameters are in upper case
In your case this should work:
/usr/bin/deployWar.sh ${WARFILENAME}
There is nothing wrong in your approach. How do you know it's not passed to the shell script? The console log will show the "execute shell" command line.
Please paste the whole console log in here
For me the accepted answer did not work. ${WARFILENAME} using the brackets did not find the parameter. The way it seems to work only for shell scripts execution in Jenkins is $WARFILENAME (without the brackets).
This tutorial helped.
Everywhere else in jenkins options used ${WARFILENAME}.

Resources