简体   繁体   中英

Jenkins Windows Batch Command Powershell Environment Variables

I have a PowerShell script to be executed as a build step in jenkins and need to pass environment variable to it

powershell -File .\Build.ps1 -Version $env:APP_VERSION_NUMBER

The APP_VERSION_NUMBER is an environment variable set by Version Number Plugin of Jenkins.

For some reason the -Version parameter is never set, and I see only $env:APP_VERSION_NUMBER in console log output.

Is this a syntax issue?

When you use the PowerShell CLI's -File parameter , the arguments passed to the script are treated as literals , so, given that you're invoking the command line not from PowerShell , $env:APP_VERSION_NUMBER is not expanded.

To force the target PowerShell process to evaluate the arguments, you must use -Command rather than -File :

powershell -Command .\Build.ps1 -Version $env:APP_VERSION_NUMBER

However, now that we know that you're invoking the command line via cmd.exe (a batch file) from Jenkins (a build step of type Execute Windows batch command ), the simpler answer is indeed to let cmd.exe expand the environment-variable reference, using its %<envVarName>% syntax :

powershell -File .\Build.ps1 -Version "%APP_VERSION_NUMBER%"

Note: Enclosing the environment-variable reference in "..." isn't strictly necessary with a version number, but is a good habit to form, so that values with embedded spaces or other shell metacharacters are passed correctly too.

It turns out indeed its a syntax issue. The fix looks like the following

powershell -File ".\Build.ps1" -Version %APP_VERSION_NUMBER%

在此处输入图片说明

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