Skip to content

Commit 2df4b6a

Browse files
committed
Fix build script to capture parameter output
1 parent 1379b42 commit 2df4b6a

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

build.ps1

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ param (
1010
$Configuration = "Debug",
1111

1212
[string]
13-
$ResourceGroupName = 'rg-pub-dbt-extension',
13+
$resourceGroupName = 'rg-pub-dbt-extension',
1414

1515
[string]
16-
$Location = 'westeurope',
16+
$location = 'westeurope',
1717

1818
[string]
19-
$ContainerRegistryName = "acrdbtextension",
19+
$containerRegistryName = "acrdbtextension",
2020

2121
[string]
22-
$DatabricksWorkspaceName = 'dbt-pub-dbt-extension',
22+
$databricksWorkspaceName = 'dbt-pub-dbt-extension',
2323

2424
[switch]
2525
$Bootstrap,
@@ -122,14 +122,77 @@ $projectPath = getProjectPath $ProjectName
122122

123123
if ($Bootstrap.IsPresent)
124124
{
125-
$result = New-AzDeployment -TemplateFile (Join-Path $PSScriptRoot 'infrastructure' 'main.bicep') `
126-
-TemplateParameterFile (Join-Path $PSScriptRoot 'infrastructure' 'main.bicepparam.json') `
127-
-Location $Location `
125+
$bicepFile = Join-Path $PSScriptRoot 'infrastructure' 'main.bicep'
126+
$bicepParameterFile = Join-Path $PSScriptRoot 'infrastructure' 'main.bicepparam'
127+
128+
# Go through existing to see if user provided different parameters
129+
$currentParameters = $PSBoundParameters
130+
if ($currentParameters.ContainsKey('ResourceGroupName') -or $currentParameters.ContainsKey('Location') -or $currentParameters.ContainsKey('ContainerRegistryName') -or $currentParameters.ContainsKey('DatabricksWorkspaceName')){
131+
$bicepExe = testBicepExe
132+
if ($bicepExe) {
133+
$randomFileName = [System.IO.Path]::GetRandomFileName()
134+
$fileExtension = [System.IO.Path]::GetExtension($randomFileName)
135+
$randomFileName = $randomFileName.Replace($fileExtension, '.json')
136+
$tempJsonPath = Join-Path $env:TEMP $randomFileName
137+
138+
# Using outfile instead of stdout which has large outpus
139+
& $bicepExe build-params (Join-Path $PSScriptRoot 'infrastructure' 'main.bicepparam') `
140+
--outfile $tempJsonPath
141+
142+
$existingParameters = Get-Content -Path $tempJsonPath -Raw | ConvertFrom-Json -AsHashtable
143+
144+
# Set new content
145+
$setContent = $false
146+
foreach ($key in $currentParameters.Keys) {
147+
Write-Verbose "Processing parameter '$key' with value '$($currentParameters[$key])'"
148+
if ($existingParameters.parameters.ContainsKey($key)) {
149+
Write-Verbose -Message "Parameter '$key' already exists with value '$($existingParameters.parameters[$key].value)'"
150+
if ($existingParameters.parameters[$key].value -ne $currentParameters[$key]) {
151+
$setContent = $true
152+
Write-Verbose "Updating parameter '$key' from '$($existingParameters.parameters[$key].value)' to '$($currentParameters[$key])'"
153+
$existingParameters.parameters[$key].value = $currentParameters[$key]
154+
}
155+
}
156+
}
157+
158+
if ($setContent) {
159+
Write-Verbose "Updating parameters in '$tempJsonPath'"
160+
Set-Content -Path $tempJsonPath -Value ($existingParameters | ConvertTo-Json -Depth 10) -Encoding UTF8 -Force
161+
162+
$randomFileName = [System.IO.Path]::GetRandomFileName()
163+
$fileExtension = [System.IO.Path]::GetExtension($randomFileName)
164+
$randomFileName = $randomFileName.Replace($fileExtension, '.bicepparam')
165+
$tempBicepParameterFile = Join-Path (Split-Path $bicepParameterFile -Parent) $randomFileName
166+
& $bicepExe decompile-params $tempJsonPath `
167+
--outfile $tempBicepParameterFile `
168+
--bicep-file $bicepFile
169+
Write-Verbose "Decompiled parameters to '$tempBicepParameterFile'"
170+
171+
# Reset bicepParameterFile to the new file
172+
$bicepParameterFile = $tempBicepParameterFile
173+
}
174+
}
175+
176+
}
177+
178+
$params = @{
179+
TemplateFile = $bicepFile
180+
TemplateParameterFile = $bicepParameterFile
181+
Location = $location
182+
}
183+
184+
Write-Verbose "Deploying Bicep template with parameters:" -Verbose
185+
Write-Verbose ($params | ConvertTo-Json | Out-String) -Verbose
186+
$result = New-AzDeployment @params
128187

129188
$environment = @{
130189
ContainerRegistryUrl = ([System.String]::Concat($result.Outputs.containerLoginServer.value, '.azurecr.io'))
131190
WorkspaceUrl = ('https://' + $result.Outputs.dbtWorkspaceUrl.value)
132191
}
192+
193+
if ($setContent) {
194+
Remove-Item -Path $tempBicepParameterFile -Force -ErrorAction Ignore
195+
}
133196
}
134197

135198
if ($Clean.IsPresent)

infrastructure/main.bicep

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
targetScope = 'subscription'
22

3-
param rgName string
3+
param resourceGroupName string
44
param location string
55
param storageName string
66
param containerRegistryName string
77
param workspaceName string
88

99
module rg 'br/public:avm/res/resources/resource-group:0.4.1' = {
10-
name: '${rgName}-${uniqueString(deployment().name, location)}'
10+
name: '${resourceGroupName}-${uniqueString(deployment().name, location)}'
1111
params: {
12-
name: rgName
12+
name: resourceGroupName
1313
location: location
1414
}
1515
}
1616

1717
module st 'br/public:avm/res/storage/storage-account:0.26.0' = {
18-
scope: resourceGroup(rgName)
18+
scope: resourceGroup(resourceGroupName)
1919
name: '${uniqueString(deployment().name, location)}-${storageName}'
2020
params: {
2121
name: storageName
@@ -41,8 +41,8 @@ module st 'br/public:avm/res/storage/storage-account:0.26.0' = {
4141
}
4242

4343
module registry 'br/public:avm/res/container-registry/registry:0.9.1' = {
44-
name: '${rgName}-${uniqueString(deployment().name, location)}-registry'
45-
scope: resourceGroup(rgName)
44+
name: '${resourceGroupName}-${uniqueString(deployment().name, location)}-registry'
45+
scope: resourceGroup(resourceGroupName)
4646
params: {
4747
name: containerRegistryName
4848
location: location
@@ -54,7 +54,7 @@ module registry 'br/public:avm/res/container-registry/registry:0.9.1' = {
5454
}
5555

5656
module dbt 'br/public:avm/res/databricks/workspace:0.11.2' = {
57-
scope: resourceGroup(rgName)
57+
scope: resourceGroup(resourceGroupName)
5858
params: {
5959
name: workspaceName
6060
location: location

infrastructure/main.bicepparam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using 'main.bicep'
22

3-
param rgName = 'rg-pub-dbt-extension'
3+
param resourceGroupName = 'rg-pub-dbt-extension'
44
param location = 'westeurope'
55
param storageName = 'bicepdatabricksstorage'
66
param containerRegistryName = 'acrdbtextension'

0 commit comments

Comments
 (0)