blog/_posts/2018-12-25-static-websites-...

126 lines
4.6 KiB
Markdown

---
title: "Static websites automatic deployment with Gitea, an example with Jekyll"
date: 2018-12-25
last_modified_at: 2023-01-29
url: static-websites-automatic-deployment-with-gitea-an-example-with-jekyll
layout: post
category: Tutorials
image: /img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_1.png
description: "Little write-up about static websites automatic deployment with Gitea hooks, example with Jekyll"
---
[![A missing blog post image](/img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_1.png)](/img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_1.png)
### Introduction
[As this blog is no longer hosted on GitHub Pages](https://mastodon.social/web/statuses/101297442552745267), I needed a quick and lite way to perform continuous deployment on a Web server.
Available on [my new Gitea instance](https://git.forestier.app/HorlogeSkynet/blog), 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](https://drone.io/)) 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 :
{% highlight bash %}
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
{% endhighlight %}
Now you'll have to add a new hook to your repository settings.
[![A missing blog post image](/img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_2.png)](/img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_2.png)
Modify the below script to fit your needs, and add it as a `post-receive` hook :
{% highlight bash %}
#!/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 !"
{% endhighlight %}
#### 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 :
{% highlight bash %}
su - www-data -l -s /bin/bash
ssh-keygen -f ~/.ssh/id_deploy
cat ~/.ssh/id_deploy.pub
# Copy the public key here !
{% endhighlight %}
Now you can go to your Gitea repository settings, and add the new deploy key generated :
[![A missing blog post image](/img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_3.png)](/img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_3.png)
Still as `www-data`, you can try your deploy key at this moment :
{% highlight bash %}
git clone <your.repository.information> /var/www/repository/
{% endhighlight %}
---
Finally, you will also need a new script (`/path/to/deployment.sh`) :
{% highlight bash %}
#!/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
{% endhighlight %}
Don't forget to :
{% highlight bash %}
chmod +x /path/to/deployment.sh
{% endhighlight %}
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 :
{% highlight bash %}
# 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
# ...
{% endhighlight %}
### Conclusion
As always, improvements are welcome below !
And guess what ? This blog post has been automatically deployed :sunglasses: