A missing blog post image

Introduction

As this blog is no longer hosted on GitHub Pages, I needed a quick and lite way to perform continuous deployment on a WEB server.

Available on my new Gitea instance, I thought about webhooks at first.
On the other hand, I didn’t want to deploy a webhook server dedicated to static deployment, nor a CI/CD solution (as Drone) for this specific need.

So this is a short write-up (only) about SSH, BASH scripts and Jekyll usage, if you want to achieve something similar.

Here, I assume that your Gitea instance and your WEB server are running separately.

The procedure

On the Gitea container

Before anything else, impersonate the git session and generate a keys pair to allow SSH authentication on the WEB server :

su - git
ssh-keygen -f ~/.ssh/id_repository
cat ~/.ssh/id_repository.pub
# Copy this public key for later !
ssh-keyscan -H web.server.ip.address > /home/git/.ssh/known_hosts 2> /dev/null

Now you’ll have to add a new hook to your repository settings.

A missing blog post image

Modify script below to fit your needs, and add it as a post-receive hook :

#!/usr/bin/env bash


# Loads the SSH key into an SSH agent, runs the deployment and kills the SSH agent
nohup bash -c ' \
        eval "$(ssh-agent -s)" && \
        ssh-add -t 60 ~/.ssh/id_repository && \
        ssh root@web.server.ip.address "/path/to/deployment.sh repository" && \
        trap "ssh-agent -k" EXIT \
' > /dev/null 2>&1 &

echo "Automatic deployment successfully started !"


exit 0

On the WEB server

Open a root shell on your WEB server and let’s generate a deploy key for the www-data session, allowing it to pull from the Gitea repository :

su - www-data -l -s /bin/bash
ssh-keygen -f ~/.ssh/id_deploy
cat ~/.ssh/id_deploy.pub
# Copy the public key here !

Now you can go to your Gitea repository settings, and add the new deploy key generated :

A missing blog post image

Still as www-data, you can try your deploy key at this moment :

git clone <your.repository.information> /var/www/repository/

Finally, you will also need a new script (/path/to/deployment.sh) :

#!/usr/bin/env bash


# Repository deployment
if [[ "$1" == "repository" ]]; then
	su - www-data -l -s /bin/bash -c ' \
		eval "$(ssh-agent -s)" && \
		ssh-add -t 60 ~/.ssh/id_deploy && \
		git -C /var/www/repository/ pull && \
		JEKYLL_ENV=production jekyll build -s /var/www/repository/ -d /var/www/repository/_site/ && \
		trap "ssh-agent -k" EXIT
	'

# Another website ? Sure.
elif [[ "$1" == "another-website" ]]; then
	# Your own logic over here...

fi


exit 0

Don’t forget to :

chmod +x /path/to/deployment.sh

Finally, you’ll have to authorize the remote git session (present on the Gitea machine) to execute the script above with a specific argument (/root/.ssh/authorized_keys), set the public key copied at the first step of this guide :

# Static websites deployment
from="your.gitea.ip.address",command="/path/to/deployment.sh repository" ssh-rsa AAAA... git@gitea-container
from="your.gitea.ip.address",command="/path/to/deployment.sh another-website" ssh-rsa AAAA... git@gitea-container
# ...

Conclusion

As always, improvements are welcome below !

And guess what ? This blog post has been automatically deployed :sunglasses: