Skip to content

Fix: Ensure azd up -e deploys resources to the specified environment, ignoring the default environment #4561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 21, 2025
Merged
9 changes: 9 additions & 0 deletions cli/azd/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ func registerCommonDependencies(container *ioc.NestedContainer) {
envValue = ""
}

if envValue == "" {
// If no explicit environment flag was set, but one was provided
// in the context, use that instead.
// This is used in workflow execution (in `up`) to influence the environment used.
if envFlag, ok := cmd.Context().Value(envFlagCtxKey).(internal.EnvFlag); ok {
return envFlag
}
}

return internal.EnvFlag{EnvironmentName: envValue}
})

Expand Down
4 changes: 4 additions & 0 deletions cli/azd/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func (u *upAction) Run(ctx context.Context) (*actions.ActionResult, error) {
u.console.Message(ctx, output.WithGrayFormat("Note: Running custom 'up' workflow from azure.yaml"))
}

if u.flags.EnvironmentName != "" {
ctx = context.WithValue(ctx, envFlagCtxKey, u.flags.EnvFlag)
}

if err := u.workflowRunner.Run(ctx, upWorkflow); err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions cli/azd/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,9 @@ func openWithDefaultBrowser(ctx context.Context, console input.Console, url stri
console.Message(ctx, fmt.Sprintf("Azd was unable to open the next url. Please try it manually: %s", url))
}

type envFlagKey string

// envFlagCtxKey is the context key for internal.EnvFlag
var envFlagCtxKey envFlagKey = "envFlag"

const referenceDocumentationUrl = "https://learn.microsoft.com/azure/developer/azure-developer-cli/reference#"
67 changes: 67 additions & 0 deletions cli/azd/test/functional/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/test/azdcli"
"github.com/azure/azure-dev/cli/azd/test/recording"
"github.com/joho/godotenv"
Expand Down Expand Up @@ -505,6 +506,72 @@ func Test_CLI_Up_ResourceGroupScope(t *testing.T) {
require.NoError(t, err)
}

// Test_CLI_Up_EnvironmentFlags validates that the up workflow handles the --environment/-e flag
// correctly.
func Test_CLI_Up_EnvironmentFlags(t *testing.T) {
ctx, cancel := newTestContext(t)
defer cancel()

dir := tempDirWithDiagnostics(t)
t.Logf("DIR: %s", dir)

cli := azdcli.NewCLI(t)
cli.WorkingDirectory = dir

// set up basic project
err := copySample(dir, "storage")
require.NoError(t, err, "failed expanding sample")

// create environments
envNew(ctx, t, cli, "env1", false)
envNew(ctx, t, cli, "env2", false)

// select env1 as default
envSelect(ctx, t, cli, "env1")

// update up workflow
err = os.WriteFile(filepath.Join(dir, "azure.yaml"), []byte(`
name: up-envflag
workflows:
up:
steps:
- azd: env set CURRENT_ENV VALUE
- azd: env set ENV1 VALUE1 -e env1
- azd: env set ENV2 VALUE2 -e env2
`), osutil.PermissionFile)
require.NoError(t, err)

// run with env1 selected as implicitly default
_, err = cli.RunCommand(ctx, "up")
require.NoError(t, err)

values := envGetValues(ctx, t, cli, "-e", "env1")
require.Contains(t, values, "ENV1")
require.Contains(t, values, "CURRENT_ENV")
require.NotContains(t, values, "ENV2")

values = envGetValues(ctx, t, cli, "-e", "env2")
require.Contains(t, values, "ENV2")
require.NotContains(t, values, "CURRENT_ENV")
require.NotContains(t, values, "ENV1")

// run with env2 selected explicitly,
// this should newly set CURRENT_ENV on env2
_, err = cli.RunCommand(ctx, "up", "-e", "env2")
require.NoError(t, err)

values = envGetValues(ctx, t, cli, "-e", "env1")
require.Contains(t, values, "ENV1")
require.Contains(t, values, "CURRENT_ENV")
require.NotContains(t, values, "ENV2")

values = envGetValues(ctx, t, cli, "-e", "env2")
require.Contains(t, values, "ENV2")
require.Contains(t, values, "CURRENT_ENV")
require.NotContains(t, values, "ENV1")

}

type httpClient interface {
Get(url string) (*http.Response, error)
}
Expand Down
Loading