From d6f8dc926abc64fbdca66dfb3366a29fce8218a2 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Wed, 30 Jun 2021 17:46:37 -0400 Subject: [PATCH 1/5] Support building and deploying bicep templates --- .../TestResources/New-TestResources.ps1 | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index aa95298be5..9b05a656e5 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -140,15 +140,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' -and !(Get-Command bicep)) { + Write-Error "A bicep file was found at '$($_.FullName)' but the Azure Bicep CLI is not installed" + throw + } + $templateFiles += $_ + } } if (!$templateFiles) { @@ -432,7 +435,20 @@ try { } # Deploy the templates - foreach ($templateFile in $templateFiles) { + foreach ($templateFileObject in $templateFiles) { + if ($templateFileObject.Extension -eq '.bicep') { + $templateFile = $templateFileObject.DirectoryName + '/test-resources.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 $templateFileObject.FullName --outfile $templateFile + if ($LASTEXITCODE) { + Write-Error "Failure building bicep file '$($templateFileObject.FullName)'" + throw + } + } else { + $templateFile = $templateFileObject.FullName + } + # Deployment fails if we pass in more parameters than are defined. Write-Verbose "Removing unnecessary parameters from template '$templateFile'" $templateJson = Get-Content -LiteralPath $templateFile | ConvertFrom-Json From b26e58bb6db7f2caccb374265a0865ffe74972e1 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Wed, 30 Jun 2021 19:27:28 -0400 Subject: [PATCH 2/5] Add bicep powershell install aka link to deployment error message --- eng/common/TestResources/New-TestResources.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 9b05a656e5..148120e618 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -147,7 +147,7 @@ try { Get-ChildItem -Path $root -Filter "$_" -Recurse | ForEach-Object { Write-Verbose "Found template '$($_.FullName)'" if ($_.Extension -eq '.bicep' -and !(Get-Command bicep)) { - Write-Error "A bicep file was found at '$($_.FullName)' but the Azure Bicep CLI is not installed" + Write-Error "A bicep file was found at '$($_.FullName)' but the Azure Bicep CLI is not installed. See https://aka.ms/install-bicep-pwsh" throw } $templateFiles += $_ From b2e3d2672a0a879f81c4f43f85cc7e9133a84772 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Wed, 30 Jun 2021 20:05:29 -0400 Subject: [PATCH 3/5] Write bicep compiled arm templates to temp directory --- .../TestResources/New-TestResources.ps1 | 27 ++++++++++++------- .../TestResources/deploy-test-resources.yml | 2 ++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 148120e618..323170e318 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -119,6 +119,22 @@ function MergeHashes([hashtable] $source, [psvariable] $dest) { } } +function BuildBicepFile([System.IO.FileSystemInfo] $file) { + $tmp = $env:TEMP ? $env:TEMP : [System.IO.Path]::GetTempPath() + $prefix = ($file | Resolve-Path -Relative) -replace '\.|/', '_' + $templateFilePath = Join-Path $tmp ($prefix + '.test-resources.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 $templateFileObject.FullName --outfile $templateFilePath + if ($LASTEXITCODE) { + Write-Error "Failure building bicep file '$($templateFileObject.FullName)'" + throw + } + + return $templateFilePath +} + # Support actions to invoke on exit. $exitActions = @({ if ($exitActions.Count -gt 1) { @@ -146,7 +162,7 @@ try { Write-Verbose "Checking for '$_' files under '$root'" Get-ChildItem -Path $root -Filter "$_" -Recurse | ForEach-Object { Write-Verbose "Found template '$($_.FullName)'" - if ($_.Extension -eq '.bicep' -and !(Get-Command bicep)) { + if ($_.Extension -eq '.bicep' -and !(Get-Command bicep -ErrorAction Ignore)) { Write-Error "A bicep file was found at '$($_.FullName)' but the Azure Bicep CLI is not installed. See https://aka.ms/install-bicep-pwsh" throw } @@ -437,14 +453,7 @@ try { # Deploy the templates foreach ($templateFileObject in $templateFiles) { if ($templateFileObject.Extension -eq '.bicep') { - $templateFile = $templateFileObject.DirectoryName + '/test-resources.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 $templateFileObject.FullName --outfile $templateFile - if ($LASTEXITCODE) { - Write-Error "Failure building bicep file '$($templateFileObject.FullName)'" - throw - } + $templateFile = BuildBicepFile $templateFileObject } else { $templateFile = $templateFileObject.FullName } diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index ac05e14c79..7eeeda847f 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) From 9052f0c3a2a4c82257bb7f743a627bf980de4afa Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Thu, 1 Jul 2021 21:07:50 -0400 Subject: [PATCH 4/5] Simplify bicep building code/function usage --- .../TestResources/New-TestResources.ps1 | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 323170e318..de61671d48 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -120,15 +120,20 @@ 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() $prefix = ($file | Resolve-Path -Relative) -replace '\.|/', '_' $templateFilePath = Join-Path $tmp ($prefix + '.test-resources.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 $templateFileObject.FullName --outfile $templateFilePath + bicep build $file.FullName --outfile $templateFilePath if ($LASTEXITCODE) { - Write-Error "Failure building bicep file '$($templateFileObject.FullName)'" + Write-Error "Failure building bicep file '$($file.FullName)'" throw } @@ -162,11 +167,11 @@ try { Write-Verbose "Checking for '$_' files under '$root'" Get-ChildItem -Path $root -Filter "$_" -Recurse | ForEach-Object { Write-Verbose "Found template '$($_.FullName)'" - if ($_.Extension -eq '.bicep' -and !(Get-Command bicep -ErrorAction Ignore)) { - Write-Error "A bicep file was found at '$($_.FullName)' but the Azure Bicep CLI is not installed. See https://aka.ms/install-bicep-pwsh" - throw + if ($_.Extension -eq '.bicep') { + $templateFiles += (BuildBicepFile $_) + } else { + $templateFiles += $_.FullName } - $templateFiles += $_ } } @@ -451,13 +456,7 @@ try { } # Deploy the templates - foreach ($templateFileObject in $templateFiles) { - if ($templateFileObject.Extension -eq '.bicep') { - $templateFile = BuildBicepFile $templateFileObject - } else { - $templateFile = $templateFileObject.FullName - } - + foreach ($templateFile in $templateFiles) { # Deployment fails if we pass in more parameters than are defined. Write-Verbose "Removing unnecessary parameters from template '$templateFile'" $templateJson = Get-Content -LiteralPath $templateFile | ConvertFrom-Json From 4ab1fee5895edaad12c3ca6f4a5bbab005a801e6 Mon Sep 17 00:00:00 2001 From: Ben Broderick Phillips Date: Fri, 2 Jul 2021 15:43:14 -0400 Subject: [PATCH 5/5] Use bicep location for compiled arm templates, and remove them on success --- eng/common/TestResources/New-TestResources.ps1 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index de61671d48..a38155cba8 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -126,8 +126,7 @@ function BuildBicepFile([System.IO.FileSystemInfo] $file) { } $tmp = $env:TEMP ? $env:TEMP : [System.IO.Path]::GetTempPath() - $prefix = ($file | Resolve-Path -Relative) -replace '\.|/', '_' - $templateFilePath = Join-Path $tmp ($prefix + '.test-resources.compiled.json') + $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. @@ -580,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 {