From 20bcf0559a9eae911f8765e836c9c599bebb7237 Mon Sep 17 00:00:00 2001 From: zulkhair Date: Thu, 8 Aug 2024 11:01:54 +0700 Subject: [PATCH] move tar writer close --- common/docker/docker.go | 12 ++++++++---- common/docker/script.go | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/common/docker/docker.go b/common/docker/docker.go index 1f880ba..6f140b4 100644 --- a/common/docker/docker.go +++ b/common/docker/docker.go @@ -291,9 +291,9 @@ func pullImageIfNotExists(ctx context.Context, cli *client.Client, imageName str func buildImage(ctx context.Context, cli *client.Client, dockerfile Dockerfile, imageName string) error { fmt.Println("Building image ", imageName) + buf := new(bytes.Buffer) tw := tar.NewWriter(buf) - defer tw.Close() // Add the Dockerfile to the tar archive header := &tar.Header{ @@ -312,18 +312,22 @@ func buildImage(ctx context.Context, cli *client.Client, dockerfile Dockerfile, return err } + if err := tw.Close(); err != nil { + return eris.Wrap(err, "Failed to close tar writer") + } + // Read the tar archive - tarReader := bytes.NewReader(buf.Bytes()) + buildContext := io.NopCloser(bytes.NewReader(buf.Bytes())) + defer buildContext.Close() buildOptions := types.ImageBuildOptions{ Dockerfile: "Dockerfile", Tags: []string{imageName}, Target: dockerfile.Target, - Version: types.BuilderBuildKit, } // Build the image - buildResponse, err := cli.ImageBuild(ctx, tarReader, buildOptions) + buildResponse, err := cli.ImageBuild(ctx, buildContext, buildOptions) if err != nil { return err } diff --git a/common/docker/script.go b/common/docker/script.go index 54e0c77..accdc12 100644 --- a/common/docker/script.go +++ b/common/docker/script.go @@ -20,7 +20,8 @@ const ( # Copy the rest of the source code and build the binary COPY /cardinal ./ - RUN --mount=type=cache,target="/root/.cache/go-build" go build -v -o /go/bin/app + # RUN --mount=type=cache,target="/root/.cache/go-build" go build -v -o /go/bin/app + RUN go build -v -o /go/bin/app ################################ # Runtime Image - Normal @@ -57,7 +58,8 @@ const ( # Copy the rest of the source code and build the binary with debugging symbols COPY /cardinal ./ - RUN --mount=type=cache,target="/root/.cache/go-build" go build -gcflags="all=-N -l" -v -o /usr/bin/app + # RUN --mount=type=cache,target="/root/.cache/go-build" go build -gcflags="all=-N -l" -v -o /usr/bin/app + RUN go build -gcflags="all=-N -l" -v -o /usr/bin/app # Copy world.toml to the image COPY world.toml world.toml