How to use environment variable in Jenkins Pipeline build?

86 views
Skip to first unread message

ZillaYT

unread,
Nov 8, 2018, 10:26:34 AM11/8/18
to Jenkins Users
I have a Jenkins Pipeline job which uses "Pipeline script from SCM" as my pipeline. One of this block's parameters is "Branch to build" of course. How can I used an environment variable for the text block? I tried, for example , $branchToBuild, ${branchToBuild} or "${branchToBuild}"  and it just takes those as literal values and does not extrapolate the string. I do have that variable defined and use it in other jobs.

Is it just not possible to do this?

ZillaYT

unread,
Nov 8, 2018, 11:56:38 AM11/8/18
to Jenkins Users
Someone suggested using ${env.branchToBuild}, so I tried env.branchToBuild, $env.branchToBuild, ${env.branchToBuild}, and "${env.branchToBuild}" all to NO avail.

Cuong Tran

unread,
Nov 8, 2018, 12:45:18 PM11/8/18
to Jenkins Users
Where do you define your "environment variable"?   

It should work as ${BRANCH_TO_BUILD} if you define BRANCH_TO_BUILD under global environment variables, folder, inside your pipeline, etc.

ZillaYT

unread,
Nov 8, 2018, 2:19:06 PM11/8/18
to Jenkins Users
It should work, but doesn't!

I define my environment variable in Manage Jenkins->System Configuration, Global Properties, Environment variables. I use the variable in other jobs so I know the variable definition works!

Thanks.

ZillaYT

unread,
Nov 8, 2018, 2:23:45 PM11/8/18
to Jenkins Users
Just to be clear, this is the text box in the job configuration page where I want to use an environment variable.

Screen Shot 2018-11-08 at 1.37.35 PM.png

Mark Waite

unread,
Nov 8, 2018, 2:53:03 PM11/8/18
to jenkins...@googlegroups.com
That's a good clarification.  I believe there is a known bug that the git plugin does not expand variable references in some of the configuration fields.  

Use a multibranch Pipeline instead, then you can let the Pipeline script expand the variable, and you won't have a confusing mix of changes in the history of the job.

Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/264ef2d4-3b6e-471c-8d98-40bc1e16496b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Thanks!
Mark Waite

ZillaYT

unread,
Nov 8, 2018, 2:59:18 PM11/8/18
to Jenkins Users
I got it answered in stackoverflow How to use environment variables in a Jenkins pipeline job? I just had to uncheck the "Lightweight checkout" box at the bottom of that block, and it worked!

Thanks!
Reply all
Reply to author
Forward
0 new messages