Skip to content

Commit ff97fe1

Browse files
authored
Merge pull request moby#5390 from crazy-max/ci-mount-docker-config
hack: mount docker config on gha
2 parents 31896c7 + 33088c1 commit ff97fe1

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

.github/CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ CGO_ENABLED=1 GOBUILDFLAGS="-race" ./hack/test integration
164164
Set `TEST_KEEP_CACHE=1` for the test framework to keep external dependant images in a docker volume
165165
if you are repeatedly calling `./hack/test` script. This helps to avoid rate limiting on the remote registry side.
166166

167+
You can also set `MOUNT_BUILDKIT_DOCKER_CONFIG_PATH` to forward docker config that will be used to pull
168+
test images into the container. Don't use your personal docker config, create a new one with a dedicated
169+
token that only has public read-only access.
170+
167171
If you are working behind a proxy, you can set some of or all
168172
`HTTP_PROXY=http://ip:port`, `HTTPS_PROXY=http://ip:port`, `NO_PROXY=http://ip:port` for the test framework
169173
to specify the proxy build args.

hack/shell

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@ if [ -n "$MOUNT_BUILDKIT_SOURCE" ]; then
2020
volumes="-v $(pwd):/src"
2121
fi
2222

23+
config=
24+
if [ -n "$MOUNT_BUILDKIT_DOCKER_CONFIG_PATH" ]; then
25+
if [ -f "$MOUNT_BUILDKIT_DOCKER_CONFIG_PATH" ]; then
26+
config="-v $MOUNT_BUILDKIT_DOCKER_CONFIG_PATH:/root/.docker/config.json:ro"
27+
fi
28+
fi
29+
2330
set -x
24-
docker run $SSH $volumes -it --privileged -v /tmp -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry --rm $(cat $iidfile) ash
31+
docker run $SSH $volumes $config -it --privileged -v /tmp -e BUILDKIT_REGISTRY_MIRROR_DIR=/root/.cache/registry --rm $(cat $iidfile) ash

hack/test

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set -eu -o pipefail
2929
: "${DOCKERFILE_RELEASES=}"
3030
: "${BUILDKIT_WORKER_RANDOM=}"
3131
: "${BUILDKIT_TEST_DISABLE_FEATURES=}"
32+
: "${MOUNT_BUILDKIT_DOCKER_CONFIG_PATH=}"
3233

3334
if [ "$TEST_DOCKERD" == "1" ]; then
3435
if [ ! -f "$TEST_DOCKERD_BINARY" ]; then
@@ -107,7 +108,18 @@ if [ "$TEST_KEEP_CACHE" != "1" ]; then
107108
trap 'docker rm -v $cacheVolume' EXIT
108109
fi
109110

110-
baseCreateFlags="--rm --privileged \
111+
dockerConfigMount=""
112+
if [ "$GITHUB_ACTIONS" = "true" ] || [ -n "$MOUNT_BUILDKIT_DOCKER_CONFIG_PATH" ]; then
113+
dockerConfigPath="$HOME/.docker/config.json"
114+
if [ -n "$MOUNT_BUILDKIT_DOCKER_CONFIG_PATH" ]; then
115+
dockerConfigPath="$MOUNT_BUILDKIT_DOCKER_CONFIG_PATH"
116+
fi
117+
if [ -f "$dockerConfigPath" ]; then
118+
dockerConfigMount="-v $dockerConfigPath:/root/.docker/config.json:ro"
119+
fi
120+
fi
121+
122+
baseCreateFlags="--rm --privileged $dockerConfigMount \
111123
-v /tmp $testReportsVol \
112124
--volumes-from=$cacheVolume \
113125
-e CGO_ENABLED \

util/contentutil/refs.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,41 @@ import (
1616
"github.com/pkg/errors"
1717
)
1818

19-
func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error) {
19+
type ResolveOpt struct {
20+
Credentials func(string) (string, string, error)
21+
}
22+
23+
type ResolveOptFunc func(*ResolveOpt)
24+
25+
func WithCredentials(c func(string) (string, string, error)) ResolveOptFunc {
26+
return func(o *ResolveOpt) {
27+
o.Credentials = func(host string) (string, string, error) {
28+
if host == "registry-1.docker.io" {
29+
host = "https://index.docker.io/v1/"
30+
}
31+
return c(host)
32+
}
33+
}
34+
}
35+
36+
func ProviderFromRef(ref string, opts ...ResolveOptFunc) (ocispecs.Descriptor, content.Provider, error) {
2037
headers := http.Header{}
2138
headers.Set("User-Agent", version.UserAgent())
22-
remote := docker.NewResolver(docker.ResolverOptions{
39+
40+
var ro ResolveOpt
41+
for _, f := range opts {
42+
f(&ro)
43+
}
44+
45+
dro := docker.ResolverOptions{
2346
Headers: headers,
24-
})
47+
}
48+
if ro.Credentials != nil {
49+
dro.Hosts = docker.ConfigureDefaultRegistries(
50+
docker.WithAuthorizer(docker.NewDockerAuthorizer(docker.WithAuthCreds(ro.Credentials))),
51+
)
52+
}
53+
remote := docker.NewResolver(dro)
2554

2655
name, desc, err := remote.Resolve(context.TODO(), ref)
2756
if err != nil {

util/testutil/integration/run.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/containerd/containerd/v2/core/content"
2121
"github.com/containerd/containerd/v2/core/remotes/docker"
22+
"github.com/docker/cli/cli/config"
2223
"github.com/gofrs/flock"
2324
"github.com/moby/buildkit/util/appcontext"
2425
"github.com/moby/buildkit/util/contentutil"
@@ -257,7 +258,16 @@ func copyImagesLocal(t *testing.T, host string, images map[string]string) error
257258
defer closer()
258259
}
259260
} else {
260-
desc, provider, err = contentutil.ProviderFromRef(from)
261+
dockerConfig := config.LoadDefaultConfigFile(os.Stderr)
262+
263+
desc, provider, err = contentutil.ProviderFromRef(from, contentutil.WithCredentials(
264+
func(host string) (string, string, error) {
265+
ac, err := dockerConfig.GetAuthConfig(host)
266+
if err != nil {
267+
return "", "", err
268+
}
269+
return ac.Username, ac.Password, nil
270+
}))
261271
if err != nil {
262272
return err
263273
}

0 commit comments

Comments
 (0)