Skip to content

Instantly share code, notes, and snippets.

@dannyrb
Created December 13, 2016 13:37
Show Gist options
  • Save dannyrb/0949b031d164cf44058fa39a6aa239e3 to your computer and use it in GitHub Desktop.
Save dannyrb/0949b031d164cf44058fa39a6aa239e3 to your computer and use it in GitHub Desktop.
Hook Octopus Deployment into Slack Notifications. Example with resources to assist modifying.
## Resources
## Original: https://library.octopusdeploy.com/step-templates/21a2ae12-e721-42c1-901d-d6ed08007ca7/actiontemplate-slack-notify-deployment
## Slack API JSON
## https://api.slack.com/docs/messages/builder
## Octopus Variables
## http://docs.octopusdeploy.com/display/OD/System+variables
## DEBUG -- Shows in output log
##write-host $OctopusParameters['Octopus.Release.Number'];
##write-host $OctopusParameters['Octopus.Release.Notes'];
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"];
});
};
);
}
Invoke-RestMethod -Method POST -Body ($payload | ConvertTo-Json -Depth 4) -Uri $OctopusParameters['HookUrl'] -ContentType 'application/json'
}
$IncludeMachineName = [boolean]::Parse($OctopusParameters['IncludeMachineName']);
if ($IncludeMachineName) {
$MachineName = $OctopusParameters['Octopus.Machine.Name'];
$FormattedMachineName = "($MachineName)";
}
$OctopusReleaseNotes = $OctopusParameters['Octopus.Release.Notes'];
$OctopusReleaseNumber = $OctopusParameters['Octopus.Release.Number'];
$OctopusEnvironmentName = $OctopusParameters['Octopus.Environment.Name'];
if ($OctopusParameters['Octopus.Deployment.Error'] -eq $null){
Slack-Rich-Notification @{
title = "Deployed release $OctopusReleaseNumber to $OctopusEnvironmentName";
value = "Release Notes: $OctopusReleaseNotes";
fallback = "Deployed $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName successfully";
color = "good";
};
} else {
Slack-Rich-Notification @{
title = "Failed deploy of release $OctopusReleaseNumber to $OctopusEnvironmentName";
value = "Deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName $FormattedMachineName";
fallback = "Failed to deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName";
color = "danger";
};
}
@jiimaho
Copy link

jiimaho commented Feb 2, 2018

With inspiration from this I also created a script that includes the url back to octopus. Handy when you want to see what went wrong with a failed deploy: https://gist.github.com/jiimaho/106efaa6193e7aeac3100d5427a591ce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment