简体   繁体   中英

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.

要使用可模板化的字符串,其中变量被替换为字符串,请使用双引号。

sh "echo $some_var"

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" 

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.

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 '''

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM