PowerShell and TeamCity build status output info.
After doing quite a bit of research on the subject, I found that there was not a lot of content in regards to how to handle a PowerShell build running in TeamCity and having it handle exits and errors correctly. Due to this lack of ability to find detailed information, I figured why not add a little bit here.
Example of an Azure ARM Deployment with error handling in TeamCity:
Yes, there are probably much better ways to handle this, but this is just a quick example of how we could write this. And no, it won't catch all issues with the deployment, but this is a work in progress.
The breakdown:
We start off creating a splat to handle the parameters of the commands we are going to run. This is to save typing and readability later on. Next up we start our first try/catch/finally block. We test the ARM template to make sure it is valid and if it is, it falls into the next try/catch block. You're not here for that. What you're here for is the output. Notice we have 2 things going on here. We are writing to the host a build status. The format of this variable is important. It lets TeamCity know the status of the build. it must be formatted this way or TeamCity doesn't pick it up correctly. Next up we are setting an exit code. This lets TeamCity know the process has finished and if it was successful or not. It's really not complicated, however, I wasn't able to find much information on the topic when looking.
Thank you for reading.
Example of an Azure ARM Deployment with error handling in TeamCity:
Yes, there are probably much better ways to handle this, but this is just a quick example of how we could write this. And no, it won't catch all issues with the deployment, but this is a work in progress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | $DeploymentSplat = @{ "Name" = "TestDBDeploy" "ResourceGroupName" = "my-awesome-resourcegroup" "Mode" = "Incremental" "TemplateParameterFile" = "C:\Path\To\Arm\TemplateParameters.json" "TemplateFile" = "C:\Path\To\Arm\Template.json" "ErrorAction" = "Stop" } try { Test-AzureRmResourceGroupDeployment @DeploymentSplat $BuildStatus = "##teamcity[buildStatus status='SUCCESS']" try { New-AzureRMResourceGroupDeployment @DeploymentSplat $BuildStatus = "##teamcity[buildStatus status='SUCCESS']" $ExitCode = 0 } catch { Write-Error $_ $BuildStatus = "##teamcity[buildStatus status='FAILURE' text='$_']" $ExitCode = 1 } } catch { Write-Error $_ $BuildStatus = "##teamcity[buildStatus status='FAILURE' text='$_']" $ExitCode = 1 } finally { Write-host $BuildStatus exit($ExitCode); } |
The breakdown:
We start off creating a splat to handle the parameters of the commands we are going to run. This is to save typing and readability later on. Next up we start our first try/catch/finally block. We test the ARM template to make sure it is valid and if it is, it falls into the next try/catch block. You're not here for that. What you're here for is the output. Notice we have 2 things going on here. We are writing to the host a build status. The format of this variable is important. It lets TeamCity know the status of the build. it must be formatted this way or TeamCity doesn't pick it up correctly. Next up we are setting an exit code. This lets TeamCity know the process has finished and if it was successful or not. It's really not complicated, however, I wasn't able to find much information on the topic when looking.
Thank you for reading.