Skip to content

Fix background cache not updating #170

Fix background cache not updating

Fix background cache not updating #170

Workflow file for this run

name: deploy
on:
push:
branches:
- dev
- main
paths-ignore:
- "*.md"
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Prepare environment
run: |
echo "TAG=${{ github.head_ref || github.ref_name }}" >> $GITHUB_ENV
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Nix
uses: cachix/install-nix-action@v25
with:
nix_path: nixpkgs=channel:nixpkgs-23.11-darwin
- name: Extract nixpkgs hash
run: |
nixpkgs_hash=$(grep -o -P '(?<=archive/)[0-9a-f]{40}(?=\.tar\.gz)' shell.nix)
echo "NIXPKGS_HASH=$nixpkgs_hash" >> $GITHUB_ENV
- name: Cache Nix store
uses: actions/cache@v4
id: nix-cache
with:
key: nix-${{ runner.os }}-${{ env.NIXPKGS_HASH }}
path: /tmp/nix-cache
- name: Import Nix store cache
if: steps.nix-cache.outputs.cache-hit == 'true'
run: |
nix-store --import < /tmp/nix-cache
- name: Cache Python packages
uses: actions/cache@v4
with:
key: python-${{ runner.os }}-${{ hashFiles('poetry.lock') }}
path: .venv
- name: Install dependencies
run: |
nix-shell --pure --run true
nix-shell --pure --arg isDevelopment false --run true
- name: Export Nix store cache
if: steps.nix-cache.outputs.cache-hit != 'true'
run: |
nix-store --export $(find /nix/store -maxdepth 1 -name '*-*') > /tmp/nix-cache
- name: Build container image
run: |
echo "IMAGE_PATH=$(nix-build --no-out-link)" >> $GITHUB_ENV
- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
echo "Host remote
HostName ${{ secrets.SSH_HOST }}
User ${{ secrets.SSH_USER }}
Port ${{ secrets.SSH_PORT }}
IdentityFile ~/.ssh/id_rsa
" > ~/.ssh/config
- name: Upload container image
run: |
scp "${{ env.IMAGE_PATH }}" remote:~
- name: Deploy on remote
run: |
ssh remote <<\EOF
set -e
tag="${{ env.TAG }}"
image_filename="$(basename "${{ env.IMAGE_PATH }}")"
cleanup() {
cd ~
echo "Cleaning up"
rm -f "$image_filename"
}
trap cleanup EXIT
echo "Loading Docker image"
docker load < "$image_filename"
echo "Fetching latest changes from the git repository"
cd "$tag"
git fetch origin "$tag"
git checkout "$tag"
git reset --hard "origin/$tag"
echo "Restarting containers"
docker compose down --remove-orphans
TAG="$tag" docker compose --env-file "envs/compose/$tag.env" up -d
echo "Pruning dangling images"
docker image prune -f
EOF