-
-
Save coryb/60e5470ee2f4b534ad2538b8b3dfb55e to your computer and use it in GitHub Desktop.
This file contains 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" | |
mrand "math/rand" | |
"os" | |
"path/filepath" | |
"time" | |
"github.com/moby/buildkit/client" | |
_ "github.com/moby/buildkit/client/connhelper/dockercontainer" | |
"github.com/moby/buildkit/frontend/dockerfile/builder" | |
gwclient "github.com/moby/buildkit/frontend/gateway/client" | |
"github.com/moby/buildkit/util/progress/progresswriter" | |
"github.com/pkg/errors" | |
"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 := build(ctx); err != nil { | |
return err | |
} | |
} | |
}) | |
} | |
err := eg.Wait() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "ERROR: % +v", err) | |
} | |
} | |
func build(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) | |
if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte(` | |
FROM busybox:latest | |
RUN touch /foobar | |
COPY . /stuff | |
`), 0o644); err != nil { | |
return err | |
} | |
// prevent /moby.buildkit.v1.frontend.LLBBridge/ReadFile errors when | |
// dockerignore files don't exist | |
if err := os.WriteFile(filepath.Join(dir, "Dockerfile.dockerignore"), []byte(``), 0o644); err != nil { | |
return err | |
} | |
if err := os.WriteFile(filepath.Join(dir, ".dockerignore"), []byte(``), 0o644); err != nil { | |
return err | |
} | |
localDirs := map[string]string{ | |
"context": dir, | |
"dockerfile": dir, | |
} | |
frontendAttrs := map[string]string{ | |
"context": "context", | |
"filename": "Dockerfile", | |
} | |
if mrand.Intn(2) == 1 { | |
frontendAttrs["no-cache"] = "" | |
} | |
solveOpt := client.SolveOpt{ | |
LocalDirs: localDirs, | |
FrontendAttrs: frontendAttrs, | |
} | |
_, err = c.Build(ctx, solveOpt, "buildkit-test", func(ctx context.Context, gw gwclient.Client) (*gwclient.Result, error) { | |
buildRes, err := builder.Build(ctx, gw) | |
if err != nil { | |
return nil, errors.WithStack(err) | |
} | |
ref, err := buildRes.SingleRef() | |
if err != nil { | |
return nil, errors.WithStack(err) | |
} | |
res := gwclient.NewResult() | |
res.SetRef(ref) | |
return res, nil | |
}, pw.Status()) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment