Static Sites as Gitlab Review Apps with Caddy#

For our book’s page at Grundkurs agiles Software-Engineering we use Sphinx with myst-parser as a static site generator and Gitlab CI to build and deploy.

We use GitLab Review apps to deploy the site on a subdomain, e.g. some-branch.grundkurs-agiles-software-engineering.de.

Caddy serves different subdomains to different directories on the file system with a mapping like this (TLD = top level domain):

$TLD --> /var/www/$TLD/main
www.$TLD --> /var/www/$TLD/main
some-branch.$TLD --> /var/www/$TLD/some-branch

Development Notes#

For our 0-main.de domain (which always points to localhost), the mapping looks like this:

0-main.de --> /var/www/0-main.de/main
www.0-main.de --> /var/www/0-main.de/main
some-branch.0-main.de --> /var/www/0-main.de/some-branch

First, install Caddy locally [1]:

nix-env -iA nixpkgs.caddy

Setup some content

sudo install -d -o $(id -u) -g $(id -g) /var/www/0-main.de

for name in main foo bar; do
  mkdir -p /var/www/0-main.de/$name
  echo "<html><body><p>Hello $name</p></body></html>" > /var/www/0-main.de/$name/index.html
done

tree /var/www/0-main.de/

Create a repo

git init ~/hacks/caddy-static-review-apps
cd ~/hacks/caddy-static-review-apps

Caddyfile

{
	debug
}

:8080 {
	# https://caddyserver.com/docs/caddyfile/directives/map#examples
	map {host} {source_directory} {
		0-main.de "/var/www/0-main.de/main"
		www.0-main.de "/var/www/0-main.de/main"
		~(.*)\.0-main\.de$ "/var/www/0-main.de/${1}"
	}
	root {source_directory}
	file_server
}

Makefile

MAKEFLAGS += --always-make

default: fmt run

fmt:
	caddy fmt --overwrite

run:
	caddy run --config Caddyfile

Test it by visiting the following URLs:

URL

should show

http://0-main.de:8080/

Hello main

http://www.0-main.de:8080/

Hello main

http://foo.0-main.de:8080/

Hello foo

http://bar.0-main.de:8080/

Hello bar

And there you have it. A simple server for static sites. ❤

Visit the repo on Github.

Contact

Notes? Comments? Feel free to contact me on The Matrix.