Skip to content

Make deployment tag a choice and fix origin branch #212

Make deployment tag a choice and fix origin branch

Make deployment tag a choice and fix origin branch #212

Workflow file for this run

name: deploy
on:
push:
branches:
- main
paths-ignore:
- "*.md"
workflow_dispatch:
inputs:
tag:
description: 'Deployment tag'
required: true
type: type
default: 'main'
options:
- main
- dev
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Configure deployment tag
id: tag
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "out=${{ inputs.tag }}" >> "$GITHUB_OUTPUT"
else
echo "out=dev" >> "$GITHUB_OUTPUT"
fi
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v30
with:
nix_path: nixpkgs=channel:nixpkgs-24.11-darwin
- name: Generate cache key
id: cache-key
run: |
nixpkgs_hash=$(egrep -o 'archive/[0-9a-f]{40}\.tar\.gz' shell.nix | cut -d'/' -f2 | cut -d'.' -f1)
echo "out=${{ runner.os }}-$nixpkgs_hash" >> "$GITHUB_OUTPUT"
- name: Cache Nix store
id: nix-cache
uses: actions/cache@v4
with:
key: nix-${{ steps.cache-key.outputs.out }}
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 venv
uses: actions/cache@v4
with:
key: python-${{ steps.cache-key.outputs.out }}-${{ hashFiles('uv.lock') }}
path: |
~/.cache/uv
.venv
- name: Build container image
id: image
run: |
nix-shell --pure --run true
image_path=$(nix-build --no-out-link)
echo "out=$image_path" >> "$GITHUB_OUTPUT"
- 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: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id && chmod 600 ~/.ssh/id
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
" > ~/.ssh/config
- name: Upload container image
run: scp "${{ steps.image.outputs.out }}" remote:~
- name: Deploy on remote
run: |
ssh remote <<\EOF
set -e
tag="${{ steps.tag.outputs.out }}"
image_filename="$(basename "${{ steps.image.outputs.out }}")"
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 main
git checkout main
git reset --hard origin/main
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