From 5030e075a9853c9ebc5081023309c8a02b06a4e5 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Sun, 27 Feb 2022 11:48:58 +0000 Subject: [PATCH] dockerfile,e2e: Use buildx and cache mounts for faster rebuilds in E2E --- .dockerignore | 1 + Dockerfile | 30 +++++++++++++++------------ test/e2e/e2e_test.go | 7 ++++--- testing/docker.go | 48 +++++++++++++++++++++++++++++++++----------- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/.dockerignore b/.dockerignore index f75df5ce..2ac045ef 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,3 +11,4 @@ charts *.md *.txt *.sh +test/e2e/.docker-build diff --git a/Dockerfile b/Dockerfile index 73a7f9ac..7e68a355 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,22 +8,26 @@ WORKDIR /workspace ENV GO111MODULE=on \ CGO_ENABLED=0 -# Copy the Go Modules manifests -COPY go.mod go.sum ./ +# # Copy the Go Modules manifests +# COPY go.mod go.sum ./ -# cache deps before building and copying source so that we don't need to re-download as much -# and so that source changes don't invalidate our downloaded layer -RUN go mod download +# # cache deps before building and copying source so that we don't need to re-download as much +# # and so that source changes don't invalidate our downloaded layer +# RUN --mount=type=cache,target=/go/pkg/mod go mod download # Copy the go source -COPY . . +# COPY . . + +ARG TARGETOS +ARG TARGETARCH # Build -RUN export GOOS=$(echo ${TARGETPLATFORM} | cut -d / -f1) && \ - export GOARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) && \ - GOARM=$(echo ${TARGETPLATFORM} | cut -d / -f3 | cut -c2-) && \ - go build -a -o manager main.go && \ - go build -a -o github-webhook-server ./cmd/githubwebhookserver +RUN --mount=target=. \ + --mount=type=cache,mode=0777,target=/root/.cache/go-build \ + --mount=type=cache,mode=0777,target=/go/pkg/mod\ + GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ + GOARM=$(echo ${TARGETPLATFORM} | cut -d / -f3 | cut -c2-) \ + go build -o /out/manager main.go && go build -o /out/github-webhook-server ./cmd/githubwebhookserver # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details @@ -31,8 +35,8 @@ FROM gcr.io/distroless/static:nonroot WORKDIR / -COPY --from=builder /workspace/manager . -COPY --from=builder /workspace/github-webhook-server . +COPY --from=builder /out/manager . +COPY --from=builder /out/github-webhook-server . USER nonroot:nonroot diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 57e6aa71..db3753b4 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -30,9 +30,10 @@ var ( builds = []testing.DockerBuild{ { - Dockerfile: "../../Dockerfile", - Args: []testing.BuildArg{}, - Image: controllerImage, + Dockerfile: "../../Dockerfile", + Args: []testing.BuildArg{}, + Image: controllerImage, + EnableBuildX: true, }, { Dockerfile: "../../runner/Dockerfile", diff --git a/testing/docker.go b/testing/docker.go index dddab3b9..cb5533dc 100644 --- a/testing/docker.go +++ b/testing/docker.go @@ -3,8 +3,11 @@ package testing import ( "context" "fmt" + "log" + "os" "os/exec" "path/filepath" + "strings" "github.com/actions-runner-controller/actions-runner-controller/testing/runtime" ) @@ -14,9 +17,10 @@ type Docker struct { } type DockerBuild struct { - Dockerfile string - Args []BuildArg - Image ContainerImage + Dockerfile string + Args []BuildArg + Image ContainerImage + EnableBuildX bool } type BuildArg struct { @@ -25,12 +29,7 @@ type BuildArg struct { func (k *Docker) Build(ctx context.Context, builds []DockerBuild) error { for _, build := range builds { - var args []string - args = append(args, "--build-arg=TARGETPLATFORM="+"linux/amd64") - for _, buildArg := range build.Args { - args = append(args, "--build-arg="+buildArg.Name+"="+buildArg.Value) - } - _, err := k.CombinedOutput(k.dockerBuildCmd(ctx, build.Dockerfile, build.Image.Repo, build.Image.Tag, args)) + _, err := k.dockerBuildCombinedOutput(ctx, build) if err != nil { return fmt.Errorf("failed building %v: %w", build, err) @@ -40,10 +39,35 @@ func (k *Docker) Build(ctx context.Context, builds []DockerBuild) error { return nil } -func (k *Docker) dockerBuildCmd(ctx context.Context, dockerfile, repo, tag string, args []string) *exec.Cmd { +func (k *Docker) dockerBuildCombinedOutput(ctx context.Context, build DockerBuild) (string, error) { + var args []string + + args = append(args, "--build-arg=TARGETPLATFORM="+"linux/amd64") + for _, buildArg := range build.Args { + args = append(args, "--build-arg="+buildArg.Name+"="+buildArg.Value) + } + + dockerfile := build.Dockerfile + repo := build.Image.Repo + tag := build.Image.Tag + buildContext := filepath.Dir(dockerfile) + + docker := "docker" + env := os.Environ() args = append([]string{"build", "--tag", repo + ":" + tag, "-f", dockerfile, buildContext}, args...) - cmd := exec.CommandContext(ctx, "docker", args...) - return cmd + if build.EnableBuildX { + args = append([]string{"buildx"}, args...) + args = append(args, "--load") + + env = append(env, "DOCKER_BUILDKIT=1") + } + + cmd := exec.CommandContext(ctx, docker, args...) + cmd.Env = env + + log.Printf("%s %s", docker, strings.Join(args, " ")) + + return k.CombinedOutput(cmd) }