Skip to content

Commit

Permalink
azd init adds checks for dockerfile args
Browse files Browse the repository at this point in the history
  • Loading branch information
Menghua1 committed Dec 18, 2024
1 parent d3faad6 commit 37d759f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cli/azd/internal/appdetect/appdetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"

"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/pkg/tools/dotnet"
"github.com/bmatcuk/doublestar/v4"
)
Expand Down Expand Up @@ -167,8 +168,9 @@ type Port struct {
}

type Docker struct {
Path string
Ports []Port
Path string
Ports []Port
BuildArgs []osutil.ExpandableString
}

type projectDetector interface {
Expand Down
49 changes: 48 additions & 1 deletion cli/azd/internal/repository/app_init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repository

import (
"bufio"
"context"
"fmt"
"maps"
Expand Down Expand Up @@ -44,6 +45,34 @@ var dbMap = map[appdetect.DatabaseDep]struct{}{

var featureCompose = alpha.MustFeatureKey("compose")

// parseDockerfileForArgs parses a Dockerfile to extract ARG instructions and returns them as ExpandableString values.
func parseDockerfileForArgs(dockerfilePath string) ([]osutil.ExpandableString, error) {
var buildArgs []osutil.ExpandableString

file, err := os.Open(dockerfilePath)
if err != nil {
return nil, fmt.Errorf("failed to open Dockerfile: %w", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "ARG ") {
argLine := strings.TrimPrefix(line, "ARG ")
if len(argLine) > 0 {
buildArgs = append(buildArgs, osutil.NewExpandableString(argLine))
}
}
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading Dockerfile: %w", err)
}

return buildArgs, nil
}

// InitFromApp initializes the infra directory and project file from the current existing app.
func (i *Initializer) InitFromApp(
ctx context.Context,
Expand Down Expand Up @@ -264,6 +293,23 @@ func (i *Initializer) InitFromApp(
}
}

for idx := range detect.Services {
servicePath := detect.Services[idx].Path
dockerfilePath := filepath.Join(servicePath, "Dockerfile")

if _, err := os.Stat(dockerfilePath); err == nil {

buildArgs, err := parseDockerfileForArgs(dockerfilePath)
if err != nil {
return fmt.Errorf("failed to parse Dockerfile ARGs at %s: %w", dockerfilePath, err)
}

if len(buildArgs) > 0 {
detect.Services[idx].Docker.BuildArgs = buildArgs
}
}
}

tracing.SetUsageAttributes(fields.AppInitLastStep.String("generate"))

title = "Generating " + output.WithHighLightFormat("./"+azdcontext.ProjectFileName)
Expand Down Expand Up @@ -544,7 +590,8 @@ func ServiceFromDetect(
}

svc.Docker = project.DockerProjectOptions{
Path: relDocker,
Path: relDocker,
BuildArgs: prj.Docker.BuildArgs,
}
}

Expand Down
13 changes: 12 additions & 1 deletion cli/azd/internal/repository/app_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/internal/appdetect"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/pkg/project"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -93,7 +94,13 @@ func TestInitializer_prjConfigFromDetect(t *testing.T) {
{
Language: appdetect.DotNet,
Path: "dotnet",
Docker: &appdetect.Docker{Path: "Dockerfile"},
Docker: &appdetect.Docker{
Path: "Dockerfile",
BuildArgs: []osutil.ExpandableString{
osutil.NewExpandableString("ARG1"),
osutil.NewExpandableString("ARG2"),
},
},
},
},
},
Expand All @@ -112,6 +119,10 @@ func TestInitializer_prjConfigFromDetect(t *testing.T) {
RelativePath: "dotnet",
Docker: project.DockerProjectOptions{
Path: "Dockerfile",
BuildArgs: []osutil.ExpandableString{
osutil.NewExpandableString("ARG1"),
osutil.NewExpandableString("ARG2"),
},
},
},
},
Expand Down

0 comments on commit 37d759f

Please sign in to comment.