Reputation: 207
I always get jenkins failure:
Build step 'Execute Windows batch command' marked build as failure
but when I put in batch command: "echo Exit Code is %errorlevel%" I get 0 code.
My batch command is:
ELSE (
ROBOCOPY.EXE C:\dir2\ C:\dir1\ script.bat /IS
cd C:\dir1\
echo Exit Code is %errorlevel%
C:\dir1\script.bat
echo Exit Code is %errorlevel%
ROBOCOPY.EXE C:\dir1 C:\dir3 /E /IS /XF *.config
echo Exit Code is %errorlevel%
)
echo TEST
Command echo TEST is never shown. When I delete from batch "C:\dir1\script.bat" everything works fine and jenkins job is success.
Script bat contains only commands like:
del .\bin\library.dll
What do you think, the problem is with script.bat? When I add in the end of this script: EXIT /B 0 , the job is also failed.
Upvotes: 0
Views: 1708
Reputation: 80023
You would have to provide us with more than the half-statement here for a definitive answer. It may be that the )
before the else
is missing (it must be on the same line as the else
keyword).
More likely, you are victim number 48 million of the delayed expansion
problem. See endless examples on SO.
Within a block statement (a parenthesised series of statements)
, the entire block is parsed and then executed. Any %var%
within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block)
.
Hence, IF (something) else (somethingelse)
will be executed using the values of %variables%
at the time the IF
is encountered.
Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion
and use !var!
in place of %var%
to access the changed value of var
or 2) to call a subroutine to perform further processing using the changed values.
Hence, you are seeing the value of errorlevel
as it existed when the (presumed) IF
was parsed - before execution.
Upvotes: 0