#!/usr/bin/env python3 import os import tempfile import subprocess from pathlib import Path if __name__ == '__main__': with tempfile.TemporaryDirectory() as tmpdir: pagefind = Path('~/.cargo/bin/pagefind').expanduser() pagefind = str(pagefind) if pagefind.is_file() else 'pagefind' current_branch = subprocess.run(['git', 'symbolic-ref', '-q', 'HEAD'], check=False, capture_output=True).stdout.strip() if current_branch == 'refs/heads/deploy': raise SystemError('This script cannot be run from the deploy branch (run from main instead)') subprocess.run(['git', 'worktree', 'add', '--detach', tmpdir], check=True) try: env = dict(os.environ) env['PATH'] = f'{Path("hack").absolute()}:{env["PATH"]}' subprocess.run(['hugo'], cwd=tmpdir, check=True, env=env) subprocess.run([pagefind, '--site', 'public'], cwd=tmpdir, check=True, env=env) subprocess.run(['git', 'add', '--force', 'public'], cwd=tmpdir, check=True) write_tree = subprocess.run(['git', 'write-tree', '--prefix=public/'], cwd=tmpdir, check=True, capture_output=True) tree = write_tree.stdout.strip() commit = subprocess.run(['git', 'commit-tree', '-p', 'HEAD', '-p', 'refs/heads/deploy', '-m', 'deploy.py auto-commit', tree], cwd=tmpdir, check=True, capture_output=True).stdout.strip() subprocess.run(['git', 'update-ref', '-m', f'deploy.sh update deploy branch to {commit}', 'refs/heads/deploy', commit], cwd=tmpdir, check=True) subprocess.run(['git', 'push', 'git@git.jaseg.de:blog.git', 'deploy'], cwd=tmpdir, check=True) finally: subprocess.run(['git', 'worktree', 'remove', '-f', tmpdir], check=True)