build: add support for named contexts

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2021-12-15 21:02:31 -08:00
parent fe9f9bba87
commit de6b04d726
2 changed files with 53 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/containerd/containerd/images"
@@ -79,6 +80,7 @@ type Inputs struct {
InStream io.Reader
ContextState *llb.State
DockerfileInline string
NamedContexts map[string]string
}
type DriverInfo struct {
@@ -1080,6 +1082,27 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
target.FrontendAttrs["filename"] = dockerfileName
for k, v := range inp.NamedContexts {
target.FrontendAttrs["frontend.caps"] = "moby.buildkit.frontend.contexts+forward"
if urlutil.IsGitURL(v) || urlutil.IsURL(v) || strings.HasPrefix(v, "docker-image://") {
target.FrontendAttrs["context:"+k] = v
continue
}
st, err := os.Stat(v)
if err != nil {
return nil, errors.Wrapf(err, "failed to get context %v", k)
}
if !st.IsDir() {
return nil, errors.Wrapf(syscall.ENOTDIR, "failed to get context path %v", v)
}
localName := k
if k == "context" || k == "dockerfile" {
localName = "_" + k // underscore to avoid collisions
}
target.LocalDirs[localName] = v
target.FrontendAttrs["context:"+k] = "local:" + localName
}
release := func() {
for _, dir := range toRemove {
os.RemoveAll(dir)