forked from vanderby/SonarQube-AzureAppService
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeploy-SonarQubeAzureAppService.ps1
87 lines (74 loc) · 3.51 KB
/
Deploy-SonarQubeAzureAppService.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
param(
[string]$ApplicationInsightsApiKey = $Env:Deployment_Telemetry_Instrumentation_Key
)
function TrackTimedEvent {
param (
[string]$InstrumentationKey,
[string]$EventName,
[scriptblock]$ScriptBlock,
[Object[]]$ScriptBlockArguments
)
[System.Diagnostics.Stopwatch]$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ScriptBlockArguments
$stopwatch.Stop()
if($InstrumentationKey)
{
$uniqueId = ''
if($Env:WEBSITE_INSTANCE_ID)
{
$uniqueId = $Env:WEBSITE_INSTANCE_ID.substring(5,15)
}
$properties = @{
"Location" = $Env:REGION_NAME;
"SKU" = $Env:WEBSITE_SKU;
"Processor Count" = $Env:NUMBER_OF_PROCESSORS;
"Always On" = $Env:WEBSITE_SCM_ALWAYS_ON_ENABLED;
"UID" = $uniqueId
}
$measurements = @{
'duration (ms)' = $stopwatch.ElapsedMilliseconds
}
$body = ConvertTo-Json -Depth 5 -InputObject @{
name = "Microsoft.ApplicationInsights.Dev.$InstrumentationKey.Event";
time = [Datetime]::UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
iKey = $InstrumentationKey;
data = @{
baseType = "EventData";
baseData = @{
ver = 2;
name = $EventName;
properties = $properties;
measurements = $measurements;
}
};
}
Invoke-RestMethod -Method POST -Uri "https://dc.services.visualstudio.com/v2/track" -ContentType "application/json" -Body $body | out-null
}
}
TrackTimedEvent -InstrumentationKey $ApplicationInsightsApiKey -EventName 'Download And Extract Binaries' -ScriptBlock {
Write-Output 'Copy wwwroot folder'
xcopy wwwroot ..\wwwroot /YI
Write-Output 'Setting Security to TLS 1.2'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Output 'Prevent the progress meter from trying to access the console'
$global:progressPreference = 'SilentlyContinue'
Write-Output 'Getting a list of downloads'
$downloadSource = 'https://binaries.sonarsource.com/Distribution/sonarqube/'
$allDownloads = Invoke-WebRequest -Uri $downloadSource -UseBasicParsing
$zipFiles = $allDownloads[0].Links | Where-Object { $_.href.EndsWith('.zip') -and !($_.href.contains('alpha') -or $_.href.contains('RC')) }
# We sort by a custom expression so that we sort based on a version and not as a string. This results in the proper order given values such as 7.9.zip and 7.9.1.zip.
# In the expression we use RegEx to find the "Version.zip" string, then split and grab the first to get just the "Version" and finally cast that to a version object
$sortedZipFiles = $zipFiles | Sort-Object -Property @{ Expression = { [Version]([RegEx]::Match($_.href, '\d+.\d+.?(\d+)?.zip').Value -Split ".zip")[0] } }
$latestFile = $sortedZipFiles[-1]
$downloadUri = $downloadSource + $latestFile.href
Write-Output "Downloading '$downloadUri'"
$outputFile = "..\wwwroot\$($latestFile.href)"
Invoke-WebRequest -Uri $downloadUri -OutFile $outputFile -UseBasicParsing
Write-Output 'Done downloading file'
TrackTimedEvent -InstrumentationKey $ApplicationInsightsApiKey -EventName 'Extract Binaries' -ScriptBlockArguments $outputFile -ScriptBlock {
param([string]$outputFile)
Write-Output 'Extracting zip'
Expand-Archive -Path $outputFile -DestinationPath '..\wwwroot' -Force
Write-Output 'Extraction complete'
}
}