The reason why I use --local-only, is because I’ve had a lot of trouble with remote building in the past. Builders hanging, running out of mem, etc. This has been much more stable for me.
I have no idea how to cache deps though, as it’s kind of a single step here. Does anyone have any tips?
(or equivalent depending on your app e.g setup-python, setup-go … etc).
Take a look at this linked section for a full example:
That would be package dependencies, rather than Docker build steps/layers (whatever the word is) … but I’d assume those would already be cached? Perhaps not. In which case this wouldn’t help with that.
To avoid remote-building on Fly’s infra, I was essentially looking for fly deploy --image=<image-tag>, and then caching of the Docker layers in Github Actions.
This cuts down my build time by a lot. Dockerfile (Elixir but this should be quite generic):
ARG ELIXIR_VERSION=1.13.4
ARG OTP_VERSION=24.3.3
ARG DEBIAN_VERSION=bullseye-20210902-slim
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
FROM ${BUILDER_IMAGE} as builder
# Compile
[...]
# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE} as deploy
RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
[...]
Important here is the as deploy in the second stage. That’s being used here in the Github workflow file .github/workflows/main.yml:
name: Fly Deploy
on:
push:
branches:
- main
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
MIX_ENV: 'prod'
jobs:
deploy:
name: Build and Deploy App
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Docker BuildX
id: buildx
uses: docker/setup-buildx-action@master
with:
install: true
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
# Key is named differently to avoid collision
key: ${{ runner.os }}-multi-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-multi-buildx
- name: Build production image
uses: docker/build-push-action@v2
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
file: ./Dockerfile
# Set the desired Dockerfile build target here
target: deploy
push: false
# Make the resulting image available in other stages
load: true
tags: prod-image:latest
cache-from: type=local,src=/tmp/.buildx-cache
# Note the mode=max here
# More: https://github.com/moby/buildkit#--export-cache-options
# And: https://github.com/docker/buildx#--cache-tonametypetypekeyvalue
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --image=prod-image:latest
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache