5

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?

2
  • 2
    Please add the /Toption to the date command to suppress it asking for setting the new date on the command line. Maybe that`s the reason for your failing command. Commented Feb 25, 2018 at 14:40
  • You do not want to use a command that is going to prompt for input. date /T will suppress the output. The other option is echo %date% Commented Mar 13, 2019 at 1:56

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

11 Comments

No it still fails with (call), could you try to give me an example and also explain bit in detail on why the failure happens?
there is a space after call and before the right parentheses. That code will force the exit code to 0.
Any luck? please let me know the result. You said you tried (call) but I asked you to try (call ). Remember space matters in Batch.
But 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.
If 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.
|
1

This windows batch script will work:

echo %DATE%
echo %Time%
echo "command executed succesfully!"

Comments

1

The windows batch script show the same error to me. Finally i have added exit 0 at the end it worked

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.