Last active
May 20, 2016 15:19
-
-
Save bronumski/eec776368fc64f47762a15e9db01f328 to your computer and use it in GitHub Desktop.
Playing with slack integration from Octopus Deploy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add-type @" | |
| using System.Net; | |
| using System.Security.Cryptography.X509Certificates; | |
| public class TrustAllCertsPolicy : ICertificatePolicy { | |
| public bool CheckValidationResult( | |
| ServicePoint srvPoint, X509Certificate certificate, | |
| WebRequest request, int certificateProblem) { | |
| return true; | |
| } | |
| } | |
| "@ | |
| [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
| $OctopusParameters = @{ | |
| "Username" = "Foo Bar"; | |
| "HookUrl" = "https://hooks.slack.com/services/???????????????????"; | |
| "Channel" = "#integrationtesting"; | |
| "IconUrl" = "http://octopusdeploy.com/content/resources/favicon.png"; | |
| "IncludeMachineName" = "false"; | |
| "SendMessageOn" = "FailureAndSuccess"; | |
| "Octopus.Deployment.Error" = "Foo"; | |
| "Octopus.Task.Argument[_Octopus.Web.BaseUrl_]" = "http://?????????.com"; | |
| "Octopus.Web.DeploymentLink" = "/app#/projects/????????????????????????????"; | |
| } | |
| $OctopusReleaseNumber = "1.2.3"; | |
| $OctopusProjectName = "Project Name"; | |
| $OctopusEnvironmentName = "Environment Name"; | |
| $FormattedMachineName = ""; | |
| function Slack-Rich-Notification ($notification) | |
| { | |
| $payload = @{ | |
| channel = $OctopusParameters['Channel'] | |
| username = $OctopusParameters['Username']; | |
| icon_url = $OctopusParameters['IconUrl']; | |
| attachments = @(@{ | |
| fallback = $notification["fallback"]; | |
| color = $notification["color"]; | |
| title = $notification["title"]; | |
| title_link = $notification["title_link"]; | |
| text = $notification["text"]; | |
| fields = @(@{ | |
| title = "Environment: " + $notification['environment']; | |
| short = "true"; | |
| }; | |
| @{ | |
| title = "Release: " + $notification['release']; | |
| short = "true"; | |
| };); | |
| };); | |
| } | |
| Invoke-RestMethod -Method POST -Body ($payload | ConvertTo-Json -Depth 4) -Uri $OctopusParameters['HookUrl'] | |
| } | |
| $SendMessagesOn = $OctopusParameters['SendMessageOn']; | |
| $payload = @{ | |
| release = $OctopusReleaseNumber; | |
| environment = $OctopusEnvironmentName; | |
| title_link = $OctopusParameters['Octopus.Task.Argument[_Octopus.Web.BaseUrl_]'] + $OctopusParameters['Octopus.Web.DeploymentLink'];} | |
| if (($OctopusParameters['Octopus.Deployment.Error'] -eq $null) -and ($SendMessagesOn -eq "FailureAndSuccess")){ | |
| $payload.Add("title", "Deployed $OctopusProjectName"); | |
| $payload.Add("fallback", "Deployed $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName successfully"); | |
| $payload.Add("color", "good"); | |
| Slack-Rich-Notification $payload; | |
| } elseif ($OctopusParameters['Octopus.Deployment.Error'] -ne $null) { | |
| $payload.Add("title", "Failed to deploy $OctopusProjectName"); | |
| $payload.Add("fallback", "Failed to deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName successfully"); | |
| $payload.Add("color", "danger"); | |
| Slack-Rich-Notification $payload; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment