Executing windows batch command in Jenkins keeps failing with Exit-1 state
Solution 1
Try to add (call )
to end of your Batch Command.
This will clear the errorlevel environment variable, there for force the build result to be successful.
If you want to check a specific command's result to determine the build result. Let's say you want to check dates
command.
(dates) || (exit %errorlevel% )
This will fail the build if error happens in the first command.
or
(dates) && (echo "command executed succesfully!")
This will show the message only when the first command successfully executed.
Then with the changed command, you do not need (call )
any more.
Solution 2
This windows batch script will work:
echo %DATE%
echo %Time%
echo "command executed succesfully!"
Gokulnath Kumar
Updated on August 21, 2022Comments
-
Gokulnath Kumar over 1 year
I am testing out "Jenkins ver. 2.89.4" in Windows10 Machine and I have configured a simple job to test out few things. Under Build section in Jenkins, I have used "Execute Windows Batch Command" and used the following two commands. Both the below commands executes fine in the command prompt but however the Jenkins Build job keeps getting failed with Exit 1 state.
date echo "SampleBuild job completed successfully."
Couldn't able to get the reason for the failure. And the following is what we see in console output.
Started by user Administrator Building in workspace C:\ProgramData\Jenkins\workspace\SampleBuildJob [SampleBuildJob] $ cmd /c call C:\WINDOWS\TEMP\jenkins6658106255890809140.bat
C:\ProgramData\Jenkins\workspace\SampleBuildJob>date The current date is: Fri 02/23/2018 Enter the new date: (mm-dd-yy) C:\ProgramData\Jenkins\workspace\SampleBuildJob>echo "SampleBuild job completed successfully." "SampleBuild job completed successfully." C:\ProgramData\Jenkins\workspace\SampleBuildJob>exit 1 Build step 'Execute Windows batch command' marked build as failure Finished: FAILURE
Can anyone tell me, what am I missing?
-
Gokulnath Kumar about 6 yearsNo it still fails with (call), could you try to give me an example and also explain bit in detail on why the failure happens?
-
Vincent Zhang about 6 yearsthere is a space after call and before the right parentheses. That code will force the exit code to 0.
-
Vincent Zhang about 6 yearsAny luck? please let me know the result. You said you tried (call) but I asked you to try (call ). Remember space matters in Batch.
-
Gokulnath Kumar about 6 yearsBut then it forces the result to exit with code 0. It doesn't even check whether it is a true success build.I am trying to understand what is wrong with the above commands and why does it give exit code 1 even when the results are successful. The solution which you have given forces the results to end with exit code 0 irrespective of the result.
-
Vincent Zhang about 6 yearsIf there are any error occurs before the (call ) is called, the build result will still be failed. The (call ) is only clearing value in the system environment variable "errorlevel". you can try to echo %errorlevel% to check that variable value after each step.
-
Gokulnath Kumar about 6 yearsThank you for the pointers. Please have a look at my recent build status. And before the (call ) there has been a failure and still the build got completed as successful.
-
Vincent Zhang about 6 yearsSorry, maybe my statement is wrong about "any error occurs before the (call ) is called, the build result will still be failed".
-
Vincent Zhang about 6 yearsSorry, I do not have windows environment right now, and made a grammar mistake in the code. Already fixed it, would you please try again?
-
Vincent Zhang about 6 yearsWould you please try again? I failed to find a windows env in a short time. So I simplified the command.
-
Vincent Zhang about 6 yearshow about removing the /b option? why the errorlevel was 0? If it keeps as 0, just exit 1 or something other than 0, instead of exit the variable.
-
Eyal Gerber over 2 yearsThanks. This solved the issue for me as well! I simply but (call ) at the end of the batch file and now my jenkins script passes.