Access a Groovy variable from within shell step in Jenkins pipeline

前端 未结 4 1327
无人共我
无人共我 2020-12-05 09:59

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?

Si

相关标签:
4条回答
  • 2020-12-05 10:01

    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" 
    
    0 讨论(0)
  • 2020-12-05 10:15

    In order to Pass groovy parameters into bash scripts in Jenkins pipelines - Use this post here

    0 讨论(0)
  • 2020-12-05 10:21

    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.

    0 讨论(0)
  • 2020-12-05 10:25

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

    sh "echo $some_var"
    
    0 讨论(0)
提交回复
热议问题