Skip to content

Instantly share code, notes, and snippets.

@jpadams
Last active November 13, 2022 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpadams/71f542bcd774573799729196771f6622 to your computer and use it in GitHub Desktop.
Save jpadams/71f542bcd774573799729196771f6622 to your computer and use it in GitHub Desktop.
Call for src twice, same client. Doesn't Work! Seems to use original snapshot of host workdir without exported build directory. To test ensure you've remove ./build/ before running.
package main
import (
"context"
"fmt"
"os"
"time"
"dagger.io/dagger"
)
func main() {
if err := build(context.Background()); err != nil {
fmt.Println(err)
}
}
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, ".")
// get reference to the local project again
src2 := client.Host().Workdir()
alpine := client.Container().From("alpine")
alpine = alpine.WithMountedDirectory("/src", src2).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)
fmt.Printf(out)
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