Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix New-TestResources.ps1 to run pre/post deployment script #1876

Merged
11 commits merged into from
Aug 13, 2021
26 changes: 12 additions & 14 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ try {
Get-ChildItem -Path $root -Filter "$_" -Recurse | ForEach-Object {
Write-Verbose "Found template '$($_.FullName)'"
if ($_.Extension -eq '.bicep') {
$templateFile = @{originalFilePath = $_.FullName; jsonFilePath = (BuildBicepFile $_)}
$templateFiles += $templateFile
$templateFiles += (BuildBicepFile $_)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not a fan of this change, @benbp. The temp file that gets returned and printed everywhere is an implementation detail of this script with no obvious tie-back to the original .bicep file. At the very least BuildBicepFile should print the source and destination paths, but that's still requiring users to look up through the logs to find it. How it was with the object I thought was a great idea, so you could even print both if someone needed to find the actual JSON template (I've ran into a few issues where this was needed with bicep files).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might be commenting on an outdated commit? Sorry for the messy commits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, at the time I commented, the variable was just a single string path. You had it as a multi-property object but Ben asked you to change it. Did you chances it back to an object? We shouldn't be printing the path to a temp JSON file that the user can't use to diagnose failures.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is an outdated diff. The main code is object based: https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/TestResources/New-TestResources.ps1#L170-L171

I think it may actually be helpful to print both paths, because when debugging bicep scripts, I often need to see the compiled output to help diagnose the problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I originally commented as well. The commit that had the original and transformed names as properties was best. My only comment from then was that the message printed when the two were different should list both. From the number of people I help set up their test resources (rather, customize them) it's clear expecting them to know how it works e.g., that it transforms the bicep file into json behind the scenes would be unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep makes sense. I plan to roll in those changes with my fix to #1824 (I promise I'll get to it @pakrym, just dealing with weird windows encryption API issues).

} else {
$templateFile = @{originalFilePath = $_.FullName; jsonFilePath = $_.FullName}
$templateFiles += $templateFile
$templateFiles += $_.FullName
}
}
}
Expand Down Expand Up @@ -459,8 +457,8 @@ try {
# Deploy the templates
foreach ($templateFile in $templateFiles) {
# Deployment fails if we pass in more parameters than are defined.
Write-Verbose "Removing unnecessary parameters from template '$($templateFile.jsonFilePath)'"
$templateJson = Get-Content -LiteralPath $templateFile.jsonFilePath | ConvertFrom-Json
Write-Verbose "Removing unnecessary parameters from template '$templateFile'"
$templateJson = Get-Content -LiteralPath $templateFile | ConvertFrom-Json
$templateParameterNames = $templateJson.parameters.PSObject.Properties.Name

$templateFileParameters = $templateParameters.Clone()
Expand All @@ -471,7 +469,7 @@ try {
}
}

$preDeploymentScript = $templateFile.originalFilePath | Split-Path | Join-Path -ChildPath 'test-resources-pre.ps1'
$preDeploymentScript = $templateFile | Split-Path | Join-Path -ChildPath 'test-resources-pre.ps1'
if (Test-Path $preDeploymentScript) {
Log "Invoking pre-deployment script '$preDeploymentScript'"
&$preDeploymentScript -ResourceGroupName $ResourceGroupName @PSBoundParameters
Expand All @@ -484,7 +482,7 @@ try {
if ($CI) {
$DebugPreference = 'Continue'
}
New-AzResourceGroupDeployment -Name $BaseName -ResourceGroupName $resourceGroup.ResourceGroupName -TemplateFile $templateFile.jsonFilePath -TemplateParameterObject $templateFileParameters -Force:$Force
New-AzResourceGroupDeployment -Name $BaseName -ResourceGroupName $resourceGroup.ResourceGroupName -TemplateFile $templateFile -TemplateParameterObject $templateFileParameters -Force:$Force
} catch {
Write-Output @'
#####################################################
Expand All @@ -500,7 +498,7 @@ try {

if ($deployment.ProvisioningState -eq 'Succeeded') {
# New-AzResourceGroupDeployment would've written an error and stopped the pipeline by default anyway.
Write-Verbose "Successfully deployed template '$($templateFile.jsonFilePath)' to resource group '$($resourceGroup.ResourceGroupName)'"
Write-Verbose "Successfully deployed template '$templateFile' to resource group '$($resourceGroup.ResourceGroupName)'"
}

$serviceDirectoryPrefix = $serviceName.ToUpperInvariant() + "_"
Expand Down Expand Up @@ -538,7 +536,7 @@ try {
Write-Host 'File option is supported only on Windows'
}

$outputFile = "$($templateFile.jsonFilePath).env"
$outputFile = "$templateFile.env"

$environmentText = $deploymentOutputs | ConvertTo-Json;
$bytes = ([System.Text.Encoding]::UTF8).GetBytes($environmentText)
Expand Down Expand Up @@ -576,15 +574,15 @@ try {
}
}

$postDeploymentScript = $templateFile.originalFilePath | Split-Path | Join-Path -ChildPath 'test-resources-post.ps1'
$postDeploymentScript = $templateFile | Split-Path | Join-Path -ChildPath 'test-resources-post.ps1'
if (Test-Path $postDeploymentScript) {
Log "Invoking post-deployment script '$postDeploymentScript'"
&$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters
}

if ($templateFile.jsonFilePath.EndsWith('.compiled.json')) {
Write-Verbose "Removing compiled bicep file $($templateFile.jsonFilePath)"
Remove-Item $templateFile.jsonFilePath
if ($templateFile.EndsWith('.compiled.json')) {
Write-Verbose "Removing compiled bicep file $templateFile"
Remove-Item $templateFile
}
}

Expand Down