I recently migrated from my previous server recently, and I took this opportunity to clean up my blog deployment routine. As I have said previously, this blog is powered by pelican and rst files, which means I am maintaining it in a private git repo.
Since I migrated to a more powerful server, I can now run gitea without worries, which allows me to use webhooks to notify new commits to a daemon.
I wrote a minimal HTTP server using aiohttp; as the blog is on the same machine as gitea, I don’t have to bother with secrets, nginx config, or whatever, and can open a port on localhost instead. (worst case: someone builds my blog)
import os import sys from aiohttp import web from subprocess import run from asyncio import get_event_loop from os.path import abspath, dirname os.chdir(dirname(abspath(sys.argv))) def update(): run(['git', 'pull']) run(['./deploy.sh']) async def handle(request): loop = get_event_loop() await loop.run_in_executor(None, update) return web.Response(text='OK') app = web.Application() app.add_routes([web.get('/deploy', handle)]) web.run_app(app, host='localhost', port=2345)
This daemon runs with a systemd service, taking care of reboots and failures:
[Unit] Description=Blog deployment After=network.target [Service] ExecStart=/usr/bin/python -OO /home/blog-dedploy/blog/server.py User=blog-deploy Restart=on-failure [Install] WantedBy=multi-user.target
I do the build + deploy in a bash script because that’s the quickest way to write it. Pelican clears the directory first, which means the renaming dance is required to minimize possible errors while the blog is building.
#!/usr/bin/env bash pushd $(dirname $(realpath $0)) mkdir -p tmp_build public_html make OUTPUTDIR=$PWD/tmp_build html chown blog-deploy:http -R tmp_build mv public_html to_delete mv tmp_build public_html rm -r to_delete