Slack - Notify Deployment

Octopus.Script exported 2022-09-08 by twerthi belongs to ‘Slack’ category.

Notifies Slack of deployment status. Uses the Octopus Deploy system variable to determine whether a deployment was successful.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Webhook URL

HookUrl

The Webhook URL provided by Slack, including token.

Channel handle

Channel

Which Slack channel to post notifications to.

Icon URL

IconUrl = https://octopus.com/content/resources/favicon.png

The icon to use for this user in Slack.

null

Username = Octopus Deploy

The username shown in Slack against these notifications.

Include machine name

IncludeMachineName = True

Should machine name be included in notification to Slack?

Use ServerUri

UseServerUri = False

If checked then uses Octopus.Web.ServerUri instead of #{if Octopus.Web.ServerUri}#{Octopus.Web.ServerUri}#{else}#{Octopus.Web.BaseUrl}#{/if}.

Script body

Steps based on this template will execute the following PowerShell script.

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"];
                title_link = $notification["title_link"];
                value = $notification["value"];
                });
            };
        );
    }

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        Invoke-RestMethod -Method POST -Body ($payload | ConvertTo-Json -Depth 4) -Uri $OctopusParameters['HookUrl']  -ContentType 'application/json' -UseBasicParsing
}

$OctopusBaseUri = $OctopusWebBaseUrl
$UseServerUri = [boolean]::Parse($OctopusParameters['UseServerUri']);
if ($UseServerUri) {
	$OctopusBaseUri = $OctopusWebServerUri
}

$IncludeMachineName = [boolean]::Parse($OctopusParameters['IncludeMachineName']);
if ($IncludeMachineName) {
    $MachineName = $OctopusParameters['Octopus.Machine.Name'];
    if ($MachineName) {
      $FormattedMachineName = "($MachineName)";
    }
}

if ($OctopusParameters['Octopus.Deployment.Error'] -eq $null){
    Slack-Rich-Notification @{
        title = "Success";
        title_link = "$OctopusBaseUri$OctopusWebDeploymentLink";
        value = "Deploy <$OctopusBaseUri$OctopusWebProjectLink|$OctopusProjectName> release <$OctopusBaseUri$OctopusWebReleaseLink|$OctopusReleaseNumber> to $OctopusEnvironmentName $OctopusActionTargetRoles $OctopusDeploymentTenantName $FormattedMachineName";
        fallback = "Deployed $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName successfully";
        color = "good";
    };
} else {
    Slack-Rich-Notification @{
        title = "Failed";
        title_link = "$OctopusBaseUri$OctopusWebDeploymentLink";
        value = "Deploy <$OctopusBaseUri$OctopusWebProjectLink|$OctopusProjectName> release <$OctopusBaseUri$OctopusWebReleaseLink|$OctopusReleaseNumber> to $OctopusEnvironmentName $OctopusActionTargetRoles $OctopusDeploymentTenantName $FormattedMachineName";
        fallback = "Failed to deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName";
        color = "danger";
    };
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "21a2ae12-e721-42c1-901d-d6ed08007ca7",
  "Name": "Slack - Notify Deployment",
  "Description": "Notifies Slack of deployment status. Uses the Octopus Deploy system variable to determine whether a deployment was successful.",
  "Version": 12,
  "ExportedAt": "2022-09-08T17:58:51.938Z",
  "ActionType": "Octopus.Script",
  "Author": "twerthi",
  "Parameters": [
    {
      "Name": "HookUrl",
      "Label": "Webhook URL",
      "HelpText": "The Webhook URL provided by Slack, including token.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Name": "Channel",
      "Label": "Channel handle",
      "HelpText": "Which Slack channel to post notifications to.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "IconUrl",
      "Label": "Icon URL",
      "HelpText": "The icon to use for this user in Slack.",
      "DefaultValue": "https://octopus.com/content/resources/favicon.png",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Username",
      "Label": null,
      "HelpText": "The username shown in Slack against these notifications.",
      "DefaultValue": "Octopus Deploy",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "IncludeMachineName",
      "Label": "Include machine name",
      "HelpText": "Should machine name be included in notification to Slack?",
      "DefaultValue": "True",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    },
    {
      "Name": "UseServerUri",
      "Label": "Use ServerUri",
      "HelpText": "If checked then uses `Octopus.Web.ServerUri` instead of `#{if Octopus.Web.ServerUri}#{Octopus.Web.ServerUri}#{else}#{Octopus.Web.BaseUrl}#{/if}`.",
      "DefaultValue": "False",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "function Slack-Rich-Notification ($notification)\n{\n    $payload = @{\n        channel = $OctopusParameters['Channel']\n        username = $OctopusParameters['Username'];\n        icon_url = $OctopusParameters['IconUrl'];\n        attachments = @(\n            @{\n            fallback = $notification[\"fallback\"];\n            color = $notification[\"color\"];\n            fields = @(\n                @{\n                title = $notification[\"title\"];\n                title_link = $notification[\"title_link\"];\n                value = $notification[\"value\"];\n                });\n            };\n        );\n    }\n\n    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n        Invoke-RestMethod -Method POST -Body ($payload | ConvertTo-Json -Depth 4) -Uri $OctopusParameters['HookUrl']  -ContentType 'application/json' -UseBasicParsing\n}\n\n$OctopusBaseUri = $OctopusWebBaseUrl\n$UseServerUri = [boolean]::Parse($OctopusParameters['UseServerUri']);\nif ($UseServerUri) {\n\t$OctopusBaseUri = $OctopusWebServerUri\n}\n\n$IncludeMachineName = [boolean]::Parse($OctopusParameters['IncludeMachineName']);\nif ($IncludeMachineName) {\n    $MachineName = $OctopusParameters['Octopus.Machine.Name'];\n    if ($MachineName) {\n      $FormattedMachineName = \"($MachineName)\";\n    }\n}\n\nif ($OctopusParameters['Octopus.Deployment.Error'] -eq $null){\n    Slack-Rich-Notification @{\n        title = \"Success\";\n        title_link = \"$OctopusBaseUri$OctopusWebDeploymentLink\";\n        value = \"Deploy <$OctopusBaseUri$OctopusWebProjectLink|$OctopusProjectName> release <$OctopusBaseUri$OctopusWebReleaseLink|$OctopusReleaseNumber> to $OctopusEnvironmentName $OctopusActionTargetRoles $OctopusDeploymentTenantName $FormattedMachineName\";\n        fallback = \"Deployed $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName successfully\";\n        color = \"good\";\n    };\n} else {\n    Slack-Rich-Notification @{\n        title = \"Failed\";\n        title_link = \"$OctopusBaseUri$OctopusWebDeploymentLink\";\n        value = \"Deploy <$OctopusBaseUri$OctopusWebProjectLink|$OctopusProjectName> release <$OctopusBaseUri$OctopusWebReleaseLink|$OctopusReleaseNumber> to $OctopusEnvironmentName $OctopusActionTargetRoles $OctopusDeploymentTenantName $FormattedMachineName\";\n        fallback = \"Failed to deploy $OctopusProjectName release $OctopusReleaseNumber to $OctopusEnvironmentName\";\n        color = \"danger\";\n    };\n}",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Slack",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/slack-notify-deployment.json",
  "Website": "/step-templates/21a2ae12-e721-42c1-901d-d6ed08007ca7",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAD9QTFRFW4cu56IU////TcCI4Q1jquLePyZDfNPcN5h70B4l6lOQz+7q8cl3g9Kw5/f2210d8Iax+ePS9rTOjRpToruuV87V3wAABolJREFUeNrsnY166iAMQLGV2VsKlm7v/6xX3dzUNin5gaofeYF5lgMNJFXTvEmYClJBKkgFqSAVpIJUkApSQSpIBakgFaSCVJA3AYnDsBuGIb42SBx2vzG8MMgNRiaUIiBxN4uX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KZfYi+9ztKpu/VKskowqHasmi1t3FKspicogWm49UnDd4r9jpeHWAgXXLT6I2C2Agrlv8UFEbkWYojgI7hY22XMuQP6hURaE59a1jHoiENytgFFIQNQXO9mt+2KQ7dakD0IpHOclLRek0QdJdmuxMGeCjBlA0tyals9IXLdiDpB1tyAKdkrGJgfIilvTiD7ZOCB9kwUEHaD9+MSLP45bfcwE4kGKU6yB0FPShyYTiAMpzqENMrJnURhu/VIkpITmVh+bjCAepNB1q5/WBh6FIPZmgPZjFkog/ZTwSaTfwgFTKLk1TmkfRApycqszH0CI3eqn5A8iBfkCKcRuESikIOdiEOMQuHWkNq/4ID9l1KcEZA9SkJtXTJC/MgoF4bh15DVGDT8XP6Hq1pHdGDX8XKSkhARylDRGiSBxfr5QcusobLrTQBYPGHK3jkd5050EsnzcE4JAFES3jDQfMrf6UWuggwAyQZ+Gm5JLSas10GGkYnFBrgXIUBwETAjDrZuSVmtYyMgTspISMBfEgQ4tkLhngnxCuWhU3TLCLYvk1hhZAx3lQJLcGiNnoEMfpN8L3OrHyBvoSHdLBwR3a4q8gY4NQJCUdLJhoaHsGtnDFAnfGzgUBZn2VLc6jUG0WPQ5spCSbrUxmuZW2Sf7I0hH/E7KoeQDcWWR/LplOsVBtCy1VpPglukITfeUlJQ9j/yAmI410IGlJM9X7qByjV+SYSEgJYXP7N9llKUPdKyRZLzXGgEKxkDHOknWu9+He63bYlA0CbywTohvktBvGvslioY1LISgkF+IYd39jqeYl7RCt74Fu/xoV2S816P4BfhSt2ShCOIkbzA8E0jzNiBeMtL8TCDuXUDCu6iFuvVSIPY9tl9033KvBeI3M0sZJGyWEO3f6PEbrRD9Hxvy7Nr3yUCC34ZD/+efFkhKcOT4HStbfH1kAmluX0n2rnldkHP9+P2bXS40zWuDlI8KUkEqSAWpIBWkglSQClJBKkgFqSAVpIJUkKzxX4ABALbWvZucWNySAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, September 8, 2022