MSBuild Task To Mark Azure Git Deployment in Rollbar

I prefer using git to deploy to Azure: it’s great that a production deployment is only a git push azure master away. Git deployment comes with a number of benefits like making it easier to rollback, forcing you to always commit, and making it hard to forget to deploy an asset.

As I’ve written before, I like using Rollbar for collecting errors so they can be notified, triaged, and addressed. Rollbar has the ability to mark a deployment so that you know which version of the software is causing problems. All you need to do is POST to their API passing along the environment and your access key.

Rollbar provides a link to a gist from Matt Hensley showing how to read the values from your web.config in order to make the API call.

However, if you like to keep your secrets out of git, you won’t want to commit the access key to your repo, especially if its open source.

Luckily, in Azure, any application settings you configure through the portal are available as environment variables. If you create a setting called RollbarAccessToken, it will be available to all processes as APPSETTING_RollbarAccessToken.

I forked Matt’s gist and simplified it to use the environment variables rather than reading from XML.

I provide it below for safe keeping as well:

Rollbar.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="NotifyRollbarOfDeploy" AfterTargets="MSDeployPublish;CopyAllFilesToSingleFolderForPackage">
<Exec Command="git log -1 --format=%%H" ConsoleToMSBuild="true" EchoOff="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitSHA" />
</Exec>
<Exec Command="git config user.email" ConsoleToMSBuild="true" EchoOff="true" WorkingDirectory="$(ProjectDir)\..">
<Output TaskParameter="ConsoleOutput" PropertyName="GitEmail" />
</Exec>
<Message Text="Rollbar.AccessToken: $(APPSETTING_RollbarAccessToken)" Importance="Normal" />
<Message Text="Rollbar.Environment: $(APPSETTING_RollbarEnvironment)" Importance="Normal" />
<Message Text="Git SHA: $(GitSHA)" Importance="Normal" />
<Message Text="Rollbar: $(GitEmail) deployed @(RollbarEnvironment) revision $(GitSHA)" Importance="High" />
<Exec Command="@powershell -NoProfile -ExecutionPolicy unrestricted -Command &quot;(new-object net.webclient).UploadString('https://api.rollbar.com/api/1/deploy/', 'access_token=$(APPSETTING_RollbarAccessToken)&amp;environment=$(APPSETTING_RollbarEnvironment)&amp;revision=$(GitSHA)&amp;local_username=$(GitEmail)')&quot;" EchoOff="true" />
</Target>
</Project>

You can include it in your web project’s .csproj file by importing it:

YourProject.Web.csproj
1
2
3
4
5
6
7
8
9
10
11
12
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\UmbracoCms.7.2.4\build\UmbracoCms.props" Condition="Exists('..\packages\UmbracoCms.7.2.4\build\UmbracoCms.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<!-- Include this line here -->
<Import Project="..\Rollbar.targets" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
...

Now when you push to Azure, the build will trigger a notification in Rollbar.

Good luck!