Skip to content

fix issue 13750 condition BUILD_RG_NAME and AzureLocation#13796

Open
v-GeorgyPuzakov wants to merge 4 commits intoactions:mainfrom
v-GeorgyPuzakov:13750
Open

fix issue 13750 condition BUILD_RG_NAME and AzureLocation#13796
v-GeorgyPuzakov wants to merge 4 commits intoactions:mainfrom
v-GeorgyPuzakov:13750

Conversation

@v-GeorgyPuzakov
Copy link

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

  • Related issue / work item is attached
  • Tests are written (if applicable)
  • Documentation is updated (if applicable)
  • Changes are tested and related VM images are successfully generated

Copilot AI review requested due to automatic review settings March 12, 2026 11:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 AzureLocation parameter non-mandatory and adds mutual exclusivity validation with BUILD_RG_NAME
  • Branches Packer validate/build commands to omit location when using BUILD_RG_NAME
  • Updates documentation to describe the BUILD_RG_NAME usage 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

Comment on lines +217 to +220
if ($UseExistingBuildResourceGroup -eq $UseAzureLocation) {
throw "Specify exactly one value: AzureLocation or BUILD_RG_NAME."
}

Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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."
}

Copilot uses AI. Check for mistakes.
Comment on lines 279 to 314
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
}

Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines +297 to +313
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
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants