Created
December 21, 2019 00:02
-
-
Save sharpjs/7fa1c3f11eb747589fdbc27a92528f89 to your computer and use it in GitHub Desktop.
How to create an Application Insights release annotation (in Octopus Deploy)
This file contains hidden or 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
using namespace System.Net | |
#Requires -Version 5.1 | |
$ErrorActionPreference = "Stop" | |
function New-ApplicationInsightsReleaseAnnotationCore { | |
param ( | |
[Parameter(Mandatory, Position=0)] | |
[string] $ApplicationId, | |
[Parameter(Mandatory, Position=1)] | |
[string] $ApiKey, | |
[Parameter(Mandatory, Position=2)] | |
[string] $ReleaseName, | |
[Parameter()] | |
[int] $RetryCount = 4 | |
) | |
# Discover API endpoint | |
Write-Host "Discovering Application Insights release annotation API endpoint." | |
$Url = "http://go.microsoft.com/fwlink/?prd=11901&pver=1.0&sbp=Application%20Insights&plcid=0x409&clcid=0x409&ar=Annotations&sar=Create%20Annotation" | |
$Response = Invoke-WebRequest ` | |
-Uri $Url ` | |
-MaximumRedirection 0 ` | |
-UseBasicParsing ` | |
-ErrorAction Ignore | |
if ($Response -and $Response.StatusCode -eq "302") { | |
$Url = $Response.Headers.Location | |
$Url = "$Url/applications/$ApplicationId/Annotations?api-version=2015-11" | |
} else { | |
throw "Failed to discover the Application Insights release annotation API endpoint." | |
} | |
Write-Verbose "Endpoint: $Url" | |
# Create release annotation | |
Write-Host "Creating release annotation: $ReleaseName" | |
$Body = [ordered] @{ | |
Id = [guid]::NewGuid() | |
AnnotationName = $ReleaseName | |
EventTime = (Get-Date).ToUniversalTime().ToString("s") | |
Category = "Deployment" | |
Properties = @{ ReleaseName = $ReleaseName } | ConvertTo-Json -Compress | |
} | |
$BodyJson = $Body | ConvertTo-Json | |
Invoke-RestMethod ` | |
-Uri $Url ` | |
-Method Put ` | |
-Body $BodyJson ` | |
-Headers @{ "X-AIAPIKEY" = $ApiKey } ` | |
-ContentType "application/json; charset=utf-8" ` | |
-UseBasicParsing | |
Write-Host "Release annotation created." | |
} | |
function New-ApplicationInsightsReleaseAnnotation { | |
param ( | |
[Parameter(Mandatory, Position=0)] | |
[string] $ApplicationId, | |
[Parameter(Mandatory, Position=1)] | |
[string] $ApiKey, | |
[Parameter(Mandatory, Position=2)] | |
[string] $ReleaseName, | |
[Parameter()] | |
[ValidateRange(0, [int]::MaxValue)] | |
[int] $RetryCount = 4 | |
) | |
$RetryWaitSeconds = 10 | |
$Protocol = [ServicePointManager]::SecurityProtocol | |
try { | |
[ServicePointManager]::SecurityProtocol = [SecurityProtocolType]::Tls12 | |
foreach ($Attempt in 0..$RetryCount) { | |
try { | |
New-ApplicationInsightsReleaseAnnotationCore @PSBoundParameters | |
return | |
} | |
catch { | |
Write-Warning $_.Exception | |
} | |
if ($Attempt -lt $RetryCount) { | |
Write-Host "Retrying in $RetryWaitSeconds seconds..." | |
Start-Sleep -Seconds $RetryWaitSeconds | |
} | |
} | |
throw "Failed to create release annotation." | |
} | |
finally { | |
[ServicePointManager]::SecurityProtocol = $Protocol | |
} | |
} | |
New-ApplicationInsightsReleaseAnnotation ` | |
-ApplicationId $OctopusParameters["ApplicationId"] ` | |
-ApiKey $OctopusParameters["ApiKey"] ` | |
-ReleaseName $OctopusParameters["ReleaseName"] ` | |
-RetryCount $OctopusParameters["RetryCount"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment