RabbitMQ - Notify Deploy

Octopus.Script exported 2015-07-05 by alfhenrik belongs to ‘RabbitMQ’ category.

Notifies a deploy by sending a message into rabbitMQ. The message contains all octopus variables and these can be used to have some insight on the deploy. The step is very beta, it is advised to improve it to match real case scenarios.

Parameters

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

rabbitmq username

rabbitUsername =

username used to publish message

rabbitmq user password

rabbitPassword =

null

rabbitMQ exchange

rabbitExchange

null

rabbitmq virtual host

rabbitVirtualHost

null

rabbitmq url endpoint

rabbitUrl = http://localhost:15672

null

Script body

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

function Add-ValueToHashtable
{
    param(
    [Parameter(Mandatory = 1)][object]$variable,
    [Parameter(Mandatory = 1)][hashtable]$hashtable
    )

    if ($variable.value.GetType() -eq [System.String])
    {
        $hashtable.Add($variable.Name, $variable.value)
        return
    }

    if (($variable.value.GetType() -eq (New-Object 'System.Collections.Generic.Dictionary[String,String]').GetType()) -or ($variable.value.GetType() -eq [Hashtable]))
    {
        foreach ($element in $variable.Value.GetEnumerator())
        {
            $obj = New-Object PsObject -Property @{ Name = $element.Key; Value = $element.Value }
            Add-ValueToHashtable -variable $obj -hashtable $hashtable
        }
        return
    }

    throw "Add-ValueToHashtable method does not know what to do with type " + $variable.value.GetType().Name
}

function Get-UnixDate
{
    $epoch = Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0	
    $now = Get-Date
    return ([math]::truncate($now.ToUniversalTime().Subtract($epoch).TotalMilliSeconds))
}

function Get-IsRollback
{
    $currentVersion = New-Object -TypeName System.Version -ArgumentList $OctopusReleaseNumber
    $prevVersion = New-Object -TypeName System.Version -ArgumentList $OctopusReleasePreviousNumber

    return ($currentVersion.CompareTo($prevVersion) -lt 0)
}

function Get-OctopusVariablesJson
{
    $octoVariables = @{}

    foreach ($var in (Get-Variable -Name OctopusParameters*))
    {
        Add-ValueToHashtable -variable $var -hashtable $octoVariables
    }

    $octoVariables.Add("isrollback", (Get-IsRollback))
    $octoVariables.Add("timestamp", (Get-UnixDate))
    $octoVariables.Add("safeprojectname", $OctopusParameters["Octopus.Project.Name"].Replace(" ", "_"))

    return ($octoVariables | ConvertTo-Json -Compress)
}

