Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/azd/pkg/ai/python_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (b *pythonBridge) RequiredExternalTools(ctx context.Context) []tools.Extern
// Run executes the specified python script with the given arguments
func (b *pythonBridge) Run(ctx context.Context, scriptName ScriptPath, args ...string) (*exec.RunResult, error) {
allArgs := append([]string{string(scriptName)}, args...)
return b.pythonCli.Run(ctx, b.workingDir, ".venv", allArgs...)
return b.pythonCli.Run(ctx, b.workingDir, ".venv", nil, allArgs...)
}

// initPython initializes the Python environment
Expand All @@ -104,7 +104,7 @@ func (b *pythonBridge) initPython(ctx context.Context) error {
return err
}

if err := b.pythonCli.InstallRequirements(ctx, targetDir, ".venv", "requirements.txt"); err != nil {
if err := b.pythonCli.InstallRequirements(ctx, targetDir, ".venv", "requirements.txt", nil); err != nil {
return err
}

Expand Down
17 changes: 15 additions & 2 deletions cli/azd/pkg/project/container_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,24 @@ func (ch *ContainerHelper) Build(

// Include full environment variables for the docker build including:
// 1. Environment variables from the host
// 2. Environment variables from the service configuration
// 3. Environment variables from the docker configuration
// 2. Environment variables from the azd environment
// 3. Environment variables from the service configuration (azure.yaml env)
// 4. Environment variables from the docker configuration
dockerEnv := []string{}
dockerEnv = append(dockerEnv, os.Environ()...)
dockerEnv = append(dockerEnv, env.Environ()...)

// Expand and add service-level environment variables from azure.yaml
if len(serviceConfig.Environment) > 0 {
expandedServiceEnv, err := serviceConfig.Environment.Expand(env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}
for key, value := range expandedServiceEnv {
dockerEnv = append(dockerEnv, fmt.Sprintf("%s=%s", key, value))
}
}

dockerEnv = append(dockerEnv, dockerOptions.BuildEnv...)

// Build the container
Expand Down
23 changes: 20 additions & 3 deletions cli/azd/pkg/project/framework_service_dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ func (dp *dotnetProject) Restore(
return nil, err
}

if err := dp.dotnetCli.Restore(ctx, projFile); err != nil {
env, err := serviceConfig.ExpandEnv(dp.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := dp.dotnetCli.Restore(ctx, projFile, env); err != nil {
return nil, err
}

Expand Down Expand Up @@ -123,7 +128,13 @@ func (dp *dotnetProject) Build(
if err != nil {
return nil, err
}
if err := dp.dotnetCli.Build(ctx, projFile, defaultDotNetBuildConfiguration, ""); err != nil {

env, err := serviceConfig.ExpandEnv(dp.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := dp.dotnetCli.Build(ctx, projFile, defaultDotNetBuildConfiguration, "", env); err != nil {
return nil, err
}

Expand Down Expand Up @@ -189,7 +200,13 @@ func (dp *dotnetProject) Package(
if err != nil {
return nil, err
}
if err := dp.dotnetCli.Publish(ctx, projFile, defaultDotNetBuildConfiguration, packageDest); err != nil {

env, err := serviceConfig.ExpandEnv(dp.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := dp.dotnetCli.Publish(ctx, projFile, defaultDotNetBuildConfiguration, packageDest, env); err != nil {
return nil, err
}

Expand Down
24 changes: 21 additions & 3 deletions cli/azd/pkg/project/framework_service_maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ func (m *mavenProject) Restore(
progress *async.Progress[ServiceProgress],
) (*ServiceRestoreResult, error) {
progress.SetProgress(NewServiceProgress("Resolving maven dependencies"))
if err := m.mavenCli.ResolveDependencies(ctx, serviceConfig.Path()); err != nil {

env, err := serviceConfig.ExpandEnv(m.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := m.mavenCli.ResolveDependencies(ctx, serviceConfig.Path(), env); err != nil {
return nil, fmt.Errorf("resolving maven dependencies: %w", err)
}

Expand Down Expand Up @@ -98,7 +104,13 @@ func (m *mavenProject) Build(
progress *async.Progress[ServiceProgress],
) (*ServiceBuildResult, error) {
progress.SetProgress(NewServiceProgress("Compiling maven project"))
if err := m.mavenCli.Compile(ctx, serviceConfig.Path()); err != nil {

env, err := serviceConfig.ExpandEnv(m.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := m.mavenCli.Compile(ctx, serviceConfig.Path(), env); err != nil {
return nil, err
}
// Create build artifact for maven compile output
Expand All @@ -125,7 +137,13 @@ func (m *mavenProject) Package(
progress *async.Progress[ServiceProgress],
) (*ServicePackageResult, error) {
progress.SetProgress(NewServiceProgress("Packaging maven project"))
if err := m.mavenCli.Package(ctx, serviceConfig.Path()); err != nil {

env, err := serviceConfig.ExpandEnv(m.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := m.mavenCli.Package(ctx, serviceConfig.Path(), env); err != nil {
return nil, err
}

Expand Down
26 changes: 23 additions & 3 deletions cli/azd/pkg/project/framework_service_npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ func (np *npmProject) Restore(
progress *async.Progress[ServiceProgress],
) (*ServiceRestoreResult, error) {
progress.SetProgress(NewServiceProgress("Installing NPM dependencies"))
if err := np.cli.Install(ctx, serviceConfig.Path()); err != nil {

// Expand service environment variables
envVars, err := serviceConfig.ExpandEnv(np.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := np.cli.Install(ctx, serviceConfig.Path(), envVars); err != nil {
return nil, err
}

Expand Down Expand Up @@ -87,7 +94,14 @@ func (np *npmProject) Build(
// Exec custom `build` script if available
// If `build`` script is not defined in the package.json the NPM script will NOT fail
progress.SetProgress(NewServiceProgress("Running NPM build script"))
if err := np.cli.RunScript(ctx, serviceConfig.Path(), "build"); err != nil {

// Expand service environment variables
envVars, err := serviceConfig.ExpandEnv(np.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

if err := np.cli.RunScript(ctx, serviceConfig.Path(), "build", envVars); err != nil {
return nil, err
}

Expand Down Expand Up @@ -122,10 +136,16 @@ func (np *npmProject) Package(
) (*ServicePackageResult, error) {
progress.SetProgress(NewServiceProgress("Running NPM package script"))

// Expand service environment variables
envVars, err := serviceConfig.ExpandEnv(np.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

// Long term this script we call should better align with our inner-loop scenarios
// Keeping this defaulted to `build` will create confusion for users when we start to support
// both local dev / debug builds and production bundled builds
if err := np.cli.RunScript(ctx, serviceConfig.Path(), "build"); err != nil {
if err := np.cli.RunScript(ctx, serviceConfig.Path(), "build", envVars); err != nil {
return nil, err
}

Expand Down
8 changes: 7 additions & 1 deletion cli/azd/pkg/project/framework_service_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ func (pp *pythonProject) Restore(
}
}

// Expand service environment variables
envVars, err := serviceConfig.ExpandEnv(pp.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

progress.SetProgress(NewServiceProgress("Installing Python PIP dependencies"))
err = pp.cli.InstallRequirements(ctx, serviceConfig.Path(), vEnvName, "requirements.txt")
err = pp.cli.InstallRequirements(ctx, serviceConfig.Path(), vEnvName, "requirements.txt", envVars)
if err != nil {
return nil, fmt.Errorf("requirements for project '%s' could not be installed: %w", serviceConfig.Path(), err)
}
Expand Down
9 changes: 8 additions & 1 deletion cli/azd/pkg/project/framework_service_swa.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package project

import (
"context"
"fmt"

"github.com/azure/azure-dev/cli/azd/pkg/async"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
Expand Down Expand Up @@ -96,16 +97,22 @@ func (p *swaProject) Build(
serviceContext *ServiceContext,
_ *async.Progress[ServiceProgress],
) (*ServiceBuildResult, error) {
env, err := serviceConfig.ExpandEnv(p.env.Getenv)
if err != nil {
return nil, fmt.Errorf("expanding service environment variables: %w", err)
}

previewerWriter := p.console.ShowPreviewer(ctx,
&input.ShowPreviewerOptions{
Prefix: " ",
MaxLineCount: 8,
Title: "Build SWA Project",
})
err := p.swa.Build(
err = p.swa.Build(
ctx,
serviceConfig.Path(),
previewerWriter,
env,
)
p.console.StopPreviewer(ctx, false)

Expand Down
24 changes: 24 additions & 0 deletions cli/azd/pkg/project/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package project

import (
"fmt"
"os"
"path/filepath"

"github.com/azure/azure-dev/cli/azd/pkg/apphost"
Expand Down Expand Up @@ -89,3 +91,25 @@ func (sc *ServiceConfig) Path() string {
}
return filepath.Join(sc.Project.Path, sc.RelativePath)
}

// ExpandEnv expands the service-level environment variables defined in azure.yaml
// using the provided lookup function and merges them with the current OS environment.
// Service-defined variables take precedence over OS environment variables.
// Returns nil if no service environment variables are configured.
func (sc *ServiceConfig) ExpandEnv(lookup func(string) string) ([]string, error) {
if len(sc.Environment) == 0 {
return nil, nil
}

expanded, err := sc.Environment.Expand(lookup)
if err != nil {
return nil, err
}

env := os.Environ()
for key, value := range expanded {
env = append(env, fmt.Sprintf("%s=%s", key, value))
}

return env, nil
}
Loading
Loading