Created
December 1, 2021 11:01
-
-
Save MrYawe/e6d3fcd633d37e5eb70087ab1ec2f048 to your computer and use it in GitHub Desktop.
Deploy a Phoenix app with GitHub Actions on fly.io with Docker layer caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on https://evilmartians.com/chronicles/build-images-on-github-actions-with-docker-layer-caching | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy_production: | |
name: Deploy production | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Prepare | |
id: prep | |
run: | | |
TAG=$(echo $GITHUB_SHA | head -c7) | |
IMAGE="my_app" | |
echo ::set-output name=tagged_image::${IMAGE}:${TAG} | |
echo ::set-output name=tag::${TAG} | |
- name: Set up Docker Buildx | |
id: buildx | |
# Use the action from the master, as we've seen some inconsistencies with @v1 | |
# Issue: https://github.com/docker/build-push-action/issues/286 | |
uses: docker/setup-buildx-action@master | |
# Only worked for us with this option on 🤷♂️ | |
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 build target here | |
target: app | |
load: true | |
tags: ${{ steps.prep.outputs.tagged_image }} | |
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 | |
- name: Move cache | |
run: | | |
rm -rf /tmp/.buildx-cache | |
mv /tmp/.buildx-cache-new /tmp/.buildx-cache | |
- uses: superfly/flyctl-actions@master | |
env: | |
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} | |
with: | |
args: deploy --config ./fly.prod.toml --image ${{ steps.prep.outputs.tagged_image }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment