mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
Merge pull request #2038 from crazy-max/localstate-group
build: support local state group
This commit is contained in:
@ -26,7 +26,6 @@ import (
|
||||
"github.com/distribution/reference"
|
||||
"github.com/docker/buildx/builder"
|
||||
"github.com/docker/buildx/driver"
|
||||
"github.com/docker/buildx/localstate"
|
||||
"github.com/docker/buildx/util/desktop"
|
||||
"github.com/docker/buildx/util/dockerutil"
|
||||
"github.com/docker/buildx/util/imagetools"
|
||||
@ -72,6 +71,7 @@ const (
|
||||
type Options struct {
|
||||
Inputs Inputs
|
||||
|
||||
Ref string
|
||||
Allow []entitlements.Entitlement
|
||||
Attests map[string]*string
|
||||
BuildArgs map[string]string
|
||||
@ -91,12 +91,11 @@ type Options struct {
|
||||
Target string
|
||||
Ulimits *opts.UlimitOpt
|
||||
|
||||
Session []session.Attachable
|
||||
|
||||
// Linked marks this target as exclusively linked (not requested by the user).
|
||||
Linked bool
|
||||
Session []session.Attachable
|
||||
Linked bool // Linked marks this target as exclusively linked (not requested by the user).
|
||||
PrintFunc *PrintFunc
|
||||
SourcePolicy *spb.Policy
|
||||
GroupRef string
|
||||
}
|
||||
|
||||
type PrintFunc struct {
|
||||
@ -424,6 +423,7 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
|
||||
}
|
||||
|
||||
so := client.SolveOpt{
|
||||
Ref: opt.Ref,
|
||||
Frontend: "dockerfile.v0",
|
||||
FrontendAttrs: map[string]string{},
|
||||
LocalDirs: map[string]string{},
|
||||
@ -433,6 +433,10 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
|
||||
SourcePolicy: opt.SourcePolicy,
|
||||
}
|
||||
|
||||
if so.Ref == "" {
|
||||
so.Ref = identity.NewID()
|
||||
}
|
||||
|
||||
if opt.CgroupParent != "" {
|
||||
so.FrontendAttrs["cgroup-parent"] = opt.CgroupParent
|
||||
}
|
||||
@ -668,12 +672,6 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
|
||||
so.FrontendAttrs["ulimit"] = ulimits
|
||||
}
|
||||
|
||||
// remember local state like directory path that is not sent to buildkit
|
||||
so.Ref = identity.NewID()
|
||||
if err := saveLocalState(so, opt, node, configDir); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return &so, releaseF, nil
|
||||
}
|
||||
|
||||
@ -751,6 +749,9 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := saveLocalState(so, k, opt, node, configDir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range gitattrs {
|
||||
so.FrontendAttrs[k] = v
|
||||
}
|
||||
@ -1645,43 +1646,6 @@ func noPrintFunc(opt map[string]Options) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func saveLocalState(so client.SolveOpt, opt Options, node builder.Node, configDir string) error {
|
||||
var err error
|
||||
|
||||
if so.Ref == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
lp := opt.Inputs.ContextPath
|
||||
dp := opt.Inputs.DockerfilePath
|
||||
if lp != "" || dp != "" {
|
||||
if lp != "" {
|
||||
lp, err = filepath.Abs(lp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if dp != "" {
|
||||
dp, err = filepath.Abs(dp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ls, err := localstate.New(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ls.SaveRef(node.Builder, node.Name, so.Ref, localstate.State{
|
||||
LocalPath: lp,
|
||||
DockerfilePath: dp,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadSourcePolicy reads a source policy from a file.
|
||||
// The file path is taken from EXPERIMENTAL_BUILDKIT_SOURCE_POLICY env var.
|
||||
// if the env var is not set, this `returns nil, nil`
|
||||
|
43
build/localstate.go
Normal file
43
build/localstate.go
Normal file
@ -0,0 +1,43 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/docker/buildx/builder"
|
||||
"github.com/docker/buildx/localstate"
|
||||
"github.com/moby/buildkit/client"
|
||||
)
|
||||
|
||||
func saveLocalState(so *client.SolveOpt, target string, opts Options, node builder.Node, configDir string) error {
|
||||
var err error
|
||||
if so.Ref == "" {
|
||||
return nil
|
||||
}
|
||||
lp := opts.Inputs.ContextPath
|
||||
dp := opts.Inputs.DockerfilePath
|
||||
if lp != "" || dp != "" {
|
||||
if lp != "" {
|
||||
lp, err = filepath.Abs(lp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if dp != "" {
|
||||
dp, err = filepath.Abs(dp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
l, err := localstate.New(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.SaveRef(node.Builder, node.Name, so.Ref, localstate.State{
|
||||
Target: target,
|
||||
LocalPath: lp,
|
||||
DockerfilePath: dp,
|
||||
GroupRef: opts.GroupRef,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user