New Relic - Complete Deployment

Octopus.Script exported 2024-04-16 by Softer belongs to ‘New Relic’ category.

Notifies New Relic of a deployment. Sends the revision/version number, deployer, etc from the Octopus deployment.

Parameters

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

Api Key

ApiKey

Your New Relic API key.

App ID

AppId

The ID of the application in New Relic RPM.

User (optional)

User

The name of the user/process that triggered this deployment.

Script body

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

# Minimum PowerShell version for ConvertTo-Json is 3

Add-Type -AssemblyName System.Web

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12

$apiKey = $OctopusParameters['ApiKey']
$user = $OctopusParameters['User']
$appId = $OctopusParameters['AppId']

#https://octopus.com/docs/deployment-process/variables/system-variables
$releaseNumber = $OctopusParameters['Octopus.Release.Number']
$releaseNotes = $OctopusParameters['Octopus.Release.Notes']
$machineName = $OctopusParameters['Octopus.Machine.Name']
$projectName = $OctopusParameters['Octopus.Project.Name']
$deploymentLink = $OctopusParameters['Octopus.Web.DeploymentLink']

## --------------------------------------------------------------------------------------
## Helpers
## --------------------------------------------------------------------------------------
# Helper for validating input parameters
function Validate-Parameter([string]$foo, [string[]]$validInput, $parameterName) {
  Write-Host "${parameterName}: $foo"
  if (! $foo) {
    throw "No value was set for $parameterName, and it cannot be empty"
  }

  if ($validInput) {
    if (! $validInput -contains $foo) {
      throw "'$input' is not a valid input for '$parameterName'"
    }
  }
}

## --------------------------------------------------------------------------------------
## Configuration
## --------------------------------------------------------------------------------------
Validate-Parameter $apiKey -parameterName "Api Key"

if (!$appId) {
  Write-Host "NewRelic Deploy - AppId has not been set yet. Skipping call to API."
  exit 0
}

if ($appId -eq 0) {
  Write-Host "NewRelic Deploy - AppId has been set to zero. Skipping call to API."
  exit 0
}

$userText = $((" by user $user", "")[!$user])
Write-Host ("NewRelic Deploy - Notify deployment{0} - App {1} - Revision {2}" -f $userText, $appId, $revision)


# https://rpm.newrelic.com/api/explore/application_deployments/create?application_id=1127348
$deployment = New-Object -TypeName PSObject
$deployment | Add-Member -MemberType NoteProperty -Name "user" -Value $user
$deployment | Add-Member -MemberType NoteProperty -Name "revision" -Value $releaseNumber
$deployment | Add-Member -MemberType NoteProperty -Name "changelog" -Value $releaseNotes
$deployment | Add-Member -MemberType NoteProperty -Name "description" -Value "Octopus deployment of $projectName to $machineName. ($deploymentLink)"

$deploymentContainer = New-Object -TypeName PSObject
$deploymentContainer | Add-Member -MemberType NoteProperty -Name "deployment" -Value $deployment

$post = $deploymentContainer | ConvertTo-Json
Write-Debug $post

# in production, we need to
#Create a URI instance since the HttpWebRequest.Create Method will escape the URL by default.
$URL = "https://api.newrelic.com/v2/applications/$appId/deployments.json"
$URI = New-Object System.Uri($URL,$true)

#Create a request object using the URI
$request = [System.Net.HttpWebRequest]::Create($URI)
$request.Method = "POST"
$request.Headers.Add("X-Api-Key","$apiKey");
$request.ContentType = "application/json"

