Deploying to Fly via CircleCI

We’ve been using Fly for a while, and deploying commits on our main branch to a staging environment. Up until now, we had been doing this via a multi-stage Dockerfile, but builds had been taking a while since we weren’t doing them locally. So, we opted to build the Docker image locally in CI and deploy to Fly. More info in the Circle docs here. Figured I would post what we learned.

To achieve this, we simplified our Dockerfile to just deploy, rather than to build and deploy, and moved our build step into a Circle step. Then, we simply need to create the Docker image in Circle, push it to Fly, and deploy the image tag. Here’s the code - this is for a Node.js app:

Dockerfile

FROM mhart/alpine-node:slim-12.16.3

COPY ./dist .

EXPOSE 8080
ENV PORT 8080

Relevant part of config.yml

  ...

  backend-deploy:
    machine:
      docker_layer_caching: true
    steps:
      - checkout
      - run:
          name: Deploy Fly
          command: |
            wget -qO- 'https://getfly.fly.dev/linux-x86-64/flyctl.tgz' | tar xz
            ./flyctl auth docker
            docker build -t registry.fly.io/$FLY_APP_NAME:$CIRCLE_BUILD_NUM .
            docker push registry.fly.io/$FLY_APP_NAME:$CIRCLE_BUILD_NUM
            ./flyctl deploy -i registry.fly.io/$FLY_APP_NAME:$CIRCLE_BUILD_NUM -a $FLY_APP_NAME

  ...

That was all we had to do. One caveat - this broke Bcrypt functionality. Tried adding RUN apk add --no-cache make gcc g++ python to our Dockerfile, to no avail, so that’s one thing we are still trying to track down. Also note that I had to do this on a machine executor - was getting 401 errors during the deploy step despite the flyctl auth login line - I suspect it has something to do with the environment.

Hope this helps somebody - it has shrunk our deploy CI step from 8 minutes down to 1-2 minutes.

4 Likes

Wow that’s a nice build speed.

What version of bcrypt are you using? I think if you upgrade to bcrypt 3.0.0 it might work without a rebuild. Alpine is really nice but musl makes native modules tricky.