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

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" '

Related

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).

How to return exit status of Matlab to Jenkins console output?

So far,I have tried using Jenkins to trigger matlab and do some test.But I want to let Jenkins know that if we have some error in matlab test,and communicate with Jenkins.So the console output will be failure.
Matlab version:2016b
So I put exit(1) in the last line of matlab script.But Jenkins console output still reveal SUCCESS.And I use %errorlevel% in batch command line,the result was 0 not what I expect.
I have tried use Start /wait in the beginning.Because I use Windows 10.But isn't help.
Below is my Jenkins batch command:
start /wait matlab.exe -wait -r -sd "D:\matlab" MyScript;exit -logfile OutputPrint.txt echo ExitCode is %errorlevel%
I expect that if some test cases in test manager didn't pass.It could inform Jenkins and the console output will be failure.
Thanks in advance for any help!
It seems that official support for providing an exit or error status from matlab was introduced in R2019a. I say this, because I see it in the online reference in R2019a, but it is missing in R2018b:
https://www.mathworks.com/help/releases/R2019a/matlab/ref/quit.html
vs
https://www.mathworks.com/help/releases/R2018b/matlab/ref/quit.html
However when I try it and do not give the 'force' input, it does seem to work for older versions of matlab, e.g. for R2016b:
"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
echo %ERRORLEVEL%
This gives me 10, which is what I expected. In your code, I see a couple of things that may give issues. I recommend to start with something simple as in my code and then add additional components if necessary and verify the errorlevel is still ok.
I see you're using start, this may block the exit code from reaching Jenkins
I see
-r -sd "D:\matlab" MyScript;exit
since the command to execute should come directly after -r, I think you meant:
-sd "D:\matlab" -r MyScript;exit
Finally because of the command "echo" after the matlab command, the errorlevel will be from the echo command. So if you want Jenkins to get the errorlevel from the MATLAB command, but you do want to run something after matlab, you should probably capture it in a separate variable and "reset" the errorlevel just before exiting the script, e.g.:
"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
SET MATLABERROR=%ERRORLEVEL%
echo ExitCode is %ERRORLEVEL%
SET ERRORLEVEL=%MATLABERROR%

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

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

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.

Having a batch file check its own output?

I'm wondering if it's possible to have a batch file check itself for a string.
I'm using a batch file to run Maven commands,and I want to check if anything failed by searching for a "FAILURE" string at the end of the script
I know you can FIND in other files, but can you have it check the current output on itself, or is the best solution to save the batch file output as a text and then search it?
As an example, if I had a batch file echo Hello World it would print Hello World, then I would want to search the output for Hello and tell me it found the string Hello.
I like vane's idea to take action upon the return code provided by mvn, but instead of using ERRORLEVEL, I like to use the || operator. The commands after || are only excecuted if the prior command failed.
::initialize error flag to undefined
set "mvnErr="
::run your Maven command and define the error flag if there was an error
call mvn {your arguments here} || set mvnErr=1
::You can now take action if there was an error
if defined mvnErr echo there was a Maven error
You can do this by checking the errorlevel after each Maven command. For example
#ECHO OFF
set hasErrors=0
REM execute maven command here
if not errorlevel 0 set hasErrors=1
REM more batch command and similar checking ...
if %hasErrors%==1 (
echo print your error info or do whatever
)
Building off of both the 2 previous answers, I think this gives the best of both worlds. The || syntax is short and easy to read and the IF statement before every command ensure processing only progresses when previous operations were successful.
set hasErrors=0
IF %hasErrors%==0 call mvn -f ./mycoservices/pom.xml install || set hasErrors=1
IF %hasErrors%==0 call mvn -f ./mycostatic/pom.xml install || set hasErrors=1
IF %hasErrors%==0 call mvn -f ./mycoweb/pom.xml install || set hasErrors=1
IF %hasErrors%==0 call mvn -f ./mycowebbundle/pom.xml install || set hasErrors=1
why not just exit the batch whenever it fail
if %ERRORLEVEL% NEQ 0 (
echo exit /b %errorlevel%
exit /b %errorlevel%
)

Resources