Last active
November 13, 2022 18:38
-
-
Save jpadams/2a18c70c6e4a555325caf594e21fe031 to your computer and use it in GitHub Desktop.
Call for client twice. Works. Implicit client close between function calls to build and list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"os" | |
"time" | |
"dagger.io/dagger" | |
) | |
func main() { | |
if err := build(context.Background()); err != nil { | |
fmt.Println(err) | |
} | |
if err := list(context.Background()); err != nil { | |
fmt.Println(err) | |
} | |
} | |
func list(ctx context.Context) error { | |
// initialize Dagger client | |
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) | |
if err != nil { | |
return err | |
} | |
defer client.Close() | |
// get reference to the local project | |
src := client.Host().Workdir() | |
alpine := client.Container().From("alpine") | |
alpine = alpine.WithMountedDirectory("/src", src).WithWorkdir("/src") | |
// ensure `ls` exec is never cached | |
epoch := fmt.Sprintf("%d", time.Now().Unix()) | |
alpine = alpine.WithEnvVariable("CACHEBUSTER", epoch) | |
out, err := alpine.Exec(dagger.ContainerExecOpts{ | |
Args: []string{"ls", "-alh", "build"}, | |
}).Stdout().Contents(ctx) | |
if err != nil { | |
return err | |
} | |
fmt.Printf(out) | |
return nil | |
} | |
func build(ctx context.Context) error { | |
fmt.Println("Building with Dagger") | |
// define build matrix | |
oses := []string{"darwin"} | |
arches := []string{"arm64"} | |
goVersions := []string{"1.19"} | |
// initialize Dagger client | |
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) | |
if err != nil { | |
return err | |
} | |
defer client.Close() | |
// get reference to the local project | |
src := client.Host().Workdir() | |
// create empty directory to put build outputs | |
outputs := client.Directory() | |
for _, version := range goVersions { | |
// get `golang` image for specified Go version | |
imageTag := fmt.Sprintf("golang:%s", version) | |
golang := client.Container().From(imageTag) | |
// mount cloned repository into `golang` image | |
golang = golang.WithMountedDirectory("/src", src).WithWorkdir("/src") | |
for _, goos := range oses { | |
for _, goarch := range arches { | |
// create a directory for each os, arch and version | |
path := fmt.Sprintf("build/%s/%s/%s/", version, goos, goarch) | |
// set GOARCH and GOOS in the build environment | |
build := golang.WithEnvVariable("GOOS", goos) | |
build = build.WithEnvVariable("GOARCH", goarch) | |
// build application | |
build = build.Exec(dagger.ContainerExecOpts{ | |
Args: []string{"go", "build", "-o", path}, | |
}) | |
// get reference to build output directory in container | |
outputs = outputs.WithDirectory(path, build.Directory(path)) | |
} | |
} | |
} | |
// write build artifacts to host | |
_, err = outputs.Export(ctx, ".") | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment