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
def update_site(script_name=""): subprocess.run(["/bin/bash", script_name]) @app.post("/api/update/mephistocc") async def update_mephisto_cc(background_tasks: BackgroundTasks): background_tasks.add_task(update_site, script_name="update_mephisto.cc.sh") return {"message": "update site in the background"}
-
Super simple update script, just pull the main repository
(.venv) ➜ stock.mephisto.cc git:(main) cat update_mephisto.cc.sh #!/bin/sh echo 'start executing webhook hook' HOME_DIR=/data/mephisto.cc #This directory is the server page display directory cd $HOME_DIR echo 'clear git cache' git clean -df echo 'Pull remote code' git pull origin main echo 'Job Done!'%
-
The service runs on gunicorn
root@tokyo:/data/mephisto.cc# systemctl status gunicorn ● gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled) Active: active (running) since xxxxx CST; 15min ago Main PID: 2467813 (gunicorn) Status: "Gunicorn arbiter booted" Tasks: 5 (limit: 1036) Memory: 91.2M CGroup: /system.slice/gunicorn.service ├─2467813 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app ├─2467814 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app ├─2467815 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app ├─2467816 /usr/bin/python3 /usr/local/bin/gunicorn -c api/gunicorn.py api.main:app └─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 on this site 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).