Executing windows batch command in Jenkins keeps failing with Exit-1 state - windows

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?

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.

This windows batch script will work:
echo %DATE%
echo %Time%
echo "command executed succesfully!"

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

Related

How to run long msbuild command in groovy file in windows slave

I have the below msbuild deploy command which is working local:
msbuild "Database Services\Database Services.sqlproj" /t:deploy /p:TargetConnectionString="Data Source=TEST,111;Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False" /p:BlockOnPossibleDataLoss=False /p:TargetDatabase="test_db"
what is correct syntax to run it in jenkisfile.groovy?
I'm running against my windows machine, so I guess it should start with bat "msbuild ......."
I tried many syntax but they all failed
I'd advise to go multiline then, to have more flexibility and freedom
bat (
label: 'Run Tests',
script: """
set ASPNETCORE_ENVIRONMENT=Pipeline
dotnet test
if errorlevel 1 exit /b 1
"""
)
Note that for multiline you have to keep track of exit code of every line, so while it is not necessary here, you can split your other lines by this to make sure build stops on a first failure.
bat ' msbuild "Database Services\\Database Services.sqlproj" /t:deploy /p:TargetConnectionString="Data Source=TEST,111;Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False" /p:BlockOnPossibleDataLoss=False /p:TargetDatabase="test_db" '

Jenkins job is always failing after windows batch command execution script for rerun cucumber tests

I am running cucumber java tests in jenkins job using maven build as below
After its done in post step under windows batch command execution script I am rerunning failures
mvn clean verify -Dcucumber.options="#rerun.txt --tags #Retry1" -Dthreadcount="2"
After this step every time execution fails. I have read somewhere that if I add any of the below line it should pass but still it fails every time.
echo "exit script"
true
command || true
exit 0
Also added #!/bin/sh at start of script
Please advise.
Since this is supposed to be executed on Windows you may try
mvn clean verify -Dcucumber.options="#rerun.txt --tags #Retry1" -Dthreadcount="2" & echo note: ignoring exit code
or
cmd /c mvn clean verify -Dcucumber.options="#rerun.txt --tags #Retry1" -Dthreadcount="2" & echo note: ignoring exit code
(sorry, don't have Jenkins with Windows agent to test this at hand).

The shell script called inside 'Execute Shell' build step fails, but Jenkins build is marked as passed

I am calling a shell script with some parameters within 'Execute Shell' build step in Jenkins.
./shell_1.sh "$Param1" "$Param2" "$Param3"
This shell script downloads puppet modules, sets up the directories and then finally calls a puppet script.
puppet apply ${WORKSPACE}/scripts/puppet/sample.pp
The problem here is that Jenkins console shows the error from the puppet script and then finish with Success status.
Following are the last 3 lines from Jenkins console:
06:46:44 [1;31mError: /Stage[main]/Main/Deployit_environment[Environments/Build/Test/UAT/UAT-env]: Could not evaluate: cannot send request to deployit server 404/Not Found:Repository entity [Infrastructure/Build/Test/UAT/server_123/test-sample-service] not found[0m
06:46:44 [mNotice: Applied catalog in 1.82 seconds[0m
06:46:45 Finished: SUCCESS
I want the Jenkins job to fail if there is any error in puppet script.
I tried calling shell script with -xe option. But didn't work.
Thanks in advance.
The Jenkins Build step checks if the error code of the script is 0. If it has another value it will fail the build.
You can use special shell variable called $? to get the exit status of the previously executed command. To print $? variable use the echo command:
echo $?

Bat script pausing while performing long action

I'm running a bat script on a Windows 2008 server. The script is launching a jar file which is creating indexes in a database. Each index may take several minutes to create and during some of those creations, the command prompt pause. I have to hit enter so the script can continue running. I'm guessing this can be due to index creation being to long, the command prompt go to 'sleep' and it has to be woken up, be I couldn't find any meaningful information about such a behavior.
Is there any way to prevent the command prompt to go to 'sleep' like that?
Here the part of the script lauching the jar :
echo [INFO] Starting build SQL Index 4P8...>>%logFile%
set msg=[INFO] Creating SQL INDEX FILE...
echo %msg%
echo %msg%>>%logFile%
set UserName=
set PassWord=
"..\jre\bin\java.exe" -jar %toolFile% -port=%portNumber% -u=%UserName% -p=%PassWord%
if ERRORLEVEL 1 (
echo [FAILED] Failed to create SQL INDEX FILE, please see Java Logs...
echo [FAILED] Failed to update SQL INDEX FILE, please see Java Logs...>>%logFile%
exit 1
)
set msg=End of the application, please see logs !
echo %msg%
echo %msg%>>%logFile%
It's not really relevant though, as the pauses occur during the jar execution.

How to get Jenkins job to fail if phantomJS.exe call exits with an error

In a Jenkins job, I have a windows batch Command build step, which runs a test with phantomJS, like this:
How can I make the job fail, if inside smokeTest.js I exit phantom with an error, like this:
phantomJS.exit(1)
Jenkins considers a job as "failed" if the return-code of any command-block
(like that 'ExecuteWindows batch command' above) is non-zero.
If it does not work for you in this case, it is probably because 'phantomjs.exe' returns '0' in any case
(can confirm this by echo-ing the ERRORLEVEL just after that command: ECHO %ERRORLEVEL% ).
If this is the case (i.e.: it returns '0' even when fails), you can handle it this way:
Print a clear error-message to the console (or STDOUT - something that will show in the log of the Job)
Use the Text-finder Plugin to catch that error-message and mark the build as 'Failed'.

Resources