|
| 1 | +# Auto-deploy |
| 2 | + |
| 3 | +Pushing a tag triggers an automatic deploy. Tests must pass first. |
| 4 | + |
| 5 | +## Flow |
| 6 | + |
| 7 | +``` |
| 8 | +Tag push |
| 9 | + → python-tests.yml runs |
| 10 | + → deploy.yml triggers (on workflow_run success) |
| 11 | + → POSTs HMAC-signed payload to https://dochub.be/webhook/dochub-deploy |
| 12 | + → server validates signature, runs deploy.sh |
| 13 | + → output returned to GitHub Action logs |
| 14 | +``` |
| 15 | + |
| 16 | +## How to deploy |
| 17 | + |
| 18 | +Push a tag matching the `YYYY.M.N` format: |
| 19 | + |
| 20 | +```bash |
| 21 | +git tag 2026.4.0 && git push origin 2026.4.0 |
| 22 | +``` |
| 23 | + |
| 24 | +Check the **Actions** tab for deploy output, or on the server: |
| 25 | + |
| 26 | +```bash |
| 27 | +journalctl -fu dochub-webhook |
| 28 | +``` |
| 29 | + |
| 30 | +## What `deploy.sh` does |
| 31 | + |
| 32 | +1. Validates the tag format |
| 33 | +2. Acquires a file lock (prevents concurrent deploys) |
| 34 | +3. `git fetch --tags --prune origin && git checkout $TAG` |
| 35 | +4. `uv run manage.py migrate --noinput` |
| 36 | +5. `uv run manage.py collectstatic --noinput` |
| 37 | +6. `sudo systemctl restart dochub-gunicorn dochub-celery` |
| 38 | + |
| 39 | +If any step fails, `set -e` stops the script before restarting services, so the old code keeps running. |
| 40 | + |
| 41 | +## GitHub secret |
| 42 | + |
| 43 | +`DEPLOY_WEBHOOK_SECRET` (repo Settings > Secrets and variables > Actions) must match the secret in `/etc/webhook/hooks.json` on the server. |
| 44 | + |
| 45 | +## Server-side components |
| 46 | + |
| 47 | +These files live on the server only (not in git): |
| 48 | + |
| 49 | +| File | Purpose | |
| 50 | +|------|---------| |
| 51 | +| `/srv/dochub/deploy.sh` | Thin wrapper that `exec`s the repo's `deploy.sh` | |
| 52 | +| `/etc/webhook/hooks.json` | Webhook config: HMAC validation, ref check, runs as `dochub` | |
| 53 | +| `/etc/systemd/system/dochub-webhook.service` | Runs `webhook` on `127.0.0.1:9000` | |
| 54 | +| `/etc/caddy/Caddyfile` | Proxies `/webhook/*` to the webhook service | |
| 55 | +| `/etc/sudoers.d/dochub-deploy` | Allows `dochub` to restart services without password | |
| 56 | + |
| 57 | +## Appendix: server-side file contents |
| 58 | + |
| 59 | +### `/srv/dochub/deploy.sh` |
| 60 | + |
| 61 | +```bash |
| 62 | +#!/bin/bash |
| 63 | +# Thin wrapper — real logic lives in /srv/dochub/source/deploy.sh (versioned in git) |
| 64 | +exec /srv/dochub/source/deploy.sh "$@" |
| 65 | +``` |
| 66 | + |
| 67 | +### `/etc/webhook/hooks.json` |
| 68 | + |
| 69 | +```json |
| 70 | +[ |
| 71 | + { |
| 72 | + "id": "dochub-deploy", |
| 73 | + "execute-command": "/srv/dochub/deploy.sh", |
| 74 | + "command-working-directory": "/srv/dochub", |
| 75 | + "include-command-output-in-response": true, |
| 76 | + "trigger-rule": { |
| 77 | + "and": [ |
| 78 | + { |
| 79 | + "match": { |
| 80 | + "type": "payload-hmac-sha256", |
| 81 | + "secret": "<DEPLOY_WEBHOOK_SECRET>", |
| 82 | + "parameter": { |
| 83 | + "source": "header", |
| 84 | + "name": "X-Hub-Signature-256" |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + "match": { |
| 90 | + "type": "regex", |
| 91 | + "regex": "^refs/tags/", |
| 92 | + "parameter": { |
| 93 | + "source": "payload", |
| 94 | + "name": "ref" |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | + }, |
| 100 | + "pass-arguments-to-command": [ |
| 101 | + { |
| 102 | + "source": "payload", |
| 103 | + "name": "ref" |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | +] |
| 108 | +``` |
| 109 | + |
| 110 | +### `/etc/systemd/system/dochub-webhook.service` |
| 111 | + |
| 112 | +```ini |
| 113 | +[Unit] |
| 114 | +Description=DocHub deploy webhook |
| 115 | +After=network.target |
| 116 | + |
| 117 | +[Service] |
| 118 | +ExecStart=/usr/bin/webhook -hooks /etc/webhook/hooks.json -ip 127.0.0.1 -port 9000 |
| 119 | +User=dochub |
| 120 | +Restart=on-failure |
| 121 | +RestartSec=5 |
| 122 | + |
| 123 | +[Install] |
| 124 | +WantedBy=multi-user.target |
| 125 | +``` |
| 126 | + |
| 127 | +### `/etc/caddy/Caddyfile` (relevant block) |
| 128 | + |
| 129 | +``` |
| 130 | +handle_path /webhook/* { |
| 131 | + rewrite * /hooks{uri} |
| 132 | + reverse_proxy localhost:9000 |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### `/etc/sudoers.d/dochub-deploy` |
| 137 | + |
| 138 | +``` |
| 139 | +dochub ALL=(root) NOPASSWD: /bin/systemctl restart dochub-gunicorn dochub-celery |
| 140 | +``` |
0 commit comments