I am testing a simple build process in Jenkins; The Build section has Execute Windows batch command
echo %ERRORLEVEL%
cscript C:\Users\user\Documents\test.vbs
echo %ERRORLEVEL%
My test.vbs does nothing except set the ERRORLEVEL environment variable; I also tried using wscript.quit to see if that had an effect on ERRORLEVEL
dim wShell
dim wSysEnv
set wShell = Wscript.CreateObject("WScript.Shell")
set wSysEnv = wShell.Environment("SYSTEM")
WScript.Echo "ERRORLEVEL=" & wSysEnv( "ERRORLEVEL" )
wSysEnv( "ERRORLEVEL" ) = 0
WScript.Echo "ERRORLEVEL=" & wSysEnv( "ERRORLEVEL" )
wSysEnv( "ERRORLEVEL" ) = 2
WScript.Echo "ERRORLEVEL=" & wSysEnv( "ERRORLEVEL" )
WScript.Quit 3
There is no post build action The console output is as follows:
Started by user anonymous
Building in workspace C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace
[workspace] $ cmd /c call C:\Users\user\AppData\Local\Temp\hudson4571524647235360597.bat
C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>echo 1
1
C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>cscript C:\Users\user\Documents\test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
ERRORLEVEL=0
ERRORLEVEL=0
ERRORLEVEL=2
C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>echo 1
1
C:\Program Files (x86)\Jenkins\jobs\TADS Host-Agent client\workspace>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
As shown in the output above, the ERRORLEVEL before I start the script is 1.
The script successfully sets ERRORLEVEL to 2 and exits with a code of 3. However when I check the ERRORLEVEL at the end of the script, it is back to 1 as it started.
I found some pages that said environment variables are only set for the current process and any child process; that is they are session environment variables.
In which case how can ERRORLEVEL be set for Jenkins to report a test as success or failure? It doesn't appear to check the variable during runtime, only at the start of the execution of the script. This is not useful as the script may change the value of the variable.