/
mod.go
26 lines (20 loc) · 866 Bytes
/
mod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package build
import (
"path/filepath"
"github.com/moby/buildkit/client/llb"
)
var GoImageRef = "golang:1.20"
func Mod(modSource llb.State, name, p, target string) llb.State {
buildOut := filepath.Join("/tmp/output", target)
res := llb.Image(GoImageRef).
File(llb.Mkdir("/opt/build", 0755, llb.WithParents(true))).
Run(
llb.AddMount("/root/.cache/go-build", llb.Scratch(), llb.AsPersistentCacheDir("go-build-cache", llb.CacheMountShared)),
llb.AddMount("/go/pkg/mod", llb.Scratch(), llb.AsPersistentCacheDir("go-mod-cache", llb.CacheMountShared)),
llb.AddMount("/opt/build", modSource),
llb.AddEnv("CGO_ENABLED", "0"),
llb.Dir("/opt/build"),
llb.Args([]string{"/bin/sh", "-c", "/usr/local/go/bin/go build -o " + buildOut + " " + p}),
).Root()
return llb.Scratch().File(llb.Copy(res, buildOut, target, createParentsCopyOption{}))
}