function ConvertTo-AsciiString
{
    param(  
    [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
    [string]$input)
    
    process {
        #custom desired transformation
        $tmp = $input.Replace([char]0x00EC, "i")
        #fallback
        $bytes = [System.Text.Encoding]::UTF8.GetBytes($tmp)
        $asciiArray = [System.Text.Encoding]::Convert([System.Text.Encoding]::UTF8, [System.Text.Encoding]::ASCII, $bytes)
        $ascistring = [System.Text.Encoding]::ASCII.GetString($asciiArray)
        return $ascistring
    }
}

$json = Get-OctopusVariablesJson | ConvertTo-AsciiString
$body = New-Object PsObject -Property @{ properties = @{}; routing_key = "#"; payload = $json; payload_encoding = "string" } | ConvertTo-Json -Compress

$securepassword = ConvertTo-SecureString $rabbitPassword -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($rabbitUsername, $securepassword)

Invoke-RestMethod -Uri "$rabbitUrl/api/exchanges/$rabbitVirtualHost/$rabbitExchange/publish" -Method Post -Credential $cred -Body $body -ContentType "application/json"

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": "309f8497-7f79-4979-89a6-5d7e15d83ae2",
  "Name": "RabbitMQ - Notify Deploy",
  "Description": "Notifies a deploy by sending a message into rabbitMQ. The message contains all octopus variables and these can be used to have some insight on the deploy. The step is very beta, it is advised to improve it to match real case scenarios.",
  "Version": 0,
  "ExportedAt": "2015-07-05T08:47:05.572+00:00",
  "ActionType": "Octopus.Script",
  "Author": "alfhenrik",
  "Parameters": [
    {
      "Name": "rabbitUsername",
      "Label": "rabbitmq username",
      "HelpText": "username used to publish message",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "rabbitPassword",
      "Label": "rabbitmq user password",
      "HelpText": null,
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Name": "rabbitExchange",
      "Label": "rabbitMQ exchange",
      "HelpText": null,
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "rabbitVirtualHost",
      "Label": "rabbitmq virtual host",
      "HelpText": null,
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "rabbitUrl",
      "Label": "rabbitmq url endpoint",
      "HelpText": null,
      "DefaultValue": "http://localhost:15672",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "function Add-ValueToHashtable\n{\n    param(\n    [Parameter(Mandatory = 1)][object]$variable,\n    [Parameter(Mandatory = 1)][hashtable]$hashtable\n    )\n\n    if ($variable.value.GetType() -eq [System.String])\n    {\n        $hashtable.Add($variable.Name, $variable.value)\n        return\n    }\n\n    if (($variable.value.GetType() -eq (New-Object 'System.Collections.Generic.Dictionary[String,String]').GetType()) -or ($variable.value.GetType() -eq [Hashtable]))\n    {\n        foreach ($element in $variable.Value.GetEnumerator())\n        {\n            $obj = New-Object PsObject -Property @{ Name = $element.Key; Value = $element.Value }\n            Add-ValueToHashtable -variable $obj -hashtable $hashtable\n        }\n        return\n    }\n\n    throw \"Add-ValueToHashtable method does not know what to do with type \" + $variable.value.GetType().Name\n}\n\nfunction Get-UnixDate\n{\n    $epoch = Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0\t\n    $now = Get-Date\n    return ([math]::truncate($now.ToUniversalTime().Subtract($epoch).TotalMilliSeconds))\n}\n\nfunction Get-IsRollback\n{\n    $currentVersion = New-Object -TypeName System.Version -ArgumentList $OctopusReleaseNumber\n    $prevVersion = New-Object -TypeName System.Version -ArgumentList $OctopusReleasePreviousNumber\n\n    return ($currentVersion.CompareTo($prevVersion) -lt 0)\n}\n\nfunction Get-OctopusVariablesJson\n{\n    $octoVariables = @{}\n\n    foreach ($var in (Get-Variable -Name OctopusParameters*))\n    {\n        Add-ValueToHashtable -variable $var -hashtable $octoVariables\n    }\n\n    $octoVariables.Add(\"isrollback\", (Get-IsRollback))\n    $octoVariables.Add(\"timestamp\", (Get-UnixDate))\n    $octoVariables.Add(\"safeprojectname\", $OctopusParameters[\"Octopus.Project.Name\"].Replace(\" \", \"_\"))\n\n    return ($octoVariables | ConvertTo-Json -Compress)\n}\n\nfunction ConvertTo-AsciiString\n{\n    param(  \n    [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]\n    [string]$input)\n    \n    process {\n        #custom desired transformation\n        $tmp = $input.Replace([char]0x00EC, \"i\")\n        #fallback\n        $bytes = [System.Text.Encoding]::UTF8.GetBytes($tmp)\n        $asciiArray = [System.Text.Encoding]::Convert([System.Text.Encoding]::UTF8, [System.Text.Encoding]::ASCII, $bytes)\n        $ascistring = [System.Text.Encoding]::ASCII.GetString($asciiArray)\n        return $ascistring\n    }\n}\n\n$json = Get-OctopusVariablesJson | ConvertTo-AsciiString\n$body = New-Object PsObject -Property @{ properties = @{}; routing_key = \"#\"; payload = $json; payload_encoding = \"string\" } | ConvertTo-Json -Compress\n\n$securepassword = ConvertTo-SecureString $rabbitPassword -AsPlainText -Force\n$cred = New-Object System.Management.Automation.PSCredential ($rabbitUsername, $securepassword)\n\nInvoke-RestMethod -Uri \"$rabbitUrl/api/exchanges/$rabbitVirtualHost/$rabbitExchange/publish\" -Method Post -Credential $cred -Body $body -ContentType \"application/json\""
  },
  "Category": "RabbitMQ",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/rabbitmq-notify-deploy.json",
  "Website": "/step-templates/309f8497-7f79-4979-89a6-5d7e15d83ae2",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBQTFRF/////2YA/9m//4xA/7J//+LP//Xv/8Wf/4Mw/+zf/3kg/3AQ/5ZQ/6Bg/7yP/6lwqNBbTgAAAbpJREFUeNrs3ctygjAYgFHD/aZ9/7etWyuJrYxNwpxv/4+cARYSjJeLJEmSJEmSdMrm5qn5F2P989iQk9EtYaelSx/UfN2bCm2TizFMIdKUOitjbCpcM0Gijrskfk6a+FTosjjG8NYhLamxubATcr9Pord5aircckCSRxRi11aXnGrLgzQgICAgICAgICAgICAgICBFQvqzQC4gICAgICAgICAgICAgICAgICAgICAgtS0rbOGtVyHzQSLv8M7hPX8+SBj3ZobpxdQ0FAcJ16e1nn5cXg2F9db8sf7TkP9r608CiVzHNULW+SSQQz90KAoSQEBAQEBAQEBAQEBAQEBAQEBAQEB+NrUPVQrZ2Xunv1YI2V8i6KfaILGljmGtC/IV/bymLkhiMbStCZJ6Y2CsCZLamKoBAQEBAQEBAQEBAQH5IGQ7yxer03zVTVxblT18iL5UPiyVQdb9B1tzdQ/o7jf880mZu7XKh9jr4zPs9vUr9ZYVQEBAQEBAQEBAQEBAQEBAQEBAQPJCtpIc2wFIXxLk0IYiYzmOQ9uJ3M9JWwajPbxZzWVoCijrH9lKkiRJkiRJH+tbgAEAnHhDbpt1S6YAAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Sunday, July 5, 2015