From b0db6e13875776a2bed50c4f7601a504451af6f8 Mon Sep 17 00:00:00 2001 From: kpenfound Date: Thu, 3 Nov 2022 14:08:32 -0400 Subject: [PATCH 1/2] update multiarch example concurrency Signed-off-by: kpenfound --- templates/go/multiarch/main.go | 88 +++++++++++++--------------------- 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/templates/go/multiarch/main.go b/templates/go/multiarch/main.go index e58ec9f..612abec 100644 --- a/templates/go/multiarch/main.go +++ b/templates/go/multiarch/main.go @@ -4,92 +4,68 @@ import ( "context" "fmt" "os" - "path/filepath" "dagger.io/dagger" - "golang.org/x/sync/errgroup" ) func main() { - repo := "https://github.com/kpenfound/greetings-api.git" // Default repo to build - if len(os.Args) > 1 { // Optionally pass in a git repo as a command line argument - repo = os.Args[1] + if len(os.Args) < 2 { + fmt.Println("Must pass in a Git repository to build") + os.Exit(1) } - if err := build(repo); err != nil { + repo := os.Args[1] + if err := build(context.Background(), repo); err != nil { fmt.Println(err) } - filepath.Walk("build", func(name string, info os.FileInfo, err error) error { - if !info.IsDir() { - fmt.Println(name) - } - return nil - }) } -func build(repoUrl string) error { - ctx := context.Background() - g, ctx := errgroup.WithContext(ctx) +func build(ctx context.Context, repoURL string) error { + fmt.Printf("Building %s\n", repoURL) - // Our build matrix + // define build matrix oses := []string{"linux", "darwin"} arches := []string{"amd64", "arm64"} goVersions := []string{"1.18", "1.19"} - // create a Dagger client + // initialize Dagger client client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) if err != nil { return err } defer client.Close() - // clone the specified git repo - repo := client.Git(repoUrl) + // clone repository with Dagger + repo := client.Git(repoURL) src := repo.Branch("main").Tree() + outputDirectory := client.Directory() + for _, version := range goVersions { - // Get golang image and mount go source + // get `golang` image for specified Go version imageTag := fmt.Sprintf("golang:%s", version) - golang := client.Container().From(imageTag) - golang = golang.WithMountedDirectory("/src", src).WithWorkdir("/src") + golang := client.Container(). + From(imageTag). + WithMountedDirectory("/src", src). + WithWorkdir("/src") - // Run matrix builds in parallel for _, goos := range oses { for _, goarch := range arches { - goos, goarch, version := goos, goarch, version // closures - g.Go(func() error { - return buildOsArch(ctx, golang, goos, goarch, version) - }) - } - } - } - if err := g.Wait(); err != nil { - return err - } - return nil -} + // create a directory for each os, arch and version + path := fmt.Sprintf("build/%s/%s/%s/", version, goos, goarch) -func buildOsArch(ctx context.Context, builder *dagger.Container, goos string, goarch string, version string) error { - fmt.Printf("Building %s %s with go %s\n", goos, goarch, version) + // set GOARCH and GOOS in the build environment + build := golang.WithEnvVariable("GOOS", goos). + WithEnvVariable("GOARCH", goarch). + Exec(dagger.ContainerExecOpts{ + Args: []string{"go", "build", "-o", path}, + }) - // Create the output path for the build - path := fmt.Sprintf("build/%s/%s/%s/", version, goos, goarch) - outpath := filepath.Join(".", path) - err := os.MkdirAll(outpath, os.ModePerm) - if err != nil { - return err + // build application + output := build.Directory(path) + outputDirectory = outputDirectory.WithDirectory(path, output) + } + } } - - // Set GOARCH and GOOS and build - build := builder.WithEnvVariable("GOOS", goos) - build = build.WithEnvVariable("GOARCH", goarch) - build = build.Exec(dagger.ContainerExecOpts{ - Args: []string{"go", "build", "-o", path}, - }) - - // Get build output from builder - output := build.Directory(path) - - // Write the build output to the host - _, err = output.Export(ctx, path) + _, err = outputDirectory.Export(ctx, ".") return err } From b109bf32ed9bcdf270ea23d9af57fd8a7345cbf5 Mon Sep 17 00:00:00 2001 From: kpenfound Date: Thu, 3 Nov 2022 14:12:08 -0400 Subject: [PATCH 2/2] simplfy output Signed-off-by: kpenfound --- templates/go/multiarch/main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/templates/go/multiarch/main.go b/templates/go/multiarch/main.go index 612abec..89337d6 100644 --- a/templates/go/multiarch/main.go +++ b/templates/go/multiarch/main.go @@ -61,8 +61,7 @@ func build(ctx context.Context, repoURL string) error { }) // build application - output := build.Directory(path) - outputDirectory = outputDirectory.WithDirectory(path, output) + outputDirectory = outputDirectory.WithDirectory(path, build.Directory(path)) } } }