Jon Simpson

Octopus Deploy Integration with Slack

13 May 2014 — octopus deploy, slack, powershell

We’re currently experimenting with Slack at FundApps and while it has a great set of integrations by default there’s no built-in integration for deployment notifications from Octopus Deploy. Support for Powershell script steps in Octopus provides all the tools needed to roll our own integration in the meantime. Slack supports both plain text and rich messages from webhooks, the rich messages provide a coloured bar to the left of the message with consistent good/green, warning/amber and danger/red shades. The plain text messages are straightforward to generate, the rich text requires a little more work, as the data structures nest quite deeply. Below is the desired and end result in Slack.

The desired output from Octopus Deploy in Slack

To get started, go to the project you want to add notifications for in Octopus and add a Powershell script step.

Configuring the Notify Slack step in Octopus Deploy

Customise the common part of the script below and insert it into the step body; you’ll need to swap the placeholders of YOURCOMPANY, YOURTOKEN and #CHANNEL with your actual values. You can alter the messages (title, value and fallback) – the fallback value is used on clients in places where the rich formatting isn’t supported so it’ll need to include all of the context from both title and value fields 1.

$hook = "https://YOURCOMPANY.slack.com/services/hooks/incoming-webhook?token=YOURTOKEN"

$hook_config = @{
    channel = "#yourchannel";
    username = "OctopusDeploy";
    icon_url = "http://octopusdeploy.com/content/resources/favicon.png";
};

function Slack-Rich-Notification ($hook_config, $notification)
{
    $payload = @{
        channel = $hook_config["channel"];
        username = $hook_config["username"];
        icon_url = $hook_config["icon_url"];
        attachments = @(
            @{
            fallback = $notification["fallback"];
            color = $notification["color"];
            fields = @(
                @{
                title = $notification["title"];
                value = $notification["value"];
                });
            };
        );
    }

    Invoke-Restmethod -Method POST -Body ($payload | ConvertTo-Json -Depth 4) -Uri $hook
}

For the first step you add, leave the run condition set to ‘Success’ and add the following lines to the bottom of the script.

$success_notification = @{
    title = "Success";
    value = "Deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName";
    fallback = "Deployed $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName successfully";
    color = "good";
};
Slack-Rich-Notification $hook_config $success_notification

Once that step is added, add a second with the same common piece of script from above, the run condition set to ‘Failure’ and the following final lines.

$failed_notification = @{
    title = "Failed";
    value = "Deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName";
    fallback = "Failed to deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName";
    color = "danger";
};
Slack-Rich-Notification $hook_config $failed_notification

This setup was a little tricky to get going as the syntax of Powershell dictionary and list manipulations is awkward and the ConvertTo-Json Powershell function has a relatively low default depth. To debug you can output the value of $payload | ConvertTo-Json -Depth 4 instead of the call to Invoke-Restmethod and compare it against the sample output below.

{
    "attachments": [
        {
            "color": "danger",
            "fallback": "Failed to deploy Rapptr release 1.0.0.9999-master to Staging - Foo",
            "fields": [
                {
                    "title": "Failed",
                    "value": "Deploy Rapptr release 1.0.0.9999-master to Staging - Foo"
                }
            ]
        }
    ],
    "channel": "#yourchannel",
    "icon_url": "http://octopusdeploy.com/content/resources/favicon.png",
    "username": "OctopusDeploy"
}
  1. Such as Notification Center alerts on the Mac.


 Home