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=""):
 ["/bin/bash", script_name])
       async def update_mephisto_cc(background_tasks: BackgroundTasks):
           background_tasks.add_task(update_site, script_name="")
           return {"message": "update site in the background"}
  • Super simple update script, just pull the main repository

       (.venv) ➜ git:(main) cat
       echo 'start executing webhook hook'
       HOME_DIR=/data/ #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/ 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/ api.main:app
                   ├─2467814 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
                   ├─2467815 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
                   ├─2467816 /usr/bin/python3 /usr/local/bin/gunicorn -c api/ api.main:app
                   └─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: Monday, August 28, 2023