Serving Branches on a Subdomain using Caddy and GitLab Review Apps

This post describes how to work with Caddy. I've previously written about how to use GitLab Review Apps with Capistrano and Nginx, which may be of interest.

Although this example uses GitLab Review Apps, it could much as easily be used for any other branch-based deployments.

I've recently been wanting to move to Caddy, for a number of reasons, but mostly its ability to manage the Let's Encrypt Lifecycle, as well as being a single static (Go) binary.

However, one of the big concerns I had with moving from Nginx to Caddy was the configuration for my Review Apps configuration. I currently am able to publish a branch, example/review-apps, to an FQDN, example-review-apps.www.review.jvt.me, where example-review-apps is provided to me by GitLab CI in the variable CI_COMMIT_REF_SLUG.

My Nginx configuration was quite simple, utilising nginx's regular expression names:

# /etc/nginx/sites-enabled/review.jvt.me
server {
    listen 80;
    server_name ~^(www\.)?(?<sname>.+?).review.jvt.me$;
    root /srv/www/review.jvt.me/review.jvt.me/review/$sname/current/site;

    index index.html index.htm index.php;
    error_page 404 /404.html;

    charset utf-8;

    access_log /var/log/nginx/review.jvt.me-access.log;
    error_log  /var/log/nginx/review.jvt.me-error.log debug;
}

The ease of this solution made me concerned that I would not be able to easily migrate to the same automagic redirection using Caddy. However, in Caddy 0.10.12, this was included using the concept of Caddy labels, via confirmation in the Caddy Community forums:

https://*.review.jvt.me {
  root /srv/www/review.jvt.me
  rewrite {
    to /{label1}{uri}
  }
}

Let us take the example above, and we come in on https://example-review-apps.www.review.jvt.me/test:

  • Caddy passes the value of the wildcard into label1, which with the rewrite rule expands out to /example-review-apps/test
  • Caddy combines this with the root directive to expand this out on disk to /srv/www/review.jvt.me/example-review-apps/test

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#guide #gitlab #review-apps #caddy #howto #deploy.

This post was filed under articles.

Has this content helped you? Please consider supporting me so I can continue to create content like this!

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.