From aec1eec9d9f4fbbd8daa0ab6aa8a81a3e965493e Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Wed, 7 Jul 2021 12:15:08 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 1767 (#14970) * Support building and deploying bicep templates * Add bicep powershell install aka link to deployment error message * Write bicep compiled arm templates to temp directory * Simplify bicep building code/function usage * Use bicep location for compiled arm templates, and remove them on success Co-authored-by: Ben Broderick Phillips --- .../TestResources/New-TestResources.ps1 | 42 +++++++++++++++---- .../TestResources/deploy-test-resources.yml | 2 + 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index aa95298be582..a38155cba855 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -119,6 +119,26 @@ function MergeHashes([hashtable] $source, [psvariable] $dest) { } } +function BuildBicepFile([System.IO.FileSystemInfo] $file) { + if (!(Get-Command bicep -ErrorAction Ignore)) { + Write-Error "A bicep file was found at '$($file.FullName)' but the Azure Bicep CLI is not installed. See https://aka.ms/install-bicep-pwsh" + throw + } + + $tmp = $env:TEMP ? $env:TEMP : [System.IO.Path]::GetTempPath() + $templateFilePath = Join-Path $tmp "test-resources.$(New-Guid).compiled.json" + + # Az can deploy bicep files natively, but by compiling here it becomes easier to parse the + # outputted json for mismatched parameter declarations. + bicep build $file.FullName --outfile $templateFilePath + if ($LASTEXITCODE) { + Write-Error "Failure building bicep file '$($file.FullName)'" + throw + } + + return $templateFilePath +} + # Support actions to invoke on exit. $exitActions = @({ if ($exitActions.Count -gt 1) { @@ -140,15 +160,18 @@ try { # Enumerate test resources to deploy. Fail if none found. $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path $root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path - $templateFileName = 'test-resources.json' $templateFiles = @() - Write-Verbose "Checking for '$templateFileName' files under '$root'" - Get-ChildItem -Path $root -Filter $templateFileName -Recurse | ForEach-Object { - $templateFile = $_.FullName - - Write-Verbose "Found template '$templateFile'" - $templateFiles += $templateFile + 'test-resources.json', 'test-resources.bicep' | ForEach-Object { + Write-Verbose "Checking for '$_' files under '$root'" + Get-ChildItem -Path $root -Filter "$_" -Recurse | ForEach-Object { + Write-Verbose "Found template '$($_.FullName)'" + if ($_.Extension -eq '.bicep') { + $templateFiles += (BuildBicepFile $_) + } else { + $templateFiles += $_.FullName + } + } } if (!$templateFiles) { @@ -556,6 +579,11 @@ try { Log "Invoking post-deployment script '$postDeploymentScript'" &$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters } + + if ($templateFile.EndsWith('.compiled.json')) { + Write-Verbose "Removing compiled bicep file $templateFile" + Remove-Item $templateFile + } } } finally { diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index ac05e14c7934..7eeeda847fad 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -58,3 +58,5 @@ steps: -Force ` -Verbose | Out-Null displayName: Deploy test resources + env: + TEMP: $(Agent.TempDirectory)