-1

I'm trying to write a Jenkins pipeline file which uses groovy and bash script and I would like to print/use some Jenkins environment variables inside a bash script block.

I want use the sh script block with triple single quotas(to prevent the need to escape all dollar signs etc.):

sh '''
    code
'''

Instead of triple double quotas(in this script block jenkins env variables can be used with enough escapes):

sh """
    code
"""

My attempts to print $WORKSPACE inside triple double quota block:

script {
        sh '''
        #!/bin/bash
        echo 1 ${WORKSPACE}
        echo 2 '${WORKSPACE}'
        echo 3 ${workspace}
        echo 4 '${workspace}'
        echo 5.1 $WORKSPACE
        echo 5.2 \$WORKSPACE
        echo 5.2.1 \\$WORKSPACE
        echo 5.2.2 \\\$WORKSPACE
        echo 5.2.3 \\\\$WORKSPACE
        echo 5.2.4 \\\"$WORKSPACE\\\"
        echo 5.2.5 \\"$WORKSPACE\\"
        echo 5.2.6 \"$WORKSPACE\"
        echo 5.2.6 "$WORKSPACE"
        echo 5.3 '\$WORKSPACE'
        echo 5.3.1 \'$WORKSPACE\'
        echo 5.3.2 \'${WORKSPACE}\'
        echo 5.4 """$WORKSPACE"""
        echo 5.5 "$WORKSPACE"
        echo 5.5.1 \"$WORKSPACE\"
        echo 5.5.2 "\"${WORKSPACE}\""
        echo 5.5.3 \\"${WORKSPACE}\\"
        echo 5.5.4 \\\"${WORKSPACE}\\\"
        echo 5.5.4 \\\"${WORKSPACE}\\\"
        echo 5.6 "\$WORKSPACE"
        echo 6 '$WORKSPACE'
        echo 7 $env.WORKSPACE
        echo 8 '$env.WORKSPACE'
        echo '$workspace'
        '''
    }

Output:

14:31:52  1
14:31:52  2 ${WORKSPACE}
14:31:52  3
14:31:52  4 ${workspace}
14:31:52  5.1
14:31:52  5.2
14:31:52  5.2.1 $WORKSPACE
14:31:52  5.2.2 $WORKSPACE
14:31:52  5.2.3 \
14:31:52  5.2.4 ""
14:31:52  5.2.5 ""
14:31:52  5.2.6 
14:31:52  5.2.6 
14:31:52  5.3 $WORKSPACE
14:31:52  5.3.1 $WORKSPACE
14:31:52  5.3.2 ${WORKSPACE}
14:31:52  5.4 
14:31:52  5.5 
14:31:52  5.5.1 
14:31:52  5.5.2 
14:31:52  5.5.3 ""
14:31:52  5.5.4 ""
14:31:52  5.5.4 ""
14:31:52  5.6 
14:31:52  6 $WORKSPACE
14:31:52  7 .WORKSPACE
14:31:52  8 $env.WORKSPACE
14:31:52  $workspace

Expected output for at least some of these:

/home/my_user/jenkins_workspace/workspace/Jenkins_debug_job
Saims
  • 1
  • 1
  • You did not set the environment variable `WORKSPACE` prior to invoking script. Otherwise, the first one should have done the job. To double-check that this is the reason, do at the start of your bash script a `printenv WORKSPACE` (no `$`!!!). – user1934428 Jul 25 '22 at 13:39
  • println(workspace), println(WORKSPACE) and println(env.WORKSPACE) print the correct workspace path when called outside the bash script block. – Saims Jul 25 '22 at 14:26
  • The single quotes prevent Groovy from expanding anything in the shell script. You can use triple double quotes, but then you have to escape any dollar signs which should be exposed to the shell. There is no way to have _some_ dollar signs mean "this is a Groovy variable" and others not. – tripleee Jul 26 '22 at 04:57
  • @Saims . What do you see when you do the check I suggested? – user1934428 Jul 26 '22 at 06:12
  • @user1934428 Nothing is printed, not even a newline. – Saims Jul 26 '22 at 06:50
  • That's kind of good, because you now know that the problem is not so much a quoting problem as how to access the environment variable from your bash code. You simply know now that no such environment variables exists. Therefore I suggest that you post a new question which targets on the problem that the environment variable is missing in the sub process. Show in your code, how you set it, so that we have a reproducible example. – user1934428 Jul 26 '22 at 07:01

1 Answers1

-1

try

sh """
echo ${env.WORKSPACE)
"""
Josh Beauregard
  • 2,498
  • 2
  • 20
  • 37