diff --git a/README.md b/README.md index 14c50c7..5f77ec7 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,27 @@ # deploy-kit +Deploy a static site to your own server **without dropping open connections**: three bash scripts, release directories, one atomic symlink rename, a validated Caddy config, a graceful `caddy reload` and a health check. + +[![CI](https://github.com/wonmortech/deploy-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/wonmortech/deploy-kit/actions/workflows/ci.yml) [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/wonmortech/deploy-kit/badge)](https://scorecard.dev/viewer/?uri=github.com/wonmortech/deploy-kit) +![version](https://img.shields.io/badge/version-0.1.0-141413) +![licence](https://img.shields.io/badge/licence-MIT-141413) + +**[Website](https://wonmor.io) · [Post: Zero-downtime static deploys with Caddy](https://wonmor.io/engineering/zero-downtime-static-deploys/) · [Changelog](CHANGELOG.md) · [Tags](https://github.com/wonmortech/deploy-kit/tags)** + +![One deploy in eight steps, and the symlink swap](docs/how-it-works.svg) -A small template for deploying a static site to your own server **without dropping open connections**: release directories, one atomic symlink rename, a validated Caddy config, a graceful `caddy reload`, and a health check against `/version.txt`. +## What it does -It is a generalised version of the deploy flow described in [Zero-downtime static deploys with Caddy](https://wonmor.io/engineering/zero-downtime-static-deploys/). All hosts, paths and names here are placeholders: `example.com`, `deploy@your-server`, `/srv/sites/example`. +- **Release directories.** Every deploy unpacks into `www/releases/-`; a live release is never modified. +- **Atomic swap.** A second symlink is renamed over `www/current` with one `rename(2)`, so every request sees the old release or the new one. +- **Validated config.** `caddy validate` runs in a throwaway container with `--network none` before anything live changes. +- **Graceful reload.** `caddy reload` applies the config in place; open downloads keep their file descriptors and finish. +- **Health check.** `deploy.sh` polls `/version.txt` on the public URL until it returns the new commit hash. +- **Rollback in one command.** `rollback.sh` uses the same rename and reload, so it is zero-downtime too. +- **Tested without a server.** `tests/run.sh` stubs `docker`, `ssh`, `curl` and `date`; CI runs it with ShellCheck on every pull request. + +It is a generalised version of the deploy flow in the [post](https://wonmor.io/engineering/zero-downtime-static-deploys/). All hosts, paths and names here are placeholders: `example.com`, `deploy@your-server`, `/srv/sites/example`. ``` deploy/ @@ -17,11 +34,6 @@ tests/run.sh tests for the three scripts (docker, ssh, curl stubbed; no .github/workflows/deploy.yml example manual-dispatch workflow ``` -## Requirements - -- Server: Linux with Docker and the Compose plugin, a user that can run `docker` and write to `/srv/sites/example`, SSH access. -- Client: `bash`, `git`, `ssh`, `tar`, `curl`. The site must be committed in git; `deploy.sh` deploys the current commit only. - ## Quick start ```bash @@ -34,6 +46,8 @@ SITE_URL=https://example.com REMOTE_DIR=/srv/sites/example deploy/deploy.sh depl The first deploy creates the layout on the server and starts the container. Point your reverse proxy (or DNS, if this Caddy terminates TLS itself; see the comment at the top of the Caddyfile) at it. +**Requirements.** Server: Linux with Docker and the Compose plugin, a user that can run `docker` and write to `/srv/sites/example`, SSH access. Client: `bash`, `git`, `ssh`, `tar`, `curl`. The site must be committed in git; `deploy.sh` deploys the current commit only. + ## How it works Server layout under `/srv/sites/example`: @@ -47,22 +61,18 @@ docker-compose.yml the web container REVISION commit hash of the live release ``` -One deploy, step by step: - | Step | What happens | Why | |---|---|---| | 1. Stage | `deploy.sh` copies `dist/` and writes the commit hash to `version.txt` inside it | The health check needs a value that is unique to this deploy | | 2. Validate | `remote.sh` runs `caddy validate` on the new Caddyfile in a throwaway container with `--network none` | A broken config stops the deploy before anything live changes | | 3. Unpack | The site moves to `www/releases/-` | No request has ever seen this directory; the timestamp means redeploying the same commit never overwrites the live release | -| 4. Swap | `ln -sfn releases/ www/current.next && mv -T www/current.next www/current` | `ln -sfn` alone is unlink-then-create; renaming a second link over the first is a single `rename(2)`, so every request sees the old release or the new one | +| 4. Swap | `ln -sfn releases/ www/current.next && mv -T www/current.next www/current` | `ln -sfn` alone is unlink-then-create; renaming a second link over the first is a single `rename(2)` | | 5. Config | `conf/Caddyfile.next` is renamed over `conf/Caddyfile` | A single-file bind mount pins the old inode; mounting the directory lets the container see the renamed file | | 6. Reload | `docker compose up -d` (a no-op unless the compose file changed), then `caddy reload` | Reload applies the config in place. Open downloads keep their file descriptors on the old release and finish normally | | 7. Prune | Only the last `KEEP_RELEASES` (default 3) releases are kept | Old releases stay long enough for slow downloads and for rollback | | 8. Health check | `deploy.sh` polls `https://example.com/version.txt` until it returns the new commit hash | Proves the public URL serves the new release, through any proxy in front of it | -### The setting deliberately left out - -Caddy's `grace_period` limits how long the server waits for open connections on shutdown, but Caddy also applies it on config reloads, so any value would force-close slow downloads during an ordinary deploy. It is left unset. The bound on shutdown time is Docker's `stop_grace_period: 15s`, which only matters when the container is actually recreated (when `docker-compose.yml` changes). +**The setting deliberately left out.** Caddy's `grace_period` limits how long the server waits for open connections on shutdown, but Caddy also applies it on config reloads, so any value would force-close slow downloads during an ordinary deploy. It is left unset. The bound on shutdown time is Docker's `stop_grace_period: 15s`, which only matters when the container is actually recreated (when `docker-compose.yml` changes). ## Rollback @@ -75,11 +85,11 @@ cd /srv/sites/example ./rollback.sh # to a specific kept release ``` -Rollback uses the same rename and reload, so it is also zero-downtime. If the health check fails, `deploy.sh` exits non-zero and prints the rollback command; it does not roll back on its own. +If the health check fails, `deploy.sh` exits non-zero and prints the rollback command; it does not roll back on its own. ## GitHub Actions -[`.github/workflows/deploy.yml`](.github/workflows/deploy.yml) is an example that runs on manual dispatch only. It builds the site, installs an SSH key and a pinned `known_hosts` from repository secrets, and calls `deploy/deploy.sh`. Configure: +[`.github/workflows/deploy.yml`](.github/workflows/deploy.yml) is an example that runs on manual dispatch only. It builds the site, installs an SSH key and a pinned `known_hosts` from repository secrets, and calls `deploy/deploy.sh`. | Name | Kind | Example | |---|---|---| @@ -92,23 +102,20 @@ Keep `dist/` in `.gitignore`: `deploy.sh` refuses to deploy from a working tree ## Limitations -These are the same limits the post lists for the original flow: - - **Mixed versions within one page load.** If the swap lands between the HTML request and an asset request, a page can get HTML from the old release and CSS from the new one. Content-hashed asset names reduce the effect. - **Rollback is a command.** Nothing switches back automatically after a failed health check. - **The health check is one URL.** It proves the release is served, not that every page works. - **A recreate still has a limit.** When `docker-compose.yml` changes, open downloads get `stop_grace_period` (15 s), not unlimited time. - **`mv -T` is GNU coreutils.** The server must be Linux (the client side does not need it). -## Testing and contributing +## Development ```bash shellcheck --severity=style deploy/*.sh tests/*.sh -bash tests/run.sh +bash tests/run.sh # on Linux: the swap tests need real symlinks and GNU mv ``` -CI runs both on every pull request. See [CONTRIBUTING.md](CONTRIBUTING.md) for the rules, and -[SECURITY.md](SECURITY.md) to report a vulnerability privately. Changes are listed in [CHANGELOG.md](CHANGELOG.md). +CI runs both on every pull request. See [CONTRIBUTING.md](CONTRIBUTING.md) for the rules and [SECURITY.md](SECURITY.md) to report a vulnerability privately. Changes are listed in [CHANGELOG.md](CHANGELOG.md). ## Licence diff --git a/docs/how-it-works.svg b/docs/how-it-works.svg new file mode 100644 index 0000000..cf34477 --- /dev/null +++ b/docs/how-it-works.svg @@ -0,0 +1,50 @@ + + deploy-kit: one deploy in eight steps, and the symlink swap on the server + + One deploy, eight steps + deploy.sh runs on your machine or in CI; remote.sh runs on the server. Nothing live changes before step 4. + + + + + deploy.sh · client + 1 Stagedist/ + version.txt = commit + ↑ Uploadtar over ssh into new/ + 8 Checkpoll /version.txt = commit + + + + + remote.sh · server + + 2 Validatecaddy validate, no network + 3 Unpackreleases/<sha12>-<UTC> + 4 Swap + 5 Configone rename(2) each + 6 Reload · 7 Prunecaddy reload, keep 3 + + + + + Step 4: the swap every request survives + + + + /srv/sites/example/www + releases/ + a1b2c3d4e5f6-20261116T091204Z + 0f9e8d7c6b5a-20261117T054011Z + 9c8b7a6d5e4f-20261117T113052Z + current→ releases/9c8b7a6d5e4f-… + + + + # not atomic: unlink, then create + ln -sfn releases/new www/current + # atomic: build a second link, rename it over + ln -sfn releases/new www/current.next + mv -T www/current.next www/current + # every request sees old or new, never neither + + + +