Skip to content

Instantly share code, notes, and snippets.

@coryb
Created June 16, 2023 00:08
Show Gist options
  • Save coryb/6f342926ed3bdc2a3abc2bd11c1eb16e to your computer and use it in GitHub Desktop.
Save coryb/6f342926ed3bdc2a3abc2bd11c1eb16e to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
mrand "math/rand"
"os"
"time"
"github.com/moby/buildkit/client"
_ "github.com/moby/buildkit/client/connhelper/dockercontainer"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/util/progress/progresswriter"
"golang.org/x/sync/errgroup"
)
func main() {
ctx := context.Background()
eg, ctx := errgroup.WithContext(ctx)
for i := 0; i < 10; i++ {
eg.Go(func() error {
for {
if err := solve(ctx); err != nil {
return err
}
}
})
}
err := eg.Wait()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: % +v", err)
}
}
func solve(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
c, err := client.New(ctx, os.Getenv("BUILDKIT_HOST"))
if err != nil {
return err
}
defer c.Close()
pw, err := progresswriter.NewPrinter(context.Background(), os.Stdout, "plain")
if err != nil {
return err
}
defer func() {
<-pw.Done()
}()
dir, err := os.MkdirTemp("", "context")
if err != nil {
return err
}
defer os.RemoveAll(dir)
runOpts := []llb.RunOption{
llb.Args([]string{"touch", "/foobar"}),
}
if mrand.Intn(2) == 1 {
runOpts = append(runOpts, llb.IgnoreCache)
}
st := llb.Image(
"busybox:latest", llb.LinuxAmd64,
).Run(runOpts...).Root().File(
llb.Copy(llb.Local("context"), "/", "/stuff"),
)
def, err := st.Marshal(ctx)
if err != nil {
return err
}
_, err = c.Solve(ctx, def, client.SolveOpt{
LocalDirs: map[string]string{"context": dir},
}, pw.Status())
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment