Docker image layer caching in builder and implications when shared between deploys

Hey,

We are using the free builder at the moment from Github CI actions.
Both for prod and pr review app deployments.

I am working on setting up a multi stage docker image to have separate steps for the hex and npm packages.

I am wondering how does the following scenario play out:

  • Change deps on a pr
  • destroy builder on fly to clear the cache
  • Deploy the pr
  • deploy prod

Will in this case the prod deploy use the layer (for anything) that was cached by the pr deployment?
Would the pr docker image/layers impact the prod deployment?

Thank you

The caching from the PR deployment can affect the production deployment yep. There’s very little hidden magic to the builder, it’s just a VM with Docker running in it (it’s not refreshed between builds or anything).

This begs the question of what’s going on where deploying a PR might cache something you don’t want cached - I’m guessing that’s related to the multi-stage part?

What I’ve done is set a build argument in a stage, and then changed/used a specific build argument if I needed to bust the cache of that build step - perhaps depending on staging vs production, etc.

I think something like this might work:

###
# Build the foobar command
##
FROM golang:1.19 as some-client

# >>>>>>>>>>>>>>>>>>> Here's the build argument I'm talking about
ARG BUILD_ENV="staging"

WORKDIR /opt

# >>>>>>>>>>>>>>>>>>> Cache busted here
RUN echo "building $BUILD_ENV"

RUN --mount=type=secret,id=secrets,dst=/tmp/secrets bash -c 'source /tmp/secrets \
    && git clone --single-branch --branch main https://$GITHUB_ACCESS_TOKEN:x-oauth-basic@github.com/foo/bar.git /opt'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o bin/foobar cmd/main.go

###
# Put everything together in last stage
##
FROM ubuntu:22.04
COPY --from=some-client /opt/bin/foobar /usr/local/bin/foobar

# and so on...

Hey,

Thanks for the reply!

I looked into docker caching, this is the simplest and most relevant way of explanation: caching - How does Docker know when to use the cache during a build and when not? - Stack Overflow

If I have my package.json and my mix.exs files as the first copy statement of their respective steps, I think docker should just figure it out.

So if my build step is a child to the dependency steps, or use things from there docker should know when to use the cache and when not to.

I will try to verify this behavior and update this post accordingly.

EDIT:
This seems to work nicely!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.