Jenkins pass or fail build on external executable

2019-08-09 10:07发布

问题:

As part of my build process, I have an EXE that I wrote that pushes database changes to the staging DB.

This process encounters errors sometimes, and if so, I would like the build to fail. How can I add the EXE as a build step so that on failure (which I could catch as an exception) I can fail the build and log some details (similar to the way NUNit shows failures)?

I would also like the exe to log some other details (like what's changed - whether the build passes or fails). Is there any documentation to how I can do this?

I'm using MSBuild, and I have full control over the EXE (I wrote it). I can code it to output what I want

回答1:

What are you using for your main build step? Mavent? Ant? Custom script? There are many ways to do it, and it highly depends on the way your .exe is designed, which you didn't explain in the OP.

Easiest would be to add another build step from the drop-down. Select Execute Windows batch command. In there, write batch commands to launch the .exe and capture the return code:

C:\Location_of_exe\your.exe
IF NOT "%ERRORLEVEL%" == "0" (
    ECHO "Your exe failed"
    EXIT /B 1
) ELSE (
    ECHO "Your exe passed"
)

If your .exe works like normal programs, it will return exit code 0 when successful, else it will return a non-zero exit code. The above statement looks for non-zero exit code (indicating failure of some sort), and if detected will exit the batch with error code 1 itself EXIT /B 1. This in turn will indicate to Jenkins that the build step has failed, and will mark the job failed.

Once again, this highly depends on your .exe being correct and returning non-zero exit code on failure.

As far as "I would also like the exe to log some other details", once again, depends entirely on your .exe

  • Does it return different exit codes for different failures? - If so, the code above can be easily modified to capture all possibilities and display error in log accordingly

  • Is it a command line .exe that display different errors in console? If that's so, then calling it from the batch will automatically display the errors back in Jenkins log. You can also use this approach if your .exe does not return correct exit codes. You can capture the command line output and use findstr to search for specific output lines there to determine success or failure

  • If your .exe is a GUI application, and does not generate correct exit codes, then you got no way of telling if it was successful or it failed (and what failed)

I will update this answer if you identify which one it is.



回答2:

This is a quick one I can think of:

  1. Break the build process into 2 jobs - A. Upto Exe. B. Post Exe
  2. In job A, Add post build step to run job B (whether or not A is successful)
  3. In configuration of B, write a script to catch the output using Jenkins environment variables. Check out <yourjenkinsurl>/env-vars.html/

But is it really an Exe or some other executable like BAT, as an exe is a binary and you can not really change an exe to achieve this - I would also like the exe to log some other details. You'll have to change the source code of the executable to capture variables from step 3 and do something.