Pass environment variables to subshell CMD

2019-07-31 13:25发布

I have a problem in my gitlab-ci.yml on Windows. I launch phpunit with environments variables.
So, I have a variable like:

PHPUNIT : %SOURCE_PATH%\cgi-bin\php.exe %PHPUNIT_PATH%

And some variables are declared before:

SOURCE_PATH: 'C:\Source'
PHPUNIT_PATH: '"%SOURCE_PATH%\cgi-bin\tests\__init\tools\phpunit.phar"'

But when I use the CALL command, Windows doesn't resolve the variable inside the other variable.

So if I do:

CALL Echo %PHPUNIT%

I have:

C:\Source\cgi-bin\php.exe "%SOURCE_PATH%\cgi-bin\tests\__init\tools\phpunit.phar"

I think this is because of %SOURCE_PATH% doesn't exist in CALL context.

But I could not find out how to pass the environment variables in the CALL. And I could not find another way to do this, too. (If I don't use CALL, the gitlab-ci stops when the inside script exits.)

I wish you can help me ...

Somethings to know.

My script is launched by gitlab-ci runner, so it is started by:

setlocal enableextensions
setlocal enableDelayedExpansion
set nl=^

And I can't change this.

I can use PowerShell if needed, or if you know another work around. :)

2条回答
太酷不给撩
2楼-- · 2019-07-31 13:50

This batch file demonstrates on execution the problem and offers a solution.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
set "SOURCE_PATH=C:\Source"
set "PHPUNIT_PATH="%%SOURCE_PATH%%\cgi-bin\tests\__init\tools\phpunit.phar""
echo %SOURCE_PATH%\cgi-bin\php.exe %PHPUNIT_PATH%
echo/
echo Reference to environment variable SOURCE_PATH in environment
echo variable PHPUNIT_PATH is not expanded on running php.exe.
echo/
echo Solution:
echo/
echo Explicitly set environment variable PHPUNIT_PATH once more with
echo its own value with using additionally the command CALL to expand
echo all variable references inside the variable value.
echo/
call set "PHPUNIT_PATH=%PHPUNIT_PATH%"
echo %SOURCE_PATH%\cgi-bin\php.exe %PHPUNIT_PATH%
echo/
endlocal
pause

Don't run SETLOCAL more than once for defining the command line environment with specifying the parameters EnableExtensions and EnableDelayedExpansion. Both can be specified on running SETLOCAL only once. See this answer for a detailed explanation what the commands SETLOCAL and ENDLOCAL do which should make it clear why it is not advisable to run SETLOCAL more often than really needed.

And enable delayed expansion only when really needed as exclamation marks in text to process or directory/file names or parameter strings are handled not anymore as literal character with delayed expansion enabled.

查看更多
Juvenile、少年°
3楼-- · 2019-07-31 13:54

Your Question is Not so much clear... But, I may guess that - Your inbetween Scripts are using 'Exit' at the end. SO, Replace it with Exit /B

Or Try to Use 'Start' Instead of Call... (Start /B ... - Check 'Start /?')

查看更多
登录 后发表回答