fix issue 13750 condition BUILD_RG_NAME and AzureLocation#13796
fix issue 13750 condition BUILD_RG_NAME and AzureLocation#13796v-GeorgyPuzakov wants to merge 4 commits intoactions:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR makes AzureLocation optional when the BUILD_RG_NAME environment variable is set, allowing users to use an existing build resource group instead of specifying an Azure location for Packer image generation.
Changes:
- Makes
AzureLocationparameter non-mandatory and adds mutual exclusivity validation withBUILD_RG_NAME - Branches Packer validate/build commands to omit
locationwhen usingBUILD_RG_NAME - Updates documentation to describe the
BUILD_RG_NAMEusage pattern
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| helpers/GenerateResourcesAndImage.ps1 | Makes AzureLocation optional, adds validation logic, branches packer commands |
| docs/create-image-and-azure-resources.md | Documents BUILD_RG_NAME usage and updated AzureLocation description |
| if ($UseExistingBuildResourceGroup -eq $UseAzureLocation) { | ||
| throw "Specify exactly one value: AzureLocation or BUILD_RG_NAME." | ||
| } | ||
|
|
There was a problem hiding this comment.
The condition $UseExistingBuildResourceGroup -eq $UseAzureLocation throws when both are true OR both are false, but the error message "Specify exactly one value: AzureLocation or BUILD_RG_NAME" is misleading when both are provided. More importantly, when both are false (neither is set), it also throws — which is correct — but the logic is confusing to read. Consider using a clearer condition like if (-not $UseExistingBuildResourceGroup -and -not $UseAzureLocation) and if ($UseExistingBuildResourceGroup -and $UseAzureLocation) with distinct error messages for each case.
| if ($UseExistingBuildResourceGroup -eq $UseAzureLocation) { | |
| throw "Specify exactly one value: AzureLocation or BUILD_RG_NAME." | |
| } | |
| if (-not $UseExistingBuildResourceGroup -and -not $UseAzureLocation) { | |
| throw "Specify one value: AzureLocation or BUILD_RG_NAME." | |
| } | |
| if ($UseExistingBuildResourceGroup -and $UseAzureLocation) { | |
| throw "Specify only one value: AzureLocation or BUILD_RG_NAME." | |
| } |
| if ($UseAzureLocation) { | ||
| & $PackerBinary validate ` | ||
| "-only=$($PackerTemplate.BuildName).*" ` | ||
| "-var=client_id=fake" ` | ||
| "-var=client_secret=$($validateClientSecret)" ` | ||
| "-var=oidc_request_token=fake" ` | ||
| "-var=oidc_request_url=fake" ` | ||
| "-var=subscription_id=$($SubscriptionId)" ` | ||
| "-var=tenant_id=fake" ` | ||
| "-var=location=$($AzureLocation)" ` | ||
| "-var=image_os=$($PackerTemplate.ImageOS)" ` | ||
| "-var=managed_image_name=$($ManagedImageName)" ` | ||
| "-var=managed_image_resource_group_name=$($ResourceGroupName)" ` | ||
| "-var=install_password=$($InstallPassword)" ` | ||
| "-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` | ||
| "-var=azure_tags=$($TagsJson)" ` | ||
| $PackerTemplate.Path | ||
| } | ||
| else { | ||
| & $PackerBinary validate ` | ||
| "-only=$($PackerTemplate.BuildName).*" ` | ||
| "-var=client_id=fake" ` | ||
| "-var=client_secret=$($validateClientSecret)" ` | ||
| "-var=oidc_request_token=fake" ` | ||
| "-var=oidc_request_url=fake" ` | ||
| "-var=subscription_id=$($SubscriptionId)" ` | ||
| "-var=tenant_id=fake" ` | ||
| "-var=image_os=$($PackerTemplate.ImageOS)" ` | ||
| "-var=managed_image_name=$($ManagedImageName)" ` | ||
| "-var=managed_image_resource_group_name=$($ResourceGroupName)" ` | ||
| "-var=install_password=$($InstallPassword)" ` | ||
| "-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` | ||
| "-var=azure_tags=$($TagsJson)" ` | ||
| $PackerTemplate.Path | ||
| } | ||
|
|
There was a problem hiding this comment.
The validate and build commands are nearly identical between the if/else branches — the only difference is the presence of -var "location=$($AzureLocation)". This duplication (repeated twice: once for validate, once for build) makes maintenance error-prone. Consider building a shared argument list and conditionally appending the location variable, then splatting the array into the packer call.
| if ($UseAzureLocation) { | |
| & $PackerBinary validate ` | |
| "-only=$($PackerTemplate.BuildName).*" ` | |
| "-var=client_id=fake" ` | |
| "-var=client_secret=$($validateClientSecret)" ` | |
| "-var=oidc_request_token=fake" ` | |
| "-var=oidc_request_url=fake" ` | |
| "-var=subscription_id=$($SubscriptionId)" ` | |
| "-var=tenant_id=fake" ` | |
| "-var=location=$($AzureLocation)" ` | |
| "-var=image_os=$($PackerTemplate.ImageOS)" ` | |
| "-var=managed_image_name=$($ManagedImageName)" ` | |
| "-var=managed_image_resource_group_name=$($ResourceGroupName)" ` | |
| "-var=install_password=$($InstallPassword)" ` | |
| "-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` | |
| "-var=azure_tags=$($TagsJson)" ` | |
| $PackerTemplate.Path | |
| } | |
| else { | |
| & $PackerBinary validate ` | |
| "-only=$($PackerTemplate.BuildName).*" ` | |
| "-var=client_id=fake" ` | |
| "-var=client_secret=$($validateClientSecret)" ` | |
| "-var=oidc_request_token=fake" ` | |
| "-var=oidc_request_url=fake" ` | |
| "-var=subscription_id=$($SubscriptionId)" ` | |
| "-var=tenant_id=fake" ` | |
| "-var=image_os=$($PackerTemplate.ImageOS)" ` | |
| "-var=managed_image_name=$($ManagedImageName)" ` | |
| "-var=managed_image_resource_group_name=$($ResourceGroupName)" ` | |
| "-var=install_password=$($InstallPassword)" ` | |
| "-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` | |
| "-var=azure_tags=$($TagsJson)" ` | |
| $PackerTemplate.Path | |
| } | |
| $packerValidateArgs = @( | |
| "-only=$($PackerTemplate.BuildName).*" | |
| "-var=client_id=fake" | |
| "-var=client_secret=$($validateClientSecret)" | |
| "-var=oidc_request_token=fake" | |
| "-var=oidc_request_url=fake" | |
| "-var=subscription_id=$($SubscriptionId)" | |
| "-var=tenant_id=fake" | |
| ) | |
| if ($UseAzureLocation) { | |
| $packerValidateArgs += "-var=location=$($AzureLocation)" | |
| } | |
| $packerValidateArgs += @( | |
| "-var=image_os=$($PackerTemplate.ImageOS)" | |
| "-var=managed_image_name=$($ManagedImageName)" | |
| "-var=managed_image_resource_group_name=$($ResourceGroupName)" | |
| "-var=install_password=$($InstallPassword)" | |
| "-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" | |
| "-var=azure_tags=$($TagsJson)" | |
| $PackerTemplate.Path | |
| ) | |
| & $PackerBinary validate @packerValidateArgs |
| else { | ||
| & $PackerBinary validate ` | ||
| "-only=$($PackerTemplate.BuildName).*" ` | ||
| "-var=client_id=fake" ` | ||
| "-var=client_secret=$($validateClientSecret)" ` | ||
| "-var=oidc_request_token=fake" ` | ||
| "-var=oidc_request_url=fake" ` | ||
| "-var=subscription_id=$($SubscriptionId)" ` | ||
| "-var=tenant_id=fake" ` | ||
| "-var=image_os=$($PackerTemplate.ImageOS)" ` | ||
| "-var=managed_image_name=$($ManagedImageName)" ` | ||
| "-var=managed_image_resource_group_name=$($ResourceGroupName)" ` | ||
| "-var=install_password=$($InstallPassword)" ` | ||
| "-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` | ||
| "-var=azure_tags=$($TagsJson)" ` | ||
| $PackerTemplate.Path | ||
| } |
There was a problem hiding this comment.
When BUILD_RG_NAME is set, the code skips passing location to Packer but never passes build_resource_group_name either. Packer's Azure builder needs build_resource_group_name to use an existing resource group. Without it, Packer won't know which resource group to use for temporary build resources, and the build will likely fail or use defaults. You should pass -var "build_resource_group_name=$($BuildResourceGroupName)" in the else branches.
Description
New tool, Bug fixing, or Improvement?
Please include a summary of the change and which issue is fixed. Also include relevant motivation and context.
For new tools, please provide total size and installation time.
Related issue:
Check list