Automation

Digital Garden

One of my needs is an easy setup for gardening. I need to be able to write notes on all of my devices, including my phone. gitea has a built-in Markdown editor, so I can edit text and work with git at the same time. The content is rendered by mdBook.

Using git hooks

Git hooks are run on the server while commits are being applied to a repository. This is a low resource approach because it only runs when the garden received an update. It is quite limited to the machine that hosts the git repository though.

Gitea has an option to manage git hooks. I read they only run if Gitea manages the SSH keys. I didn't test with local SSH keys. To enable git hooks, change the app.ini and add DISABLE_GIT_HOOKS = false, default is true. Restart gitea. Caution! : Users who can access git hooks can run arbitrary commands on the server. Only allow this option if you can fully trust the user.

Go to the repo settings and define the post-receive git hook:

#!/bin/sh
echo "removing old deployment"
targetdir=/yourwebdir
cd $targetdir
rm -rf *

echo "running deployment"
git --work-tree=$targetdir --git-dir=/path-to-your/garden.git checkout -f

/usr/local/bin/mdbook build
rm *
rm -rf src/
cp -r book/* .
rm -rf book/
echo "new mdbook built and deployed"

The directory /yourwebdir needs to have read/write permissions for the user running gitea which is usually git.

Using deploy-keys and a cron job

On my server I created a user

useradd --system --create-home --home-dir /var/lib/mdbook --shell /usr/sbin/nologin --comment "mdbook Digital Garden" mdbook

For this user, I create a new ssh-key. When asked where to store it, I choose /var/lib/mdbook/.ssh

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

In gitea, I use the option for deploy-keys and add above key. Deploy keys are read only for a specific repository.

To /var/lib/mdbook I add a build.sh script

#!/bin/bash
cd /var/lib/mdbook/garden
/usr/bin/git pull -q
/usr/bin/mdbook build -d /usr/share/garden /var/lib/mdbook/garden

And a cronjob in /etc/crontab

* * * * *  mdbook  /var/lib/mdbook/build.sh