Deployment
Updating
- Build the new binary (and
app.css). - Copy
public/and the binary next to the running one;mvit into place (a running binary cannot be overwritten in place, but it can be replaced by a rename). sudo -u myapp ./myapp migrate --forcewith the NEW binary: the migrations are compiled into it.sudo systemctl restart myapp: the old process finishes its running requests and stops its agents (withinSHUTDOWN_TIMEOUT), then the new one starts. Between the two the proxy answers 502 for a moment. Jobs that were running go back to the queue.
With two processes: migrate, then sudo systemctl restart myapp-work myapp-web. Agents stop in the old work
process and start in the new one as soon as it is up.
A deploy script, run from the app's folder on a Linux build machine (deploy is an account on the server with sudo):
#!/usr/bin/env bash
set -euo pipefail
APP=myapp
SERVER=deploy@app.example.com
DIR=/srv/myapp
smeltery build
# A new private folder (mode 700) of the deploy account on the server: no other user can change the upload.
STAGE=$(ssh "$SERVER" mktemp -d)
rsync -a --delete --exclude /storage public/ "$SERVER:$STAGE/public/"
scp "target/release/$APP" "$SERVER:$STAGE/$APP.new"
ssh "$SERVER" "set -e
sudo rsync -a --delete --chown=root:root --chmod=D755,F644 --exclude /storage $STAGE/public/ $DIR/public/
sudo install -o root -g root -m 755 $STAGE/$APP.new $DIR/$APP.new
rm -rf $STAGE
[ -f $DIR/$APP ] && sudo cp -p $DIR/$APP $DIR/$APP.previous
sudo mv $DIR/$APP.new $DIR/$APP
cd $DIR && sudo -u $APP ./$APP migrate --force
sudo systemctl restart $APP
sleep 2 && curl -fsS http://127.0.0.1:8000/up"
--chown and --chmod give public/ the owner and modes of the table under "Server layout", whoever uploaded the
files. --exclude /storage keeps the public/storage link. The previous binary stays as myapp.previous. With two
processes, the restart line is sudo systemctl restart myapp-work myapp-web.
Rollback. When the new release added migrations, undo them with the NEW binary first (an older binary does not know them and refuses), then put the old binary back:
cd /srv/myapp
sudo -u myapp ./myapp migrate:rollback --force
sudo mv myapp.previous myapp
sudo systemctl restart myapp
# with two processes instead: sudo systemctl restart myapp-work myapp-web
migrate:rollback reverts the last batch, the migrations of the last migrate run, by running each migration's
down; --step N reverts the last N migrations instead. A rollback undoes exactly what those down functions do,
so check them before relying on it (make:migration writes a down that drops what create_* and add_*
migrations add; other names get an empty down): a down that does nothing leaves the change in the database, and
running that migration again can then fail (for example on a column that already exists).