Friday, Friday. Gotta get down on Friday

A few of you might have spotted this via the Octopus Deploy June newsletter.

Friday

You might be wondering how it’s done, as at least one person has requested via Twitter. Well wait no longer. Here’s how it’s done.

What you’re looking at is a Slack notification from Octopus Deploy, featuring a little image that’s been doing the rounds on Twitter. It exists already as a Capistrano template, and I figured it wouldn’t be too hard to do in Octopus.

I was going to go with the ASCII art  version, but after some struggles with Unicode transmission to Slack via webhooks, I decided to use the image directly. Life is too short to mess around with encoding all day.

I cracked open our existing Slack Deployment Notification step template (available from the Octopus Library), and added a snippet to detect if it’s Friday, and if we’re going to production

if((Get-Date).DayOfWeek -eq "Friday" -and $OctopusEnvironmentName -eq "Production")
{
    #image here
}

Because I wasn’t being super-fastidious about my code, I simply created an entirely new $payload object, with an image_url property under attachments, pointing to the PNG image hosted over at tech.domain.com.au.

function Slack-Rich-Notification ($notification)
{
    $payload = @{
        channel = $OctopusParameters['Channel'];
        username = $OctopusParameters['Username'];
        icon_url = $OctopusParameters['IconUrl'];
        attachments = @(
            @{
            fallback = $notification["fallback"];
            color = $notification["color"];
            fields = @(
                @{
                title = $notification["title"];
                value = $notification["value"];
                });
            };
        );
    }
    
    if((Get-Date).DayOfWeek -eq "Friday" -and $OctopusEnvironmentName -eq "Production")
    {
        $payload = @{
            channel = $OctopusParameters['Channel'];
            username = $OctopusParameters['Username'];
            icon_url = $OctopusParameters['IconUrl'];
            attachments = @(
            @{
                fallback = $notification["fallback"];
                color = $notification["color"];
                image_url = "http://tech.domain.com.au/wp-content/uploads/2015/06/friday_deploy.png";
                fields = @(
                    @{
                    title = $notification["title"];
                    value = $notification["value"];
                    });
                };
            );
        }
    }
    Invoke-Restmethod -Method POST -Body ($payload | ConvertTo-Json -Depth 4) -Uri $OctopusParameters['HookUrl'] -Headers @{ "Content-Type" = "application/json; charset=utf-8"; }
}

If I was being a little more anal-retentive about my code, I’d probably have appended it to the existing $payload object using Add-Member, or perhaps added a different image for a not-Friday deploy. But this isn’t exactly what you’d call a critical function. Maybe later I’ll clean it up.

And as Octopus Deploy’s library step template is open-source, I present here the adapted version in full, for your delectation. You’ll also note, if you’re observant, that our version is modified to show which user did a deployment, not just that it happens. We run a no-blame DevOps ship, but it does help to know who to talk to if there’s a problem.

function Slack-Rich-Notification ($notification)
{
    $payload = @{
        channel = $OctopusParameters['Channel'];
        username = $OctopusParameters['Username'];
        icon_url = $OctopusParameters['IconUrl'];
        attachments = @(
            @{
            fallback = $notification["fallback"];
            color = $notification["color"];
            fields = @(
                @{
                title = $notification["title"];
                value = $notification["value"];
                });
            };
        );
    }
    
    if((Get-Date).DayOfWeek -eq "Friday" -and $OctopusEnvironmentName -eq "Production")
    {
        $payload = @{
            channel = $OctopusParameters['Channel'];
            username = $OctopusParameters['Username'];
            icon_url = $OctopusParameters['IconUrl'];
            attachments = @(
            @{
                fallback = $notification["fallback"];
                color = $notification["color"];
                image_url = "http://tech.domain.com.au/wp-content/uploads/2015/06/friday_deploy.png";
                fields = @(
                    @{
                    title = $notification["title"];
                    value = $notification["value"];
                    });
                };
            );
        }
    }
    Invoke-Restmethod -Method POST -Body ($payload | ConvertTo-Json -Depth 4) -Uri $OctopusParameters['HookUrl'] -Headers @{ "Content-Type" = "application/json; charset=utf-8"; }
}

$UserName = $OctopusParameters['Octopus.Deployment.CreatedBy.Username']

if ($OctopusParameters['DeploySuccessful'] -eq "true"){
    Slack-Rich-Notification @{
        title = "Success";
        value = "$OctopusProjectName release $OctopusReleaseNumber was deployed to $OctopusEnvironmentName by $UserName";
        fallback = "$OctopusProjectName release $OctopusReleaseNumber was deployed to $OctopusEnvironmentName by $UserName";
        color = "good";
    };
} else {
    Slack-Rich-Notification @{
        title = "Failed";
        value = "Deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName by $UserName";
        fallback = "$OctopusProjectName release $OctopusReleaseNumber failed deployment to $OctopusEnvironmentName - deploy triggered by @UserName";
        color = "danger";
    };
}

Happy deployments, and may all your Friday deploys be clean, successful and error-free.

Leave a Reply

Your email address will not be published. Required fields are marked *