banner
阿凯

阿凯的生活记录

日拱一卒,对生活充满热爱!

Real-time pull of Github repository via webhook

The blog domain is not registered, but I have a server in Hong Kong that can be used to synchronize static files from the Github repository.

This allows the content of the Gridea blog to be synchronized to Github and then pulled to the local directory by the webhook of the Baota panel.
The domain resolves to Github abroad and to my own server domestically, achieving website access acceleration.

First, the Baota panel needs to be installed on the server. I prefer using this panel, but other methods to install the webhook are also fine.

After installing the webhook plugin on the Baota panel, create a hook and add the script below, only modifying the git address; no other changes are needed.

#!/bin/bash
echo ""
# Output the current time
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"
echo "Start"
# Check if the Baota WebHook parameter exists
if [ ! -n "$1" ]; then
                echo "Parameter error"
        echo "End"
        exit
fi
# Git project path
gitPath="/www/wwwroot/$1"
# Git URL
gitHttp="https://github.com/xxxxx.git" # Just modify it to the repository's git address, no other changes needed

echo "Web site path: $gitPath"

# Check if the project path exists
if [ -d "$gitPath" ]; then
        cd $gitPath
        # Check if the git directory exists
        if [ ! -d ".git" ]; then
                echo "Cloning git in this directory"
                sudo git clone $gitHttp gittemp
                sudo mv gittemp/.git .
                sudo rm -rf gittemp
        fi
        echo "Pulling the latest project files"
        sudo git reset --hard origin/master
        sudo git pull        
        echo "Setting directory permissions"
        sudo chown -R www:www $gitPath
        echo "End"
        exit
else
        echo "This project path does not exist"
                echo "Creating project directory"
        mkdir $gitPath
        cd $gitPath
        # Check if the git directory exists
        if [ ! -d ".git" ]; then
                echo "Cloning git in this directory"
                sudo git clone $gitHttp gittemp
                sudo mv gittemp/.git .
                sudo rm -rf gittemp
        fi
        echo "Pulling the latest project files"
        sudo git reset --hard origin/master
        sudo git pull
        echo "Setting directory permissions"
        sudo chown -R www:www $gitPath
        echo "End"
        exit
fi

After saving, click to view the key, as follows:

Baota WebHook Usage:
GET/POST:
http://server_ip:port/hook?access_key=HOOK_key&param=aaa
@param access_key string HOOK key
@param param string Custom parameter (received in the hook script using $1)

==Note: Before pasting, replace the last aaa in the URL with the directory name of your corresponding static blog on Baota==
For example, if my website path is wwwroot/blog, replace ==aaa== with ==blog==
If not replaced, a directory named aaa will be created in your server's root directory, and you can also change the site directory to the aaa directory.

Copy the automatically generated link above and paste it into the Github webhook settings
544a24a32d1aa

Select content type as ==json==, the secret is a string key for the webhook, copy and paste it, keep other settings default, and save the submission.

Then go to Gridea to submit an update for the blog, check the Webhook logs, and see if there are files in the directory to determine if synchronization was successful.

When the code in the Github repository is updated, the push event is triggered through the git webhook. When users submit code (git push), the Baota webhook plugin on the server pulls the latest code (git pull).

==Note:== Using webhook to pull the Github repository on Alibaba Cloud domestic servers often fails.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.