Skip to content

Commit

Permalink
Merge pull request #1506 from softer/newrelic-fixes
Browse files Browse the repository at this point in the history
Fix "NewRelic Deploy - API called failed - HTTP . The remote script failed with exit code 1"
  • Loading branch information
twerthi authored Apr 16, 2024
2 parents 01add5e + bf2de09 commit aa3c356
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions step-templates/newrelic-complete-deployment.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"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.",
"ActionType": "Octopus.Script",
"Version": 26,
"Version": 27,
"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\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 $response.Close()\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 exit 1\n }\n} catch [System.Net.WebException] {\n $ErrorMessage = $_.Exception.Message\n $res = $_.Exception.Response\n Write-Host \"NewRelic Deploy - API called failed - $ErrorMessage\" \n exit 1\n}",
"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",
Expand Down Expand Up @@ -45,10 +45,10 @@
}
}
],
"LastModifiedOn": "2021-07-26T16:50:00.000+00:00",
"LastModifiedBy": "bobjwalker",
"LastModifiedOn": "2024-04-16T20:50:00.0000000+03:00",
"LastModifiedBy": "Softer",
"$Meta": {
"ExportedAt": "2020-04-16T09:18:09.541Z",
"ExportedAt": "2024-04-16T20:50:00.0000000+03:00",
"OctopusVersion": "2019.12.0",
"Type": "ActionTemplate"
},
Expand Down

0 comments on commit aa3c356

Please sign in to comment.