climpr / deploy-bicep

GNU General Public License v3.0
0 stars 1 forks source link

[bug] unable to find deployment scope #5

Open ewlidd opened 4 days ago

ewlidd commented 4 days ago

All workflow runs fail using this action with: Exception: [Resolve-TemplateDeploymentScope()] Target scope is resourceGroup, but resourceGroupName property is not present in deploymentConfig.json file

Get-Content -Raw collects the file content as a singular string, instead of an array of lines: https://github.com/climpr/deploy-bicep/blob/008dbaaf4c56908385d1a14a114e9bc1437c3318/src/support-functions.psm1#L195

The subsequent -match here: https://github.com/climpr/deploy-bicep/blob/008dbaaf4c56908385d1a14a114e9bc1437c3318/src/support-functions.psm1#L201 automatically iterates over an array, returning matches, but since $templateFileContent is no longer an array of lines but a singular string it attempts to match the entire template file. It fails to find targetScope in a valid bicep.main file.

ewlidd commented 4 days ago

Now that I've attempted to write a fix, the pester tests fail https://github.com/climpr/deploy-bicep/actions/runs/11628599292/job/32384035199#step:3:111

Message RuntimeException: Cannot index into a null array. at Resolve-TemplateDeploymentScope, /home/runner/work/deploy-bicep/deploy-bicep/src/support-functions.psm1:203 at <ScriptBlock>, /home/runner/work/deploy-bicep/deploy-bicep/src/Resolve-DeploymentConfig.ps1:64 at <ScriptBlock>, /home/runner/work/deploy-bicep/deploy-bicep/src/tests/Resolve-DeploymentConfig.tests.ps1:22

it appears that $Matches is not being populated after a successful match. This could also be why the above issue described fails. It might be matching successfully but failing to index $Matches at https://github.com/climpr/deploy-bicep/blob/008dbaaf4c56908385d1a14a114e9bc1437c3318/src/support-functions.psm1#L204

ewlidd commented 4 days ago

Demonstrating $Matches not working on a positive match:

PS /home/ewlidd> $templateFileContent = Get-Content -Path ~/Desktop/test.bicep
PS /home/ewlidd> $templateFileContent                                                                                     
/////////////////
// Global Parameters
/////////////////
targetScope = 'subscription'
PS /home/ewlidd> $regex = "^(?:\s)*?targetScope(?:\s)*?=(?:\s)*?(?:['\s])+?(resourceGroup|subscription|managementGroup|tenant)(?:['\s])+?"
PS /home/ewlidd> $null -eq $Matches
True
PS /home/ewlidd> if ($templateFileContent -match $regex){write-output "true - $Matches"}
true - 
PS /home/ewlidd> $null -eq $Matches
True
PS /home/ewlidd> if ("testing" -match '^testing$'){write-output "true - $Matches"}
true - System.Collections.Hashtable
PS /home/ewlidd> $Matches | fl *
Name  : 0
Key   : 0
Value : testing

PS /home/ewlidd> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Arch Linux
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0