Skip to content

Publishing new post to Mkdocs

There are a few things that needs to be done when you want to update your site, for example upload a new post, or update an existing post. I had some trouble getting back to it after not doing it for a few weeks, so here is a detailed instruction, for myself but also for you.

Deleting the old files

Login to your webserver with for example winscp for filetransfer, or how you transfer your updated files.

Go to the folder that your mkdocs or zensical configuration is in. I have mine installed at '/root/blog/'. Delete the folder named 'site'.

Go to '/var/www/html/' and delete the same folder named 'site'.

Building the site

Open the command line on your webserver, in the site folder where the mkdocs or zensical configuration file is. Go ahead and build the site with the following command.

zensical build
When you refresh on winscp, or with ls in the command line you see the folder 'site' is back. Now copy that folder back to '/var/www/html/'.

Restarting the site

Run the following command to restart the site.

systemctl restart nginx
This should have updated your website.

Creating a script to update automatically

It's not so much work to do it all manually, but I have noticed that if you update the site multiple times a day, it gets annoying to do and I started to make mistakes after a few hours. So I thought, what if I just automate it. Well, here we are!

The script

First create an upload folder, for the updated files. My upload folder is in the root directory, as seen below. Create the script with the following command.

cd /root/
mkdir upload-blog
nano update-blog.sh
Here is the script I'm using, you need to adjust the paths on line 4-7 to your own paths.
#!/bin/bash

# --- PATHS  ---
UPLOAD_SOURCE="/root/blog-upload"
POSTS_TARGET="/root/blog/docs/blog/posts"
BLOG_FOLDER="/root/blog"
WEB_FOLDER="/var/www/html"

echo -e "\e[32mStart update proces...\e[0m"

# 1. Updating files in /root/blog/docs/posts
# We check what files are in the upload-folder and overwrite these in the posts-folder
if [ -d "$UPLOAD_SOURCE" ] && [ "$(ls -A $UPLOAD_SOURCE)" ]; then
    echo -e "\e[32mUpdating files in $POSTS_TARGET...\e[0m"
    cp -v "$UPLOAD_SOURCE"/* "$POSTS_TARGET/"
else
    echo -e "\e[31mNo new files found in $UPLOAD_SOURCE. Build will stop.\e[0m"
    exit 1
fi

# 2. Removing old 'site' files
echo -e "\e[32mRemoving old site files...\e[0m"
rm -rf "$BLOG_FOLDER/site"
rm -rf "$WEB_FOLDER/site"

# 3. Building new site
echo -e "\e[32mExecuting zensical build...\e[0m"
cd "$BLOG_FOLDER" || exit
zensical build

# 4. Copy newly build site to the web-folder
if [ -d "$BLOG_FOLDER/site" ]; then
    echo -e "\e[32mUploading new site to $WEB_FOLDER...\e[0m"
    cp -r "$BLOG_FOLDER/site" "$WEB_FOLDER/"
else
    echo -e "\e[31mError: The folder $BLOG_FOLDER/site is not created by the build!\e[0m"
    exit 1
fi

# 5. Restart Nginx
echo -e "\e[32mRestarting Nginx...\e[0m"
systemctl restart nginx

# 6. Empty the blog-upload folder
echo -e "\e[32mEmptying the blog-upload folder...\e[0m"
rm -rfv /root/blog-upload/*

echo -e "\e[32mUpdate completed!\e[0m"
Now save the script from above and run the following command to give permissions to the script.
chmod +x update-blog.sh

Time for a backup!

If there is a mistake in the script, you could easily delete or edit the wrong files. So please, before you run the script, create a backup of your installation!

Run the script

If you want to run the script, add your files you want to update to the upload-blog folder and run the following command.

sudo ./update-blog.sh
This should have updated the files, that you have uploaded to the upload-blog folder and restarted the website.