Return 如果 nmake 在批处理过程中失败,则会出现错误和电子邮件
Return Error and email if nmake fails during batch
我正在尝试创建批次以在 Windows 下实现夜间构建。我设法在发布中编译并通过脚本自动调试生成。
但现在我希望在编译过程中出现错误时得到通知,如果失败则需要 return 一封电子邮件。这就提出了以下问题:
我会return 报错吗?或者更确切地说,如果失败是批处理本身?
如果是这样,我如何才能 return 在文本文件或电子邮件中出现此错误?
我 运行 在 à Windows Server 2012 下,使用任务计划程序进行自动化。这是我的批次 (EDITED) :
@echo on
echo[
set rel=release
@echo ####### COMPILATION CORE (DEBUG / x86) #######
echo[
cd C:\Users\mea\Documents\Repos\corealpi\cmake\nmake
@echo ####### delete cache #######
rm -R *
@echo ####### Generation of Makefile #######
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
cmake -DCMAKE_BUILD_TYPE=Debug -G"NMake Makefiles" ../
if %errorlevel% neq 0 goto mailcmaked
@echo ####### compilation of core #######
echo[
nmake
if %errorlevel% neq 0 goto mailnmaked
pause
@echo ####### COMPILATION CORE(RELEASE / x86) #######
echo[
cd C:\Users\mea\Documents\Repos\corealpi\cmake\nmake
@echo ####### Suppression du CACHE #######
rm -R *
@echo ####### Generation of Makefile #######
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
cmake -DCMAKE_BUILD_TYPE=Release -G"NMake Makefiles" ../
if %ERRORLEVEL% neq 0 goto mailcmaker
@echo ####### Compilation of CORE #######
echo[
>"..\log\buildRel.log" nmake
if %errorlevel% neq 0 goto mailnmaker
pause
:mailcmaked
echo Error!
sendEmail -f from@mail.com -t someone@mail.com -m "cmake debug failed" -s 192.168.x.x
exit 1
:mailnmaked
echo Error!
sendEmail -f from@mail.com -t someone@mail.com -m "nmake debug failed" -s 192.168.x.x
exit 1
:mailcmaker
sendEmail -f from@mail.com -t someone@mail.com -m "cmake release failed" -s 192.168.x.x
exit 1
:mailnmaker
grep -Eo '.{0,20}error.{0,100}' ..\log\buildRel.log > ..\log\mailBuild.txt
sendEmail -f from@mail.com -u "build failed" -t someone@mail.com -m "nmake release failed" -a ..\log\mailBuild.txt -s 192.168.x.x
exit 1
编辑
我从您的示例中汲取灵感编辑了我的脚本,现在效果很好(目前仅针对 :mailnmaker
部分)。我用 grep 命令组织我的输出。
对于邮件问题,我刚刚安装了 sendEmail 并将其放入我的 Windows PATH 中,并且可以正常工作。
感谢您的帮助。
首先,没有可让您发送电子邮件的本机命令。您可以使用 Windows 脚本(VBScript 或 JScript)使用 CDO.Message
though. The least cumbersome way of incorporating the Windows Script Host into a batch script is to use a hybrid script.
编写脚本
如果 cmake
或 nmake
以非零状态退出,您可以使用 conditional execution 触发发送电子邮件功能。
这是一个帮助您入门的示例框架。 请注意,这是未经测试的,可能会被破坏。但无论如何,它至少应该给你一个开始的地方。
(另请注意,我将 rm -R *
替换为本机 Windows 命令,并且 cmake
和 nmake
的输出被重定向到 %temp%\build.log
以包含如果需要,将其发送到电子邮件中。如果您还希望输出回显到控制台,如果您安装了 cygwin 或 gnuwin32 或类似软件,则可能必须使用 tee -a
;或者可能合并 this scripted imitation。)
@if (@CodeSection == @Batch) @then
@echo on
echo[
@echo ####### COMPILATION CORE (DEBUG / x86) #######
call :make Debug || exit /b %ERRORLEVEL%
@echo ####### COMPILATION CORE(RELEASE / x86) #######
call :make Release || exit /b %ERRORLEVEL%
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:make <Release|Debug>
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo[
cd /d C:\Users\mea\Documents\Repos\corealpi\cmake\nmake
@echo ####### delete cache #######
for /d %%I in (*) do rmdir /q /s "%%~I"
del /q /y *
@echo ####### Makefile #######
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
:: use conditional execution to send an email if cmake exits non-zero
>"%temp%\build.log" cmake -DCMAKE_BUILD_TYPE=%1 -G"NMake Makefiles" ../ || (
call :sendmail cmake %1 %ERRORLEVEL%
exit /b %ERRORLEVEL%
)
@echo ####### compilation of core (debug/x86) #######
echo[
:: use conditional execution to send an email if nmake exits non-zero
>"%temp%\build.log" nmake || (
call :sendmail nmake %1 %ERRORLEVEL%
exit /b %ERRORLEVEL%
)
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:sendmail <name> <buildtype> <errorlevel>
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
cscript /nologo /e:JScript "%~f0" "%~1" "%~2" "%~3" "%temp%\build.log"
exit /b %~3
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: end batch / begin JScript
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@end
var buildstep = WSH.Arguments(0),
buildtype = WSH.Arguments(1),
errorlevel = WSH.Arguments(2),
buildlog = WSH.Arguments(3),
oSH = WSH.CreateObject('WScript.Shell'),
fso = WSH.CreateObject('Scripting.FileSystemObject');
function SendEMail(config) {
try {
var oMsg = WSH.CreateObject("CDO.Message");
for (var i in config) oMsg[i] = config[i];
oMsg.Send();
}
catch(e) {
var profile = oSH.Environment('Process')('USERPROFILE'),
alert = fso.CreateTextFile(profile + '\Desktop\builderr.log', 1);
alert.WriteLine(buildstep + ' build type ' + buildtype
+ ' failed. Email failed. Everything is broken. I give up.');
alert.WriteLine(getLog(20));
alert.Close();
}
}
function getLog(lines) {
log = fso.OpenTextFile(buildlog, 1),
contents = [];
while (!log.AtEndOfStream) {
contents.push(log.ReadLine());
if (contents.length > lines) contents.shift();
}
log.Close();
return contents.join('\n');
}
SendEMail({
From: "from@example.com",
To: "to@example.com",
Subject: "Build failed",
TextBody: buildstep + " build type " + buildtype
+ " failed with error code " + errorlevel
+ '\n\nLast 20 lines of messages from ' + buildlog + ':\n\n' + getLog(20)
});
我正在尝试创建批次以在 Windows 下实现夜间构建。我设法在发布中编译并通过脚本自动调试生成。
但现在我希望在编译过程中出现错误时得到通知,如果失败则需要 return 一封电子邮件。这就提出了以下问题:
我会return 报错吗?或者更确切地说,如果失败是批处理本身?
如果是这样,我如何才能 return 在文本文件或电子邮件中出现此错误?
我 运行 在 à Windows Server 2012 下,使用任务计划程序进行自动化。这是我的批次 (EDITED) :
@echo on
echo[
set rel=release
@echo ####### COMPILATION CORE (DEBUG / x86) #######
echo[
cd C:\Users\mea\Documents\Repos\corealpi\cmake\nmake
@echo ####### delete cache #######
rm -R *
@echo ####### Generation of Makefile #######
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
cmake -DCMAKE_BUILD_TYPE=Debug -G"NMake Makefiles" ../
if %errorlevel% neq 0 goto mailcmaked
@echo ####### compilation of core #######
echo[
nmake
if %errorlevel% neq 0 goto mailnmaked
pause
@echo ####### COMPILATION CORE(RELEASE / x86) #######
echo[
cd C:\Users\mea\Documents\Repos\corealpi\cmake\nmake
@echo ####### Suppression du CACHE #######
rm -R *
@echo ####### Generation of Makefile #######
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
cmake -DCMAKE_BUILD_TYPE=Release -G"NMake Makefiles" ../
if %ERRORLEVEL% neq 0 goto mailcmaker
@echo ####### Compilation of CORE #######
echo[
>"..\log\buildRel.log" nmake
if %errorlevel% neq 0 goto mailnmaker
pause
:mailcmaked
echo Error!
sendEmail -f from@mail.com -t someone@mail.com -m "cmake debug failed" -s 192.168.x.x
exit 1
:mailnmaked
echo Error!
sendEmail -f from@mail.com -t someone@mail.com -m "nmake debug failed" -s 192.168.x.x
exit 1
:mailcmaker
sendEmail -f from@mail.com -t someone@mail.com -m "cmake release failed" -s 192.168.x.x
exit 1
:mailnmaker
grep -Eo '.{0,20}error.{0,100}' ..\log\buildRel.log > ..\log\mailBuild.txt
sendEmail -f from@mail.com -u "build failed" -t someone@mail.com -m "nmake release failed" -a ..\log\mailBuild.txt -s 192.168.x.x
exit 1
编辑
我从您的示例中汲取灵感编辑了我的脚本,现在效果很好(目前仅针对 :mailnmaker
部分)。我用 grep 命令组织我的输出。
对于邮件问题,我刚刚安装了 sendEmail 并将其放入我的 Windows PATH 中,并且可以正常工作。
感谢您的帮助。
首先,没有可让您发送电子邮件的本机命令。您可以使用 Windows 脚本(VBScript 或 JScript)使用 CDO.Message
though. The least cumbersome way of incorporating the Windows Script Host into a batch script is to use a hybrid script.
如果 cmake
或 nmake
以非零状态退出,您可以使用 conditional execution 触发发送电子邮件功能。
这是一个帮助您入门的示例框架。 请注意,这是未经测试的,可能会被破坏。但无论如何,它至少应该给你一个开始的地方。
(另请注意,我将 rm -R *
替换为本机 Windows 命令,并且 cmake
和 nmake
的输出被重定向到 %temp%\build.log
以包含如果需要,将其发送到电子邮件中。如果您还希望输出回显到控制台,如果您安装了 cygwin 或 gnuwin32 或类似软件,则可能必须使用 tee -a
;或者可能合并 this scripted imitation。)
@if (@CodeSection == @Batch) @then
@echo on
echo[
@echo ####### COMPILATION CORE (DEBUG / x86) #######
call :make Debug || exit /b %ERRORLEVEL%
@echo ####### COMPILATION CORE(RELEASE / x86) #######
call :make Release || exit /b %ERRORLEVEL%
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:make <Release|Debug>
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo[
cd /d C:\Users\mea\Documents\Repos\corealpi\cmake\nmake
@echo ####### delete cache #######
for /d %%I in (*) do rmdir /q /s "%%~I"
del /q /y *
@echo ####### Makefile #######
call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat"
:: use conditional execution to send an email if cmake exits non-zero
>"%temp%\build.log" cmake -DCMAKE_BUILD_TYPE=%1 -G"NMake Makefiles" ../ || (
call :sendmail cmake %1 %ERRORLEVEL%
exit /b %ERRORLEVEL%
)
@echo ####### compilation of core (debug/x86) #######
echo[
:: use conditional execution to send an email if nmake exits non-zero
>"%temp%\build.log" nmake || (
call :sendmail nmake %1 %ERRORLEVEL%
exit /b %ERRORLEVEL%
)
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:sendmail <name> <buildtype> <errorlevel>
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
cscript /nologo /e:JScript "%~f0" "%~1" "%~2" "%~3" "%temp%\build.log"
exit /b %~3
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: end batch / begin JScript
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@end
var buildstep = WSH.Arguments(0),
buildtype = WSH.Arguments(1),
errorlevel = WSH.Arguments(2),
buildlog = WSH.Arguments(3),
oSH = WSH.CreateObject('WScript.Shell'),
fso = WSH.CreateObject('Scripting.FileSystemObject');
function SendEMail(config) {
try {
var oMsg = WSH.CreateObject("CDO.Message");
for (var i in config) oMsg[i] = config[i];
oMsg.Send();
}
catch(e) {
var profile = oSH.Environment('Process')('USERPROFILE'),
alert = fso.CreateTextFile(profile + '\Desktop\builderr.log', 1);
alert.WriteLine(buildstep + ' build type ' + buildtype
+ ' failed. Email failed. Everything is broken. I give up.');
alert.WriteLine(getLog(20));
alert.Close();
}
}
function getLog(lines) {
log = fso.OpenTextFile(buildlog, 1),
contents = [];
while (!log.AtEndOfStream) {
contents.push(log.ReadLine());
if (contents.length > lines) contents.shift();
}
log.Close();
return contents.join('\n');
}
SendEMail({
From: "from@example.com",
To: "to@example.com",
Subject: "Build failed",
TextBody: buildstep + " build type " + buildtype
+ " failed with error code " + errorlevel
+ '\n\nLast 20 lines of messages from ' + buildlog + ':\n\n' + getLog(20)
});