Add --subscription and --location flags to provision and up commands#6777
Conversation
There was a problem hiding this comment.
Pull request overview
Adds inline --subscription and --location flags to azd provision and azd up, persisting the provided values into the selected environment early so provisioning can proceed non-interactively (per issue #6774).
Changes:
- Extended
ProvisionFlagsto include--subscription/--locationand surfaced getters for cross-package access. - Persisted provided subscription/location values to the environment before provisioning initialization in both
provisionandup. - Updated CLI usage snapshots to show the new flags in help output.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| cli/azd/internal/cmd/provision.go | Adds flags + persists values into .env before provisioning begins. |
| cli/azd/cmd/up.go | Persists values into .env before provisioning manager init / EnsureSubscriptionAndLocation. |
| cli/azd/cmd/testdata/TestUsage-azd-up.snap | Updates azd up help output snapshots to include new flags. |
| cli/azd/cmd/testdata/TestUsage-azd-provision.snap | Updates azd provision help output snapshots to include new flags. |
Comments suppressed due to low confidence (2)
cli/azd/internal/cmd/provision.go:66
- Both
--subscriptionand--locationdescriptions currently say "for the new environment", butazd provisiontypically operates on an existing selected environment. Consider rewording these help strings to avoid implying the command always creates a new environment.
local.StringVarP(&i.location, "location", "l", "", "Azure location for the new environment")
cli/azd/internal/cmd/provision.go:225
- New behavior persists
--subscription/--locationinto the environment before provisioning runs. There are existing functional tests forup/provision; please add a test that runs with these flags and assertsAZURE_SUBSCRIPTION_ID/AZURE_LOCATIONare written to the selected environment (and that no interactive prompt is required).
// Apply --subscription and --location flags to the environment before provisioning
if p.flags.subscription != "" {
p.env.SetSubscriptionId(p.flags.subscription)
if err := p.envManager.Save(ctx, p.env); err != nil {
return nil, fmt.Errorf("saving subscription id: %w", err)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Great catch! Added a guard that returns an error if you try to change the subscription or location for an environment that already has them set. The error message directs the user to create a new environment instead. Same value = no-op (allowed), empty value = first time setting (allowed), different value = error with clear guidance. |
Fixes Azure#6774 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Change --subscription flag help text from 'Name or ID' to 'ID' since no name-to-ID resolution is performed - Consolidate separate Save calls into a single Save after setting both subscription and location values in provision.go and up.go - Update snapshot test data to reflect the description change Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Return an error when --subscription or --location flags attempt to change values that are already set in the environment. This prevents accidentally leaking previously provisioned resources by re-provisioning in a different subscription or location. Same value = no-op (allowed), empty value = first time setting (allowed), different value = error with clear guidance to create a new environment. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
8f27b63 to
1f3368e
Compare
Azure Dev CLI Install InstructionsInstall scriptsMacOS/Linux
bash: pwsh: WindowsPowerShell install MSI install Standalone Binary
MSI
Documentationlearn.microsoft.com documentationtitle: Azure Developer CLI reference
|
Summary
Adds
--subscriptionand--locationinline flags toazd provisionandazd upcommands. This allows users to specify the Azure subscription and location directly on the command line, skipping the interactive prompts.Fixes #6774
Changes
cli/azd/internal/cmd/provision.go: Addedsubscriptionandlocationfields toProvisionFlags, registered them inBindNonCommon(), and added logic inProvisionAction.Run()to apply flag values to the environment before provisioning begins.cli/azd/cmd/up.go: Added logic inupAction.Run()to apply subscription/location flag values to the environment before the provisioning manager initializes (and beforeEnsureSubscriptionAndLocationis called).TestUsage-azd-provision.snapandTestUsage-azd-up.snapto reflect the new flags in help output.Usage
Design
Follows the existing pattern from
azd env newwhich already supports these flags. Flag values are applied to the environment viaenv.SetSubscriptionId()/env.SetLocation()+envManager.Save()before the provisioning manager initializes, soEnsureSubscriptionAndLocationsees the values and skips prompting. Fully backward-compatible - existing workflows are unchanged.