Dirk
Dirk

Reputation: 10151

Access a Groovy variable from within shell step in Jenkins pipeline

Using the Pipeline plugin in Jenkins 2.x, how can I access a Groovy variable that is defined somewhere at stage- or node-level from within a sh step?

Simple example:

node {
    stage('Test Stage') {
        some_var = 'Hello World' // this is Groovy
        echo some_var // printing via Groovy works
        sh 'echo $some_var' // printing in shell does not work
    }
}

gives the following on the Jenkins output page:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test Stage)
[Pipeline] echo
Hello World
[Pipeline] sh
[test] Running shell script
+ echo

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

As one can see, echo in the sh step prints an empty string.

A work-around would be to define the variable in the environment scope via

env.some_var = 'Hello World'

and print it via

sh 'echo ${env.some_var}'

However, this kind of abuses the environmental scope for this task.

Upvotes: 68

Views: 156957

Answers (5)

akshay_sushir
akshay_sushir

Reputation: 1891

This is extension to @Dave Bacher's answer. I'm running multiple shell command in Groovy file & want to use output of one shell command to the next command as groovy variable. Using double quotes in shell command, groovy passes variable from one to another command but using single quotes it does not work, it returns null.

So use shell command like this in double quotes: sh "echo ${FOLDER_NAME}"

FOLDER_NAME = sh(script: $/
    awk -F '=' '/CODE_COVERAGE_FOLDER/ {gsub("\"","");print$2}' ${WORKSPACE}/test.cfg
  /$, returnStdout: true).trim()

echo "Folder: ${FOLDER_NAME}" // print folder name in groovy console

sh "mkdir -p ${WORKSPACE}/${FOLDER_NAME} && chmod 777 ${WORKSPACE}/${FOLDER_NAME}"
  

Upvotes: 0

Shubham Jain
Shubham Jain

Reputation: 17603

You need to do something like below if a bash script is required :

Set this variable at global or local(function) level where from these can be accessible to sh script:

def stageOneWorkSpace = "/path/test1"
def stageTwoWorkSpace = "/path/test2"

In shell script call them like below

sh '''
echo ''' +stageOneWorkSpace+ '''
echo ''' +stageTwoWorkSpace+ '''
cp -r ''' +stageOneWorkSpace+'''/qa/folder1/* ''' +stageOneWorkSpace+'''/qa/folder2
'''

Make sure you start and end sh with three quotes like '''

Upvotes: 10

BlackViper
BlackViper

Reputation: 73

I would like to add another scenario to this discussion. I was using shell environment variables and groovy variables in the same script.

 format='html'
    for file in *.txt; 
        do mv  -- "\$file" "\${file%.txt}.$format";
    done

So here, What I have done is use \$ only for shell environment variables and use $ for groovy variables.

Upvotes: 3

hetptis
hetptis

Reputation: 806

I am adding the comment from @Pedro as an answer because I think it is important.

For sh env vars we must use

sh "echo \$some_var" 

Upvotes: 27

Dave Bacher
Dave Bacher

Reputation: 15982

To use a templatable string, where variables are substituted into a string, use double quotes.

sh "echo $some_var"

Upvotes: 91

Related Questions