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 subprocess.run(["/bin/bash", script_name])
3
4
5@app.post("/api/update/mephistocc")
6async def update_mephisto_cc(background_tasks: BackgroundTasks):
7 background_tasks.add_task(update_site, script_name="update_mephisto.cc.sh")
8 return {"message": "update site in the background"}
- Super simple update script, just pull the main repository
1(.venv) ➜ stock.mephisto.cc git:(main) cat update_mephisto.cc.sh
2#!/bin/sh
3echo 'start executing webhook hook'
4
5HOME_DIR=/data/mephisto.cc #This directory is the server page display directory
6cd $HOME_DIR
7
8echo 'clear git cache'
9git clean -df
10
11echo 'Pull remote code'
12git pull origin main
13
14echo 'Job Done!'%
- The service runs on gunicorn
1root@tokyo:/data/mephisto.cc# 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/gunicorn.py api.main:app
11 ├─2467814 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app
12 ├─2467815 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app
13 ├─2467816 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app
14 └─2467817 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app
-
Configure the Payload URL in the github repository, as shown in the figure below
-
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
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
Copyright statement:
- All content that is not sourced is original., please do not reprint without authorization (because the typesetting is often disordered after reprinting, the content is uncontrollable, and cannot be continuously updated, etc.);
- For non-profit purposes, to deduce any content of this blog, please give the relevant webpage address of this site in the form of 'source of original text' or 'reference link' (for the convenience of readers).