Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools for PR 1767 (#14970)
Browse files Browse the repository at this point in the history
* 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 <bebroder@microsoft.com>
  • Loading branch information
azure-sdk and benbp authored Jul 7, 2021
1 parent 1bbf192 commit aec1eec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
42 changes: 35 additions & 7 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions eng/common/TestResources/deploy-test-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ steps:
-Force `
-Verbose | Out-Null
displayName: Deploy test resources
env:
TEMP: $(Agent.TempDirectory)

0 comments on commit aec1eec

Please sign in to comment.