John
John

Reputation: 11831

How to replace part of a declarative Jenkinsfile pipeline variable to get the name of the job?

I want to get the name of the Jenkins job. The name is a Jenkins environmental variable called JOB_NAME (and which looks like project_name/branch). JOB_BASE_NAME strips off the project_name/, leaving branch.

I want to get project_name.

I thought there might be something like this I could do:

environment {
  NAME = "${env.JOB_NAME}.replaceAll(~/\/.*$/, '')
}

and then use ${NAME} later in the pipeline, but this doesn't seem to work

Upvotes: 0

Views: 1931

Answers (1)

Spencer Malone
Spencer Malone

Reputation: 1509

You can also do env.JOB_NAME - env.JOB_BASE_NAME and then drop one character off the end of that if you need to lose the final /.

Once you've defined something in your environment closure, it should be accessible not as a normal variable like NAME, but instead like the predefined env variables, such as env.NAME

Upvotes: 1

Related Questions