Self Serving Hugo Website
Hello everyone! In this piece, I’ll show you how to set up your very own Docker stack with NGINX and rsync, making it super easy to deploy your applications using GitHub Actions. It’s a fantastic way to streamline your development process and get your apps up and running quickly.
We’ll walk through the steps together, from configuring your Docker environment to setting up NGINX for reverse proxy and load balancing. Plus, we’ll integrate rsync for smooth file synchronization. By the end, you’ll have a slick deployment system, ready to handle your app deployments like a pro.
Whether you’re a seasoned developer or just getting started with Docker and GitHub Actions, this guide is designed to be straightforward and informative. So, welcome, folks, let’s jump right in and discover how to rock your deployments in no time! Let’s get started!
Prerequisits
There are a few preliminary steps, which need to be taken care of before proceeding.
- Virtua:l Private Server
- Reverse Proxy
- Docker Swarm
Configure the docker containers
In our screnario we have a reverse proxy, which will route the traffic to the
correct containers, while also providing SSL encryption. Reverse proxy can route
traffic to containers which are attached to the same network. In our case we
name it internal_network
.
We will deploy two containers, one for rsync
and one for nginx
server. We will
attach the nginx
container to the internatl_network
. We will also need to create
a new volume, which we will attach to both containers. rsync
will get the files
from github actions pipeline and save them onto the hugo_data
. The nginx
container will serve the site from the same volume. We create volumes using
portainer interface, but we could also use docker directly:
|
|
We will further need to configure ssh keys. In the /root/.ssh/
we create new
key, for just this pipeline.
|
|
The mail needs to be changed. This will create two files hugo
and hugo.pub
. You
need to add this public to authorized keys.
|
|
Lastly, we need to configure nginx
container. We select arbirary location
/root/config/nginx/nginx.conf
on the host server. With the following config:
|
|
We will need to build a custom rsync
image before continuing
|
|
In the next step we bring all of these configurations together:
|
|
Configure the Github Actions
Github provides a nice way of configuring automatic deployment with Github
actions. To configure this simply create a new directory in the repository if it
doesn’t exist already (.github/workflows/deploy.yml
).
Before we start configuring deployment actions, first we need to configure a few secrets with GitHub. Navigate to the repository and then to settings. Under security tab you will find secrets and variables. Then navigate to actions and then add a few secrets. Add the following secrets:
SSH_PRIVATE_KEY
:- For contents paste in the generated key in the docker step,
- To get the key, inside of the server execute
cat /root/config/hugo
, - Paste the key beginning with
-----BEGIN OPENSSH PRIVATE KEY-----
and ends with-----END OPENSSH PRIVATE KEY-----
into the secret content box.
SSH_HOST
:- Add in the ip of the server (or domain name if configured).
SSH_USER
:- Add in the user name
root
.
- Add in the user name
The following deploy script will execute steps
when push to master branch has
been pushed to the repository. The steps perform the following actions:
-
Checkout Code
- Checks out the code
-
Setup Hugo
- Uses hugo actions with extended version (for scss)
-
Build Hugo
- Runs a hugo to compile the website html pages.
- With the flag
--minify
it will produce minified css (which reduces traffic demands)
-
Install SSH Keys
- Before we can push to
rsync
container, we need to configure ssh keys
- Before we can push to
-
Add known Hosts
- To prevent prompts later (which we cannot respond to in the action) we need to configure known hosts, by querying for the public keys ahead of time.
-
Deploy with rsyn
- Finally executing
rsync
command to transport the compiled webpage.
- Finally executing
|
|
Concluding
In conclusion, we have configured a pipeline, where after updating the repository, the server automaticallly updates using a pipeline through Github or Gitlab.