
This patch improves static websites automatic deployment tutorial for Gitea by registering Bash `trap` ASAP and fixing a strange issue with `~/` actually not being expanded to the actual `git` user's home directory (where our SSH identities are !) but (apparently) to the Gitea install path.
4.6 KiB
title | date | last_modified_at | url | layout | category | image | description |
---|---|---|---|---|---|---|---|
Static websites automatic deployment with Gitea, an example with Jekyll | 2018-12-25 | 2023-01-29 | static-websites-automatic-deployment-with-gitea-an-example-with-jekyll | post | Tutorials | /img/blog/static-websites-automatic-deployment-with-gitea-an-example-with-jekyll_1.png | Little write-up about static websites automatic deployment with Gitea hooks, example with Jekyll |
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 :
{% 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.
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 :
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 ; 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 ; 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 😎