Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a variable from sh to groovy in a jenkins pipeline?

Tags:

sh

jenkins

groovy

How is it possible to export a variable from the sh context of sh context to the groovy context of the jenkins pipline job?

Pipeline Code:

node {
   echo 'Hello World'
   sh 'export VERSION="v$(date)"'
   echo env.VERSION
}

outupt:

[Pipeline] sh
[test-pipeline] Running shell script
++ date
+ export 'VERSION=vThu Dec  1 12:14:40 CET 2016'
+ VERSION='vThu Dec  1 12:14:40 CET 2016'
[Pipeline] echo`enter code here`
null

i am using Jenkins ver. 2.34

update: there is the possibility to write the variable to a temporary file and read it later. This looks totally like a hack to me. It is not "thread-safe" by default when using parallel builds and does not scale if you need to export multiple variable in one run. Is there a proper way to do this?

like image 869
HWM-Rocker Avatar asked Dec 14 '25 18:12

HWM-Rocker


1 Answers

I hope this will help.

node('master'){
    stage('stage1'){
    def commit = sh (returnStdout: true, script: '''echo hi
    echo bye | grep -o "e"
    date
    echo lol''').split()



    echo "${commit[-1]} "

    }
}
like image 54
Bibek Mantree Avatar answered Dec 18 '25 04:12

Bibek Mantree



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!