| 1 |
Param( |
| 2 |
[string] $GuardianCliLocation, |
| 3 |
[string] $Repository, |
| 4 |
[string] $BranchName='master', |
| 5 |
[string] $WorkingDirectory, |
| 6 |
[string] $AzureDevOpsAccessToken, |
| 7 |
[string] $GuardianLoggerLevel='Standard' |
| 8 |
) |
| 9 |
|
| 10 |
$ErrorActionPreference = 'Stop' |
| 11 |
Set-StrictMode -Version 2.0 |
| 12 |
$disableConfigureToolsetImport = $true |
| 13 |
$LASTEXITCODE = 0 |
| 14 |
|
| 15 |
. $PSScriptRoot\..\tools.ps1 |
| 16 |
|
| 17 |
# Don't display the console progress UI - it's a huge perf hit |
| 18 |
$ProgressPreference = 'SilentlyContinue' |
| 19 |
|
| 20 |
# Construct basic auth from AzDO access token; construct URI to the repository's gdn folder stored in that repository; construct location of zip file |
| 21 |
$encodedPat = [Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$AzureDevOpsAccessToken")) |
| 22 |
$escapedRepository = [Uri]::EscapeDataString("/$Repository/$BranchName/.gdn") |
| 23 |
$uri = "https://dev.azure.com/dnceng/internal/_apis/git/repositories/sdl-tool-cfg/Items?path=$escapedRepository&versionDescriptor[versionOptions]=0&`$format=zip&api-version=5.0-preview.1" |
| 24 |
$zipFile = "$WorkingDirectory/gdn.zip" |
| 25 |
|
| 26 |
Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 27 |
$gdnFolder = (Join-Path $WorkingDirectory '.gdn') |
| 28 |
try { |
| 29 |
# We try to download the zip; if the request fails (e.g. the file doesn't exist), we catch it and init guardian instead |
| 30 |
Write-Host 'Downloading gdn folder from internal config repostiory...' |
| 31 |
Invoke-WebRequest -Headers @{ "Accept"="application/zip"; "Authorization"="Basic $encodedPat" } -Uri $uri -OutFile $zipFile |
| 32 |
if (Test-Path $gdnFolder) { |
| 33 |
# Remove the gdn folder if it exists (it shouldn't unless there's too much caching; this is just in case) |
| 34 |
Remove-Item -Force -Recurse $gdnFolder |
| 35 |
} |
| 36 |
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipFile, $WorkingDirectory) |
| 37 |
Write-Host $gdnFolder |
| 38 |
ExitWithExitCode 0 |
| 39 |
} catch [System.Net.WebException] { } # Catch and ignore webexception |
| 40 |
try { |
| 41 |
# if the folder does not exist, we'll do a guardian init and push it to the remote repository |
| 42 |
Write-Host 'Initializing Guardian...' |
| 43 |
Write-Host "$GuardianCliLocation init --working-directory $WorkingDirectory --logger-level $GuardianLoggerLevel" |
| 44 |
& $GuardianCliLocation init --working-directory $WorkingDirectory --logger-level $GuardianLoggerLevel |
| 45 |
if ($LASTEXITCODE -ne 0) { |
| 46 |
Write-PipelineTelemetryError -Force -Category 'Build' -Message "Guardian init failed with exit code $LASTEXITCODE." |
| 47 |
ExitWithExitCode $LASTEXITCODE |
| 48 |
} |
| 49 |
# We create the mainbaseline so it can be edited later |
| 50 |
Write-Host "$GuardianCliLocation baseline --working-directory $WorkingDirectory --name mainbaseline" |
| 51 |
& $GuardianCliLocation baseline --working-directory $WorkingDirectory --name mainbaseline |
| 52 |
if ($LASTEXITCODE -ne 0) { |
| 53 |
Write-PipelineTelemetryError -Force -Category 'Build' -Message "Guardian baseline failed with exit code $LASTEXITCODE." |
| 54 |
ExitWithExitCode $LASTEXITCODE |
| 55 |
} |
| 56 |
& $(Join-Path $PSScriptRoot 'push-gdn.ps1') -Repository $Repository -BranchName $BranchName -GdnFolder $gdnFolder -AzureDevOpsAccessToken $AzureDevOpsAccessToken -PushReason 'Initialize gdn folder' |
| 57 |
ExitWithExitCode 0 |
| 58 |
} |
| 59 |
catch { |
| 60 |
Write-Host $_.ScriptStackTrace |
| 61 |
Write-PipelineTelemetryError -Category 'Sdl' -Message $_ |
| 62 |
ExitWithExitCode 1 |
| 63 |
} |