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
user and generate a key 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.
Modify the below script to fit your needs, and add it as a post-receive
hook :
#!/usr/bin/env bash
# Load the SSH key into an SSH agent and run the deployment before killing the SSH agent
nohup bash -c ' \
eval "$(ssh-agent -s)" && \
trap "ssh-agent -k" EXIT && \
ssh-add -t 60 /home/git/.ssh/id_repository && \
ssh root@web.server.ip.address "/path/to/deployment.sh repository" \
' > /dev/null 2>&1 &
echo "Automatic deployment successfully started !"
On the Web server
Open a root
shell on your Web server and let’s generate a deploy key for the www-data
user, 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 :
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)" && \
trap "ssh-agent -k" EXIT && \
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/ \
'
# Another website ? Sure.
elif [[ "$1" == "another-website" ]]; then
# Your own logic over here...
fi
Don’t forget to :
chmod +x /path/to/deployment.sh
Finally, you’ll have to authorize the remote git
user (the one likely running Gitea) 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