Sending notifications to Microsoft Teams from your Cake build scripts Even though Teams at Microsoft would appreciate you bringing them that’s not what this post is about, but notifications to the Microsoft Teams collaboration / team chat product. And Cake? is an open source build system, which lets you in an unintrusive way with a C# DSL both cross platform and environment orchestrate everything around your build process — all using existing skills if you’re a .NET developer.( ) Cake read more at cakebuild.net So that settled, wouldn’t it be great if you could send notifications from your Cake scripts to a Microsoft Teams channel? Guess what — there is! The Addin Cake can be extended through addins, Cake addins can be any .NET assembly available as a NuGet package on any nuget feed, where is the most well known package source. However you can by using a couple of attributes ( ) become a more “native” Cake addin, in the sense that they lets you extend the DSL and import namespaces using an preprocessor directive. nuget.org i.e. CakeMethodAliasAttribute and CakeNamespaceImportAttribute #addin I’ve created such an addin which makes it easy for you to communicate with a Microsoft Teams channel The directive fetches the assembly from nuget, references it, finds any Cake extension methods and imports namespaces — making them conveniently globally available for you in your Cake scripts. #addin Setup / Teardown Reporting when script started and when it succeeded / failed can be achieved by registering actions on the and methods on the Cake script host, Setup is executed before Cake tasks are executed and Teardown is always executed post task execution. Setup Teardown As you can see the teardown has an property indicating build success/failure and property containing exception on failure. A successful build would look something like this: context Successful ThrownException And a failed build would contain the exception / stack trace like this: Task Setup / Teardown If you want to track the progress of individual tasks that’s possible using the and which lets you register actions executed before and after task is executed. TaskSetup TaskTeardown, Both and context provides a property which gives you meta data about the task, the context also provides and properties indicating if task was executed and how long it took to execute the task. setup teardown Task teardown Duration Skipped Advanced formatting So besides add in method that just takes a string as message, there’s also a overload that takes an , this gives you more control of the message layout. MicrosoftTeamsPostMessage MicrosoftTeamsPostMessage MicrosoftTeamsMessageCard Posting a message for each step of the build can become very chatty, tailoring the message to instead neatly summarize the build in one message like below is probably more sustainable in the long run. Which is very similar to what Cake outputs to the console On failure you’ll get the icon clearly indicating failure and steps executed just as on success. But you also get the full stacktrace from any error when expanding the message So what could the code for this look like? A complete example you can test below The above code basically hooks up actions to , and . Only collecting data before is finally called. It’s just C# so you can go as crazy as you want, but above is a good starting point. Setup Teardown TaskTeardown Teardown Conclusion Hopefully this post has shown you how to send notifications from your cake scripts and what the extension points Cake provides for this. This could obviously be reused for other messaging platforms and if you check the section on Cake’s website you’ll find addins for Slack, HipChat, Gitter, Twitter, etc. addins
Share Your Thoughts