简体   繁体   English

Jenkins传递或失败构建外部可执行文件

[英]Jenkins pass or fail build on external executable

As part of my build process, I have an EXE that I wrote that pushes database changes to the staging DB. 作为构建过程的一部分,我编写了一个EXE,它将数据库更改推送到临时数据库。

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)? 如何将EXE添加为构建步骤以便在失败时(我可以将其作为例外捕获)我可以使构建失败并记录一些细节(类似于NUNit显示失败的方式)?

I would also like the exe to log some other details (like what's changed - whether the build passes or fails). 我还希望exe记录一些其他细节(比如改变了什么 - 构建是否通过)。 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). 我正在使用MSBuild,我完全控制了EXE(我写了它)。 I can code it to output what I want 我可以编码来输出我想要的东西

What are you using for your main build step? 您在主要构建步骤中使用了什么? Mavent? 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. 有很多方法可以做到这一点,它在很大程度上取决于你的.exe的设计方式,你没有在OP中解释。

Easiest would be to add another build step from the drop-down. 最简单的方法是从下拉列表中添加另一个构建步骤 Select Execute Windows batch command . 选择执行Windows批处理命令 In there, write batch commands to launch the .exe and capture the return code: 在那里,编写批处理命令来启动.exe并捕获返回代码:

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. 如果你的.exe像普通程序一样工作,它会在成功时返回退出代码0,否则它将返回一个非零退出代码。 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 . 上面的语句查找非零退出代码(表示某种类型的失败),如果检测到将退出批处理,错误代码1本身为EXIT /B 1 This in turn will indicate to Jenkins that the build step has failed, and will mark the job failed. 这反过来将向Jenkins表明构建步骤失败,并将标记作业失败。

Once again, this highly depends on your .exe being correct and returning non-zero exit code on failure. 再一次,这在很大程度上取决于你的.exe是否正确并在失败时返回非零退出代码。

As far as "I would also like the exe to log some other details", once again, depends entirely on your .exe 至于“我还希望exe记录其他一些细节”,再一次完全取决于你的.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? 它是一个在控制台中显示不同错误的命令行.exe吗? If that's so, then calling it from the batch will automatically display the errors back in Jenkins log. 如果是这样,那么从批处理中调用它将自动在Jenkins日志中显示错误。 You can also use this approach if your .exe does not return correct exit codes. 如果.exe没有返回正确的退出代码,也可以使用此方法。 You can capture the command line output and use findstr to search for specific output lines there to determine success or failure 您可以捕获命令行输出并使用findstr搜索那里的特定输出行以确定成功或失败

  • 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) 如果您的.exe是一个GUI应用程序,并且没有生成正确的退出代码,那么您无法确定它是否成功或失败(以及失败的内容)

I will update this answer if you identify which one it is. 如果你确定它是哪一个,我会更新这个答案。

This is a quick one I can think of: 这是我能想到的一个快速的:

  1. Break the build process into 2 jobs - A. Upto Exe. 将构建过程分解为2个作业 - A. Upto Exe。 B. Post Exe B. Post Exe
  2. In job A, Add post build step to run job B (whether or not A is successful) 在作业A中, Add post build step以运行作业B(无论A是否成功)
  3. In configuration of B, write a script to catch the output using Jenkins environment variables. 在B的配置中,编写脚本以使用Jenkins环境变量捕获输出。 Check out <yourjenkinsurl>/env-vars.html/ 查看<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 . 但它真的是一个Exe或其他可执行文件,如BAT,因为exe是一个二进制文件,你不能真正改变一个exe来实现这一点 - 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. 您必须更改可执行文件的源代码以从步骤3捕获变量并执行某些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM