At Umbraco we are utilizing VisualStudio Team Services(VSTS) as our main build and release system. Over the last year or so we have been in the process of moving old TeamCity and AppVeyor builds into VSTS to use all of its awesomeness. Having used both AppVeyor and TeamCity intensively previously we did have something we were missing from those systems, that were not quite there in VSTS.
One of those things was a "BuildServer to Slack"-notifier, that was useful. In TeamCity there were a few different plugins that could be used for this, and they were very verbose. In AppVeyor the notification was simple and direct, and we liked it.
An example looks like this:
This would pop up in one of our Slack channels, whenever a build would fail - meaning either the code couldn't compile, the tests couldn't run or creating the final release wouldn't work(for Umbraco Cms that is a nuget and a zip file).
The message is simple - the build failed - this commit made by this person, with this text caused it - including a link, that would allow us to drill further down into the build log to find all the details associated.
After moving to VSTS, the only thing we were able to find, of default things is a Slack notifier, but is was much to general. Pr. default it would log the same thing as above like this:
The above wouldn't bee all bad, and it a good start, it does include a link to the build it self, so you'd always be able to see the details, but trouble is that to get something useful out of it, you would need to click that link, making you leave Slack. Furthermore it doesn't associate anything about what failed, and who to be accountable. We don't point fingers if someone breaks the build, but having the name of the one who did the commit right there in Slack ensures that if you have no interest in the failed build, you can ignore the message. In the default VSTS one, everyone would have to click the link, to check weather it was one of their commits that caused it. Worth mentioning is that the Umbraco build on VSTS takes 20-30 mins to finish.
I decided to take things into my own hands, partly because I wanted to see how integrating to VSTS's API was, and partly because we liked the Slack notifications, when they are proper done. So in short this became the result:
So, the new notification tries to mimic what the AppVayer one did, but also extending it a bit - So it now shows exactly what failed, by naming the build task, it also includes which commits caused the failure, as well as who did those commits. Finally, a lot of relevant links, that will allow the users to go directly to something that can help them figure out what and why it failed. This is v1 so there will come further changes to it, but for now its way better than what came by default.
So - How does it work?
VSTS has s feature called service hooks - these can be setup pr. project you have. A service hook can be setup to be just a Webhook, which is what we did here. A service hook simply just wants to know when it should invoke the Webhook. In our case we tell to invoke it on build failure. Then it's the Webhook that does the magic.
Basically the Webhook implemented does this:
- Get a payload from VSTS with basic information
- Analyze the payload
- Extend and enrich the payload with information form VSTS's own API and GitHub's API
- Format a response message
- Post it to Slack
Show me the code:
I decided to implement this in an Azure Function - but it would be easy to implement the exact same in a regular MVC site in a WebApi controller or the like. The code can be found on my GitHub here: https://github.com/mikkelhm/TeamServicesToSlack - It is simple, as I decided to go with a simple approach where fetching from the API's are just webrequests, that will be deserialized via json.net, in order to read some useable objects in the code.
Use if you you'd like to - ask questions - if further interested :-)