fix: SWA deployment fails with BadRequest "default" environment name — add configurable environment support#8588
fix: SWA deployment fails with BadRequest "default" environment name — add configurable environment support#8588Copilot wants to merge 3 commits into
Conversation
… error Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
|
@copilot - You did a quick fix. You need to go deeper an analyze swa deployment mechanics and create a better fix that allows us to specify environments when needed and default to the right ones. |
Working on a deeper fix. The proper solution adds a services:
web:
host: staticwebapp
staticwebapp:
environment: staging # optional; omit for production
The |
Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates azd’s Azure Static Web Apps (SWA) deployment flow to avoid the newly-invalid explicit "default" environment name and adds azure.yaml support for targeting named preview environments.
Changes:
- SWA CLI deploy now conditionally includes
--envonly when a non-empty environment name is provided (production deploys omit--env). azure.yamlgains astaticwebapp.environmentservice option, and the SWA target uses it consistently for deploy + REST API polling/endpoints.- Tests and docs are updated to cover/describe the new behavior.
Show a summary per file
| File | Description |
|---|---|
| docs/reference/azure-yaml-schema.md | Documents new staticwebapp.environment service option and examples. |
| cli/azd/pkg/tools/swa/swa.go | Omits --env for production (empty environment) and preserves named environment support. |
| cli/azd/pkg/tools/swa/swa_test.go | Updates deploy arg assertions and adds coverage for named environments. |
| cli/azd/pkg/project/service_target_staticwebapp.go | Introduces StaticWebAppOptions + API environment mapping and threads configured environment through deploy/endpoints/verification. |
| cli/azd/pkg/project/service_target_staticwebapp_test.go | Adds unit tests for environment mapping and YAML parsing behavior. |
| cli/azd/pkg/project/service_config.go | Adds staticwebapp options to ServiceConfig for YAML configuration. |
Copilot's findings
- Files reviewed: 6/6 changed files
- Comments generated: 6
| // apiEnvironmentName returns the environment identifier to use for Azure REST API | ||
| // calls against the Static Web Apps service. The production environment is identified | ||
| // as "default" in the API; named preview environments use their configured name. | ||
| func (o *StaticWebAppOptions) apiEnvironmentName() string { | ||
| if o.Environment != "" { | ||
| return o.Environment | ||
| } | ||
| return DefaultStaticWebAppEnvironmentName | ||
| } |
| _, err = at.swa.Deploy(ctx, | ||
| cwd, | ||
| at.env.GetTenantId(), | ||
| targetResource.SubscriptionId(), | ||
| targetResource.ResourceGroupName(), | ||
| targetResource.ResourceName(), | ||
| DefaultStaticWebAppEnvironmentName, | ||
| serviceConfig.StaticWebApp.Environment, | ||
| *deploymentToken, |
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/pkg/azapi" | ||
| "github.com/azure/azure-dev/cli/azd/pkg/environment" | ||
| "github.com/stretchr/testify/require" | ||
| "gopkg.in/yaml.v3" | ||
| ) |
| } | ||
| } | ||
|
|
||
| func TestStaticWebAppOptions_YamlRoundTrip(t *testing.T) { |
| require.Equal(t, "staging", svc.StaticWebApp.Environment) | ||
| } | ||
|
|
||
| func TestStaticWebAppOptions_YamlRoundTripNoEnvironment(t *testing.T) { |
|
|
||
| | Property | Type | Description | | ||
| |---|---|---| | ||
| | `environment` | string | The named SWA preview environment to deploy to. When omitted (the default), azd deploys to the **production** environment by omitting `--env` from the SWA CLI command. Set to a custom name (e.g. `"staging"`) to target a named preview environment. | |
As of June 9th 2026, the Azure Static Web Apps service rejects
"default"as an explicit environment name, breaking all SWA deployments withBadRequest: The environment name "default" is invalid. For production deployments the--envflag must be omitted entirely. This fix also adds proper support for deploying to named preview environments viaazure.yamlconfiguration.Changes
pkg/tools/swa/swa.go: Make--envconditional — only appended whenenvironmentis non-empty. Empty string = production deploy (no--envflag); non-empty = named preview environment (--env <name>).pkg/project/service_target_staticwebapp.go: AddStaticWebAppOptionsstruct with anEnvironmentfield and anapiEnvironmentName()helper that maps the configured value to the correct Azure REST API build identifier ("default"for production, the configured name for preview environments).Deploy(),Endpoints(), andverifyDeployment()all use the configured environment consistently.pkg/project/service_config.go: AddStaticWebApp StaticWebAppOptionsfield (YAML keystaticwebapp) toServiceConfig, following the same pattern asK8s AksOptionsandDocker DockerProjectOptions.pkg/tools/swa/swa_test.go: Updated tests to assert--envis absent for empty environment, and addedTest_SwaDeploy_WithEnvironmentto confirm non-empty environment names still emit the flag.pkg/project/service_target_staticwebapp_test.go: AddedTestStaticWebAppOptions_ApiEnvironmentName,TestStaticWebAppOptions_YamlRoundTrip, andTestStaticWebAppOptions_YamlRoundTripNoEnvironmenttests.docs/reference/azure-yaml-schema.md: Documented the newstaticwebappservice property with production and preview environment examples.Before / After
Configuring environments in
azure.yamlRCA: Why tests didn't catch this
Tests mock the command runner and only assert the CLI args shape — they never invoke the real SWA CLI or hit the service. A service-side validation change is invisible to unit tests that stop at the process boundary.