32

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

Kelsius
  • 433
  • 2
  • 5
  • 19
HSNN
  • 531
  • 1
  • 6
  • 14
  • 1
    I can't answer your entire question, but for your last query, you have access to the `$WORKSPACE` environment variable, which helps for knowing where Jenkins is building from. I use it in my batch script. – Bartek Dec 20 '12 at 12:54
  • Thanks. %WORKSPACE% helps. %BUILD_ID% helps too, guess I´ll just point the batch file to build on %WORKSPACE%\builds\%BUILD_ID% – HSNN Dec 20 '12 at 13:13

6 Answers6

41

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

Community
  • 1
  • 1
Gonen
  • 4,005
  • 1
  • 31
  • 39
  • 2
    Nice compilation but, like I said, %SVN_REVISION% is empty. %SVN_REVISION_1% works though. – HSNN Jan 07 '13 at 12:28
  • 1
    Well, either you have more than one module, or it's a bug. – Gonen Jan 07 '13 at 13:24
  • > "If your last command is ECHO FAILURE!!!, Jenkins thinks everything is OK" - I tried, but Jenkins returns failure because previous comand failed – sergtk Sep 18 '13 at 14:44
  • Replaced 'echo' example with 'copy', to be more accurate. – Gonen Apr 18 '14 at 12:08
8

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.

Stéphane Piette
  • 5,341
  • 6
  • 34
  • 50
  • Thanks, the bat already treated compilation errors but used a plain "exit". Exit 1 solved my problem. – HSNN Jan 07 '13 at 12:26
  • That sounds right. Also, you could write a result to a text file just before the 'exit 1' (on a error) and then another process could read that file for a result. – djangofan Feb 22 '13 at 16:15
6

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%
J. Beattie
  • 183
  • 1
  • 7
4

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

sdmythos_gr
  • 5,444
  • 3
  • 25
  • 25
0

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.

Ravi Amlani
  • 92
  • 1
  • 9
0

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.

koga73
  • 964
  • 9
  • 16