Skip to content

fix: SWA deployment fails with BadRequest "default" environment name — add configurable environment support#8588

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-deploy-static-web-app
Open

fix: SWA deployment fails with BadRequest "default" environment name — add configurable environment support#8588
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-deploy-static-web-app

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

As of June 9th 2026, the Azure Static Web Apps service rejects "default" as an explicit environment name, breaking all SWA deployments with BadRequest: The environment name "default" is invalid. For production deployments the --env flag must be omitted entirely. This fix also adds proper support for deploying to named preview environments via azure.yaml configuration.

Changes

  • pkg/tools/swa/swa.go: Make --env conditional — only appended when environment is non-empty. Empty string = production deploy (no --env flag); non-empty = named preview environment (--env <name>).
  • pkg/project/service_target_staticwebapp.go: Add StaticWebAppOptions struct with an Environment field and an apiEnvironmentName() 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(), and verifyDeployment() all use the configured environment consistently.
  • pkg/project/service_config.go: Add StaticWebApp StaticWebAppOptions field (YAML key staticwebapp) to ServiceConfig, following the same pattern as K8s AksOptions and Docker DockerProjectOptions.
  • pkg/tools/swa/swa_test.go: Updated tests to assert --env is absent for empty environment, and added Test_SwaDeploy_WithEnvironment to confirm non-empty environment names still emit the flag.
  • pkg/project/service_target_staticwebapp_test.go: Added TestStaticWebAppOptions_ApiEnvironmentName, TestStaticWebAppOptions_YamlRoundTrip, and TestStaticWebAppOptions_YamlRoundTripNoEnvironment tests.
  • docs/reference/azure-yaml-schema.md: Documented the new staticwebapp service property with production and preview environment examples.

Before / After

# Before (rejected by SWA service)
npx @azure/static-web-apps-cli deploy ... --env default --deployment-token ...

# After: production (default — no azure.yaml change needed for existing users)
npx @azure/static-web-apps-cli deploy ... --deployment-token ...

# After: named preview environment (opt-in via azure.yaml)
npx @azure/static-web-apps-cli deploy ... --env staging --deployment-token ...

Configuring environments in azure.yaml

# Production (default — no change needed for existing users)
services:
  web:
    host: staticwebapp

# Named preview environment
services:
  web:
    host: staticwebapp
    staticwebapp:
      environment: staging

RCA: 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.

Copilot AI linked an issue Jun 10, 2026 that may be closed by this pull request
… error

Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix deployment issue for static web app fix: omit --env flag for SWA production deployments (BadRequest regression) Jun 10, 2026
Copilot AI requested a review from jongio June 10, 2026 04:42
@jongio

jongio commented Jun 10, 2026

Copy link
Copy Markdown
Member

@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.

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

@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 StaticWebAppOptions struct to ServiceConfig (following the same pattern as AksOptions/DockerProjectOptions) so users can configure the SWA environment name in azure.yaml:

services:
  web:
    host: staticwebapp
    staticwebapp:
      environment: staging   # optional; omit for production
  • When omitted: no --env flag to the SWA CLI (production deploy) and "default" to the Azure REST API (production build ID)
  • When set: --env <name> to the SWA CLI and <name> to the REST API (preview environment)

The DefaultStaticWebAppEnvironmentName = "default" constant is retained as the REST API's production build identifier.

Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
Copilot AI changed the title fix: omit --env flag for SWA production deployments (BadRequest regression) fix: SWA deployment fails with BadRequest "default" environment name — add configurable environment support Jun 10, 2026
@jongio jongio marked this pull request as ready for review June 10, 2026 05:33
@jongio jongio requested a review from vhvb1989 as a code owner June 10, 2026 05:33
Copilot AI review requested due to automatic review settings June 10, 2026 05:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 --env only when a non-empty environment name is provided (production deploys omit --env).
  • azure.yaml gains a staticwebapp.environment service 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

Comment on lines +39 to +47
// 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
}
Comment on lines 207 to 214
_, err = at.swa.Deploy(ctx,
cwd,
at.env.GetTenantId(),
targetResource.SubscriptionId(),
targetResource.ResourceGroupName(),
targetResource.ResourceName(),
DefaultStaticWebAppEnvironmentName,
serviceConfig.StaticWebApp.Environment,
*deploymentToken,
Comment on lines 6 to 14
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. |
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.

Unable to deploy static web app

3 participants