Skip to content

Commit 95d50f2

Browse files
committed
feat(image): support git remote context
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 990161d commit 95d50f2

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

image/build.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"os"
89
"path/filepath"
910
"time"
1011

1112
"github.com/cenkalti/backoff/v4"
13+
"github.com/docker/docker/builder/remotecontext/git"
14+
"github.com/docker/docker/builder/remotecontext/urlutil"
1215
"github.com/moby/go-archive"
1316
"github.com/moby/go-archive/compression"
1417
"github.com/moby/term"
@@ -23,13 +26,25 @@ import (
2326
// This function is useful for creating a build context to build an image.
2427
// The dockerfile path needs to be relative to the build context.
2528
func ArchiveBuildContext(dir string, dockerfile string) (r io.ReadCloser, err error) {
26-
// always pass context as absolute path
27-
abs, err := filepath.Abs(dir)
28-
if err != nil {
29-
return nil, fmt.Errorf("absolute path: %w", err)
29+
switch {
30+
case isLocalDir(dir):
31+
// always pass context as absolute path
32+
dir, err = filepath.Abs(dir)
33+
if err != nil {
34+
return nil, fmt.Errorf("absolute path: %w", err)
35+
}
36+
case urlutil.IsGitURL(dir):
37+
clone, err := git.Clone(dir)
38+
if err != nil {
39+
return nil, fmt.Errorf("unable to clone git build context: %w", err)
40+
}
41+
dir = clone
42+
// TODO case urlutil.IsURL(dir):
43+
default:
44+
return nil, fmt.Errorf("unable to prepare context: path %q not found", dir)
3045
}
3146

32-
dockerIgnoreExists, excluded, err := ParseDockerIgnore(abs)
47+
dockerIgnoreExists, excluded, err := ParseDockerIgnore(dir)
3348
if err != nil {
3449
return nil, fmt.Errorf("parse docker ignore: %w", err)
3550
}
@@ -41,7 +56,7 @@ func ArchiveBuildContext(dir string, dockerfile string) (r io.ReadCloser, err er
4156
}
4257

4358
buildContext, err := archive.TarWithOptions(
44-
abs,
59+
dir,
4560
&archive.TarOptions{
4661
ExcludePatterns: excluded,
4762
IncludeFiles: includes,
@@ -55,6 +70,11 @@ func ArchiveBuildContext(dir string, dockerfile string) (r io.ReadCloser, err er
5570
return buildContext, nil
5671
}
5772

73+
func isLocalDir(c string) bool {
74+
_, err := os.Stat(c)
75+
return err == nil
76+
}
77+
5878
// ImageBuildClient is a client that can build images.
5979
type ImageBuildClient interface {
6080
ImageClient

image/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/klauspost/compress v1.18.0 // indirect
4141
github.com/moby/docker-image-spec v1.3.1 // indirect
4242
github.com/moby/sys/sequential v0.6.0 // indirect
43+
github.com/moby/sys/symlink v0.3.0 // indirect
4344
github.com/moby/sys/user v0.4.0 // indirect
4445
github.com/moby/sys/userns v0.1.0 // indirect
4546
github.com/morikuni/aec v1.0.0 // indirect

image/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w
6262
github.com/moby/sys/atomicwriter v0.1.0/go.mod h1:Ul8oqv2ZMNHOceF643P6FKPXeCmYtlQMvpizfsSoaWs=
6363
github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7zgvCU=
6464
github.com/moby/sys/sequential v0.6.0/go.mod h1:uyv8EUTrca5PnDsdMGXhZe6CCe8U/UiTWd+lL+7b/Ko=
65+
github.com/moby/sys/symlink v0.3.0 h1:GZX89mEZ9u53f97npBy4Rc3vJKj7JBDj/PN2I22GrNU=
66+
github.com/moby/sys/symlink v0.3.0/go.mod h1:3eNdhduHmYPcgsJtZXW1W4XUJdZGBIkttZ8xKqPUJq0=
6567
github.com/moby/sys/user v0.4.0 h1:jhcMKit7SA80hivmFJcbB1vqmw//wU61Zdui2eQXuMs=
6668
github.com/moby/sys/user v0.4.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
6769
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=

0 commit comments

Comments
 (0)