I just joined a company that uses batch files to build a C++ project. The batch does all sorts of things (updates svn, which is now done by jenkins), creates build folders, deletes unnecessary files after building, copies library files to the build folder, etc.
My problem is Jenkins always considers the build successful, even when it´s not. The .bat file creates a file called errormake.txt when something goes wrong. How do I make jenkins read that and mark the build as a failure?
Also, is there any way I can find out the build folder Jenkins created from inside the .bat file (maybe send a variable when I call the batch file)?
This is the single line I'm currently using to call the .bat file:
call "C:\Source\BuildVersion\AppName\build_version.bat"
Edit: Also, this project is split up into several SVN repositories. %SVN_REVISION% is blank. How can I get the correct %SVN_REVISION% from the first repository (the main one)?
To answer each of your questions -
Jenkins always return "SUCCESS", even when the Job actually failed:
Jenkins sets the status of the Job, based on the return-code of the last command
that ran in each "Execute windows batch command" block.
If your last command is copy some.log D:,
Jenkins thinks everything is OK
(If the 'copy' command went fine...)
Use EXIT xx or EXIT /B xx, depending on your OS,
where 'xx' is some integer greater than zero.
How do I make Jenkins mark the build as a failure, based on a log-file:
Use the Text-finder Plugin, as mentioned by sdmythos_gr .
Is there any way I can find out the build folder Jenkins created:
There are a few parameters that are available as environment-variables
for each script that runs under Jenkins - see here for the list of those parameters:
Jenkins Environment Variables.
To your question:
%WORKSPACE% - The absolute path of the workspace
%BUILD_NUMBER% - The current build number, such as "153"
%BUILD_ID% - The current build id, such as "2005-08-22_23-59-59"
(YYYY-MM-DD_hh-mm-ss)
How can I get the correct %SVN_REVISION% from the first repository:
This answer is from the same link:
%SVN_REVISION% - For Subversion-based projects,
this variable contains the revision number of the module.
If you have more than one module specified, this won't be set.
Hope that helps
Jenkins use the windows error code to know whether a build failed or not.
You should return a value different from 0 when your build failed, with "exit /B 1" for example.
On "newer" versions of Windows (I tested on Server 2012 R2), put the following at the end of each Windows batch command:
#EXIT /b %ERRORLEVEL%
This will pass the error code that the cmd.exe received back to the caller (i.e. Jenkins). The "#" turns off echoing so you don't clutter up your log.
If you have multiple lines in the command and want to stop after the first failure, put the following after each line that you want to check (yes, this is not pretty):
#IF NOT %ERRORLEVEL% == 0 EXIT /b %ERRORLEVEL%
For example:
step1.exe
#IF NOT %ERRORLEVEL% == 0 EXIT /b %ERRORLEVEL%
step2.exe
#IF NOT %ERRORLEVEL% == 0 EXIT /b %ERRORLEVEL%
call "C:\Source\BuildVersion\AppName\build_version.bat"
#EXIT /b %ERRORLEVEL%
I'm also going to answer just part of your question.
There is a Text Finder plugin for Jenkins that you could use.
https://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin
You can mark the build as unstable or failed at the end of the build depending on the contents of a file or the console output.
Maybe this could help...
I know the question is quite older but may be useful to some people. To execute your bat file, instead of using following line,
call "C:\Source\BuildVersion\AppName\build_version.bat"
You can use below format,
<someRelativeOrAbsolutePath>\<.batFileName> <param1> <param2> <and so on>
Executing the command in this way inside Execute Windows Batch Command of Build section of Jenkins will use your last return code of the command. ${BUILD_STATUS} will depend on that. And you will not have to modify your script to return some condition based error codes.
As other users have stated your batch files should use "exit /B 1". Here is a trick to chain together your calls causing Jenkins to return a failure if one fails:
call a.bat &&^
echo "a success" &&^
call b.bat &&^
echo "b success"
"&&" denotes that the next action should only run on success (exit code 0). "^" lets us split the command into multiple lines. The downside to this approach is the build progress bar doesn't display accurately.
Related
I have a project which builds an application and whenever it builds the application the names it generates are like this MyApp1.1.exe, myapp1.2.exe, myApp,1.3.exe etc. I would like to deploy the application in another environment whenever there is a new build. But the problem is that I'm using the following in command in the batch script, which is is keep throwing me an error
MyApp1.*.exe
But it always throws an error in the command line saying that 'MyApp1.*.exe' is not recognized as internal or external command, operable program or batch file. I know that this should be very simple but I could not seem to find any solution
cmd, which is used to run batch-files, requires that you run for loops to do something for each item. You therefore need to give for some criteria and it will return the list based on that. You can then do something with these metavariables.
#echo off
for %%i in (MyApp*.*.exe) do echo start "" "%%~i"
For the purpose of this demonstration, I am not actually running the executable's, instead I just echo the full command. If you feel that it is suitable for this purpose, then simpy remove echo from the echo start "".. section.
I have SVN and Tortoise SVN installed in my machine. I have created a Batch file".bat". I am trying to get Log message to validate it that was provided in the UI before commit.
I tried this answer which was failing with following error.
svnlook: E720003: Can't open file 'C:\Users\GOPICH~1\AppData\Local\Temp\svnC891.tmp\format': The system cannot find the path specified.
Can anyone guide me what is the issue is (or) is there anyway to achieve the same.
I had issues with svnlook log as it appends the \format.
Arguments returned from stderr to the commit failed dialog are known as:
path depth messagefile cwd
These arguments are printed from using the code in this answer when commit fails.
The 1st and 3rd are .tmp files located in the temp directory. (The 1st is a list of files to commit).
The 3rd argument is the message file so you can use that to check if a message is used.
#echo off
findstr . "%~3" >nul
if errorlevel 1 echo args: %* >&2 & exit 1
exit 0
This causes commit failure if message file is empty else allows the commit to succeed.
Issues:
Only a single space in message file causes commit failure.
Only a double space in message file allows commit.
Seems findstr pattern could be improved. This could be a personal preference so keeping the linked answer pattern from the question.
Idea aschipfl shared in comments:
#echo off
if %~z3 gtr 0 (exit 0) else exit 1
Any character in message file makes the file not zero in size.
AFAIK, stdout is redirected as being a hook script, thus unavailable,
so #echo off is possibly not needed.
Note: Tested with SVN-client which may be why svnlook log
gave me strange results. Thanks to aschipfl for the information.
So, I have a program for the Windows Command Prompt, which is for changing the directory (so I can go to a language directory without having to do cd everytime)
and I want to kill it after I select an option. However, I have tried some
methods, which do one of the following:
a. taskkill /IM ... (blows up with a process not found error)
and
b. exit [as shown here] (does what I want, but it also closes the command prompt)
This is my program (the important part):
:C++
cd C:\Users\S.G.\Documents\C++ Scripts
echo What's in C++ Scripts:
dir
pause
exit
:Python
cd C:\Users\S.G.\AppData\Local\Programs\Python\Python36-32
echo What's in Python:
dir
exit
The reason why I'm stuck as to how one does this is because say I choose option "C++".
If I choose it, it runs what I have described, but it also runs the "Python" function. If I run the "Python"function however, it runs fine and doesn't display whatever's in the "C++" function.
Why is function "C++" also running "Python" when I intend not to?
You should use exit /b, followed by an optional error code (eg. exit /b 0). An alternative way to do this is to skip to the end of the file using GOTO:EOF.
I'm writing a CMD script to execute a Java program. The program requires at least one argument so I created an if else block to check that argument one %1 is not blank. When I run the script without providing argument one I get the expected you must provide an argument to run. But when I do provide the argument the program does not execute. Additionally if I don't have #echo off set, the entire script prints out as text in the window when I do provide the proper argument.
Here's the full script:
set APP_HOME=C:\Temp\Hammer
rem Set APP_HOME to the place where you installed Hammer
if ["%1"] == [""] (
echo you must provide a python script to run
) else (
java -cp %APP_HOME%\lib\jython.jar;%APP_HOME%\lib\hammer.jar;%CLASSPATH% bridenstine.hammer.main.Main %1 %2
)
I think this is a problem with the script and not the program itself because when I run the line that's in the else block by itself without the script,
java -cp C:\Temp\Hammer\lib\jython.jar;C:\Temp\Hammer\lib\hammer.jar;%CLASSPATH% bridenstine.hammer.main.Main argument1
The program executes normally. I've been looking at example scripts and cross referencing this site for CMD files and what I have seems to be valid. Do I have a syntax error?
Update:
I'm running the script like so,
cd C:\Temp\Hammer
bin\ProgramScript.cmd argument1
Update 2:
Someone pointed out that when they run this script they get an error message saying Java is not recognized as an internal or external command (the expected message when Java is not set on their PATH) But they made a good point that this means the script is in fact getting inside the else block. I then pointed out the following,
After I run the script with a valid argument it prints out the entire script as text on the command prompt. I am then able to mark the line from inside the else statement (that was printed), copy it, paste it, and it runs the program fine. So the Java command seems to be valid. But you make a good point that the script is obviously getting inside the else block...something is still going wrong here and it doesn't seem to be the program.
Note:
If relevant I am using Windows 8.1 and I am using the standard command prompt, not one that has administrative privileges (the results remained the same regardless of using a command prompt with administrative privileges).
I suggest to use
set APP_HOME=C:\Temp\Hammer
rem Set APP_HOME to the place where you installed Hammer.
if "%~1"=="" (
echo You must provide a Python script to run.
pause
) else (
java.exe -cp "%APP_HOME%\lib\jython.jar;%APP_HOME%\lib\hammer.jar;%CLASSPATH%" bridenstine.hammer.main.Main %*
)
It is always better to enclose an argument string in double quotes if it contains environment variables like CLASSPATH which might have a string value containing 1 or more spaces.
%* is expanded by all arguments passed to the batch file as argument, i.e. %1 %2 ...
It is best to always specify an application like java with full path and file extension as otherwise Windows has to search for a file with a file extension as defined in environment variable PATHEXT in current working directory and all directories defined in environment variable PATH. At least the file extension should be in the batch file if the program files directory of the application varies.
I can only offer a suggestion; I've not tried this.
I would try escaping each ; with a caret ^ thus:
java -cp %APP_HOME%\lib\jython.jar^;%APP_HOME%\lib\hammer.jar^;%CLASSPATH% bridenstine.hammer.main.Main %1 %2
(But I'll admit it's clutching at straws...)
You are missing you she-bang at the top of the script
#!/bin/bash
On a cmd prompt or bat file, I issue the following:
start textpad myfile.txt and it works fine.
If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.
I desire to trap this error so that I could do something like
start textpad myfile.txt || start notepad myfile.txt
where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK.
My intent is to avoid the sound and the requirement of any user intervention.
I have also tried the following bat approach below, to no avail.
start textpad
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END
Any help would be great.
thanks
ted
You can use the following little snippet to find out whether the program you intend to launch exists:
for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.
But may I suggest that you simply use
start foo.txt
instead of forcing a specific editor onto the user? File type associations are there for a reason.
I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.
There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.
For these tricks, take a look at this page.
"/wait" parameter would do the trick for you..
START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.
START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
You probably already have an answer, but my over-sized ego has forced me to post my answer.
So, this should work.
start textpad 2> nul||start notepad
This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.