Jenkins Environment Variable String Interpolation for Setting Custom Workspace

1.3k views Asked by At

I am using a multibranch pipeline in Jenkins and I need to set the name of the custom workspace directory dynamically based on the branch name that is being built. When I define the custom workspace for the pipeline, I attempt to access the environment variable that contains the branch name as follows:

pipeline {

  agent {

    node {

      label 'master'

      customWorkspace 'some/path/${BRANCH_NAME}'

However, during git init of the workspace, the string interpolation does not happen. Instead of inserting whatever the branch name is into the string, it tries to set the workspace name as '${BRANCH_NAME}' without interpolating the branch name variable. If I interpolate the variable outside the agent block (for example, if I interpolate the variable in a stage block), the string interpolation occurs perfectly fine and I can receive the name of the branch from Jenkins.

What might be the cause of this problem? Are there any other ways to set a custom workspace based on the branch name in a multibranch pipeline?

Thanks!

1

There are 1 answers

1
Prathamesh Shenoy On

The string interpolation works with double quotes, but does not work with single quotes. When I change the syntax to the following, it works:

pipeline {

  agent {

    node {

      label 'master'

      customWorkspace "some/path/${BRANCH_NAME}"

Notice the double quotes used around the customWorkspace definition.

Apparently, Groovy cannot execute string interpolation on single-quoted strings. For some reason, however, string interpolation in single-quoted strings works outside the agent block (such as in a stage block), but refuses to work in the example above.