From b2bcd48feb35e4ba7bc5cf9c50aa2e3d654eef06 Mon Sep 17 00:00:00 2001 From: Jeppe Fihl-Pearson Date: Mon, 5 Dec 2022 03:47:43 +0000 Subject: [PATCH] Cache downloaded Go dependencies and Atlantis compilation in Docker (#2738) This has the potential to speed up rebuilds of the Atlantis container significantly, especially for non-x86 platforms which are emulated when done in GitHub Actions. It works by using cache mounts: https://docs.docker.com/engine/reference/builder/#run---mounttypecache. They mean we can have specific paths set that are carried over from one image build to the next, regardless of which files are changed in the project. The files are however at the same time not part of the final image, which reduces the generated "builder" image from 1.66 GB to just 386 MB on the current main branch. In my local tests, which doesn't make use of emulation, I saw a drop for the "go build" step going from taking just over 40 seconds with an empty cache to around 1 second when I make a one character change to a Go source file. That is otherwise a change that prior to this would cause a complete rebuild of the Docker image layer. Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com> --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 0bd0f7eb1..e771d942f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,9 @@ FROM golang:1.19.3-alpine AS builder WORKDIR /app COPY . /app -RUN CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" -v -o atlantis . +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" -v -o atlantis . # Stage 2 # The runatlantis/atlantis-base is created by docker-base/Dockerfile.