-
-
Save tbenade/5378295 to your computer and use it in GitHub Desktop.
Powershell method for posting a deployment to the New Relic HTTP deployment API
This file contains 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
param( | |
$user, | |
$app_id, | |
$revision, | |
$description, | |
$api_key | |
) | |
#thanks to https://gist.github.com/kfrancis/3164709 | |
#Create a URI instance since the HttpWebRequest.Create Method will escape the URL by default. | |
$URL = "http://api.newrelic.com/deployments.xml" | |
$post = "deployment[user]=$user&deployment[app_id]=$app_id&deployment[revision]=$revision&deployment[description]=$description" | |
$URI = New-Object System.Uri($URL,$true) | |
#Create a request object using the URI | |
$request = [System.Net.HttpWebRequest]::Create($URI) | |
#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") | |
) | |
#Since this is a POST we need to set the method type | |
$request.Method = "POST" | |
$request.Headers.Add("x-api-key", $api_key); | |
#Set the Content Type as text/xml since the content will be a block of xml. | |
$request.ContentType = "application/x-www-form-urlencoded" | |
$request.Accept = "text/xml" | |
try { | |
#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() | |
$response.Close() | |
if ([int]$response.StatusCode -eq 201) { | |
Write-Host "NewRelic Deploy API called succeeded." | |
} else { | |
Write-Host "NewRelic Deploy API called failed." | |
} | |
} catch [System.Net.WebException] { | |
$res = $_.Exception.Response | |
Write-Host "NewRelic Deploy API called failed." | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment