Use github webhook and Fastapi to automatically update the website

My small broken site is placed on a beggar host in a foreign computer room, and the code is hosted in a private warehouse of github. When I push the code, I hope to update the website automatically. There are actions in github, and I can write relevant logic by myself to update the code on the runner of github. The easiest way is to ssh to the server to perform related operations, but to fill in the Actions secrets in this way, I am honestly a little uneasy. I want to write a simple service update code by myself. After all, I am in control of everything. The truth is in my hand. I have it. Reliance, supply chain security^_^

Write the interface for github webhook

  • As shown below, when submitting a post request to /api/update/mephistocc using the Background Tasks function of Fastapi, execute the script to update the website
1def update_site(script_name=""):
2["/bin/bash", script_name])
6async def update_mephisto_cc(background_tasks: BackgroundTasks):
7    background_tasks.add_task(update_site, script_name="")
8    return {"message": "update site in the background"}
  • Super simple update script, just pull the main repository
 1(.venv) ➜ git:(main) cat
 3echo 'start executing webhook hook'
 5HOME_DIR=/data/ #This directory is the server page display directory
 6cd $HOME_DIR
 8echo 'clear git cache'
 9git clean -df
11echo 'Pull remote code'
12git pull origin main
14echo 'Job Done!'%
  • The service runs on gunicorn
 1root@tokyo:/data/ systemctl status gunicorn
 2● gunicorn.service - gunicorn daemon
 3    Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
 4    Active: active (running) since xxxxx CST; 15min ago
 5Main PID: 2467813 (gunicorn)
 6    Status: "Gunicorn arbiter booted"
 7    Tasks: 5 (limit: 1036)
 8    Memory: 91.2M
 9    CGroup: /system.slice/gunicorn.service
10            ├─2467813 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
11            ├─2467814 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
12            ├─2467815 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
13            ├─2467816 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
14            └─2467817 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
  • Configure the Payload URL in the github repository, as shown in the figure below

    github webhook

  • push triggers the webhook, and then go to the background to view the results, see the following figure triggering successfully, and returns the result defined by the code

    github webhook result

Of course, there are tens of thousands of ways to update the website. You can update directly through scp ssh ftp rsync in ancient times. I don’t want to log in all the time because I’m stuck on a foreign server. Hang up, gitlab class also has webhook, no need to learn the syntax of action, personally think it is easy to use and controllable, complex dynamic site update, you can define extra logic by yourself

Lastmod: Friday, December 8, 2023