20 October 2012

Handling PowerShell Scripts Failure Conditions in TeamCity

PowerShell scripts in TeamCity build steps can be a pain to handle when they fail and you want them to brake the build process.

Part of our OneClick-deployment-for-Sitecore is a build step for production where we run a backup script for the production Sql servers prior to releasing the Sitecore Items with TDS. Some time ago we were hit with the following issue that showed up in the buildlog but did not have any effect on the build process.

This meant that we did not have the backup in place prior to deploying the Sitecore Items!

[10:43:37]Step 3/7: Backup DB (Powershell) (32s)
[10:43:37][Step 3/7] Starting: C:\Windows\system32\cmd.exe /c C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -File D:\TeamCity\buildAgent\work\54acd6c7ad57d4b9\BackupScript.ps1 xx prod xx prod 0.0.148.1937 && exit /b %ERRORLEVEL%
[10:43:37][Step 3/7] in directory: D:\TeamCity\buildAgent\work\54acd6c7ad57d4b9
[10:44:09][Step 3/7] Exception calling "SqlBackup" with "1" argument(s): "Backup failed for Server '
[10:44:09][Step 3/7] xxxx'. "
[10:44:09][Step 3/7] At D:\TeamCity\buildAgent\work\54acd6c7ad57d4b9\BackupScript.ps1:35 char:22
[10:44:09][Step 3/7] +     $smoBackup.SqlBackup <<<< ($server)
[10:44:09][Step 3/7]     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
[10:44:09][Step 3/7]     + FullyQualifiedErrorId : DotNetMethodException
[10:44:09][Step 3/7]  
[10:44:09][Step 3/7] Exception calling "SqlBackup" with "1" argument(s): "Backup failed for Server '
[10:44:09][Step 3/7] xxx'. "
[10:44:09][Step 3/7] At D:\TeamCity\buildAgent\work\54acd6c7ad57d4b9\BackupScript.ps1:35 char:22
[10:44:09][Step 3/7] +     $smoBackup.SqlBackup <<<< ($server)
[10:44:09][Step 3/7]     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
[10:44:09][Step 3/7]     + FullyQualifiedErrorId : DotNetMethodException
[10:44:09][Step 3/7]  
[10:44:10][Step 3/7] Process exited with code 0

The problem was caused by missing disk space on the Sql server (due to many releases and backups) so we were lucky to have a disk space monitor alert from the hosting provider.

Updating the build for handling these PowerShell Exceptions can be done by either updating the return codes for the shell script but can also be achieved with TeamCity's "Build Failure Conditions" with a text validation on the build log.

No comments: