Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 345
Mark Allison Avatar asked Jan 25 '17 13:01

Mark Allison


Video Answer


2 Answers

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".

like image 80
Marcin Kłopotek Avatar answered Oct 10 '22 05:10

Marcin Kłopotek


I worked it out. In case anyone finds it useful:

def jobBaseName = "${env.JOB_NAME}".split('/').last()
echo "Job Name (excl. path): ${jobBaseName}"
like image 30
Mark Allison Avatar answered Oct 10 '22 03:10

Mark Allison