#Build up a nice User Agent
$request.UserAgent = $(
"{0} (PowerShell {1}; .NET CLR {2}; {3})" -f $UserAgent,
$(if($Host.Version){$Host.Version}else{"1.0"}),
[Environment]::Version,
[Environment]::OSVersion.ToString().Replace("Microsoft Windows ", "Win")
)
$ReturnCode = 0
try {
  Write-Host "Posting data to $URL"
  #Create a new stream writer to write the xml to the request stream.
  $stream = New-Object IO.StreamWriter $request.GetRequestStream()
  $stream.AutoFlush = $True
  $PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post)
  $stream.Write($PostStr, 0,$PostStr.length)
  $stream.Close()

  #Make the request and get the response
  $response = $request.GetResponse()

  if ([int]$response.StatusCode -eq 201) {
    Write-Host "NewRelic Deploy - API called succeeded - HTTP $($response.StatusCode)."
  } else {
    Write-Host "NewRelic Deploy - API called failed - HTTP $($response.StatusCode)."
    $ReturnCode = 1
  }
  $response.Close()
} catch {
  $ErrorMessage = $_.Exception.Message
  $res = $_.Exception.Response
  Write-Host "NewRelic Deploy - API called failed - $ErrorMessage"
  $ReturnCode = 1
}
exit $ReturnCode

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": "7c88ea1e-de71-452d-be7e-b99dda397ba7",
  "Name": "New Relic - Complete Deployment",
  "Description": "Notifies [New Relic](https://newrelic.com) of a deployment.\nSends the revision/version number, deployer, etc from the Octopus deployment.",
  "Version": 27,
  "ExportedAt": "2024-04-16T20:50:00.0000000+03:00",
  "ActionType": "Octopus.Script",
  "Author": "Softer",
  "Parameters": [
    {
      "Id": "caf3f015-671f-4600-bce0-ebcc7d5957fe",
      "Name": "ApiKey",
      "Label": "Api Key",
      "HelpText": "Your New Relic API key.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "3748da93-614d-4040-8c63-7c86a260c79e",
      "Name": "AppId",
      "Label": "App ID",
      "HelpText": "The ID of the application in New Relic RPM.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "719ab099-a361-4a58-ad34-b1d4acb70dd2",
      "Name": "User",
      "Label": "User (optional)",
      "HelpText": "The name of the user/process that triggered this deployment.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "# Minimum PowerShell version for ConvertTo-Json is 3\n\nAdd-Type -AssemblyName System.Web\n\n[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12\n\n$apiKey = $OctopusParameters['ApiKey']\n$user = $OctopusParameters['User']\n$appId = $OctopusParameters['AppId']\n\n#https://octopus.com/docs/deployment-process/variables/system-variables\n$releaseNumber = $OctopusParameters['Octopus.Release.Number']\n$releaseNotes = $OctopusParameters['Octopus.Release.Notes']\n$machineName = $OctopusParameters['Octopus.Machine.Name']\n$projectName = $OctopusParameters['Octopus.Project.Name']\n$deploymentLink = $OctopusParameters['Octopus.Web.DeploymentLink']\n\n## --------------------------------------------------------------------------------------\n## Helpers\n## --------------------------------------------------------------------------------------\n# Helper for validating input parameters\nfunction Validate-Parameter([string]$foo, [string[]]$validInput, $parameterName) {\n  Write-Host \"${parameterName}: $foo\"\n  if (! $foo) {\n    throw \"No value was set for $parameterName, and it cannot be empty\"\n  }\n\n  if ($validInput) {\n    if (! $validInput -contains $foo) {\n      throw \"'$input' is not a valid input for '$parameterName'\"\n    }\n  }\n}\n\n## --------------------------------------------------------------------------------------\n## Configuration\n## --------------------------------------------------------------------------------------\nValidate-Parameter $apiKey -parameterName \"Api Key\"\n\nif (!$appId) {\n  Write-Host \"NewRelic Deploy - AppId has not been set yet. Skipping call to API.\"\n  exit 0\n}\n\nif ($appId -eq 0) {\n  Write-Host \"NewRelic Deploy - AppId has been set to zero. Skipping call to API.\"\n  exit 0\n}\n\n$userText = $((\" by user $user\", \"\")[!$user])\nWrite-Host (\"NewRelic Deploy - Notify deployment{0} - App {1} - Revision {2}\" -f $userText, $appId, $revision)\n\n\n# https://rpm.newrelic.com/api/explore/application_deployments/create?application_id=1127348\n$deployment = New-Object -TypeName PSObject\n$deployment | Add-Member -MemberType NoteProperty -Name \"user\" -Value $user\n$deployment | Add-Member -MemberType NoteProperty -Name \"revision\" -Value $releaseNumber\n$deployment | Add-Member -MemberType NoteProperty -Name \"changelog\" -Value $releaseNotes\n$deployment | Add-Member -MemberType NoteProperty -Name \"description\" -Value \"Octopus deployment of $projectName to $machineName. ($deploymentLink)\"\n\n$deploymentContainer = New-Object -TypeName PSObject\n$deploymentContainer | Add-Member -MemberType NoteProperty -Name \"deployment\" -Value $deployment\n\n$post = $deploymentContainer | ConvertTo-Json\nWrite-Debug $post\n\n# in production, we need to\n#Create a URI instance since the HttpWebRequest.Create Method will escape the URL by default.\n$URL = \"https://api.newrelic.com/v2/applications/$appId/deployments.json\"\n$URI = New-Object System.Uri($URL,$true)\n\n#Create a request object using the URI\n$request = [System.Net.HttpWebRequest]::Create($URI)\n$request.Method = \"POST\"\n$request.Headers.Add(\"X-Api-Key\",\"$apiKey\");\n$request.ContentType = \"application/json\"\n\n#Build up a nice User Agent\n$request.UserAgent = $(\n\"{0} (PowerShell {1}; .NET CLR {2}; {3})\" -f $UserAgent,\n$(if($Host.Version){$Host.Version}else{\"1.0\"}),\n[Environment]::Version,\n[Environment]::OSVersion.ToString().Replace(\"Microsoft Windows \", \"Win\")\n)\n$ReturnCode = 0\ntry {\n  Write-Host \"Posting data to $URL\"\n  #Create a new stream writer to write the xml to the request stream.\n  $stream = New-Object IO.StreamWriter $request.GetRequestStream()\n  $stream.AutoFlush = $True\n  $PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post)\n  $stream.Write($PostStr, 0,$PostStr.length)\n  $stream.Close()\n\n  #Make the request and get the response\n  $response = $request.GetResponse()\n\n  if ([int]$response.StatusCode -eq 201) {\n    Write-Host \"NewRelic Deploy - API called succeeded - HTTP $($response.StatusCode).\"\n  } else {\n    Write-Host \"NewRelic Deploy - API called failed - HTTP $($response.StatusCode).\"\n    $ReturnCode = 1\n  }\n  $response.Close()\n} catch {\n  $ErrorMessage = $_.Exception.Message\n  $res = $_.Exception.Response\n  Write-Host \"NewRelic Deploy - API called failed - $ErrorMessage\"\n  $ReturnCode = 1\n}\nexit $ReturnCode\n",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "false",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.FeedId": null,
    "Octopus.Action.Package.PackageId": null
  },
  "Category": "New Relic",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/newrelic-complete-deployment.json",
  "Website": "/step-templates/7c88ea1e-de71-452d-be7e-b99dda397ba7",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGX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