GO uses Webhook to automate github deployment

Usually most of your development is submitted by local git push, and git pull is updated manually on the server.Git can be deployed automatically using a webhook.
A webhook is a hook event for a warehouse platform that listens for code and calls back notifications (the notification address is the webhook address you fill out in each git warehouse platform.
Usually in one of your project repositories such asMyproject.gitSettings inside) Address filled out by warehouse platform.
In fact, the principle flow is:

So the main way to automate deployment is:

-Modify code push

- GitHub (other warehouse platforms) sends requests to your web server

-Site server receives update request and executes Auto-Deployment script

-Auto-deploy script executes actions such as code pulling, packaging, modifying files to complete the update and deployment of the website

1. Download the github-webhook tool:

wget http://img.sgfoot.com/github-webhook1.4.1.linux-amd64.tar.gz

2. Run github-webhook installation

tar -zxvf github-webhook1.4.1.linux-amd64.tar.gz
cp github-webhook /usr/bin/
chmod u+x /usr/bin/github-webhook

3. Operation mode

Default port: 2020, can be changed, http route: /web-hook
 Valid access address:http://ip:2020/web-hook
 -b is a shell script path parameter
 -s is the password set by github webhook

Parameter description:

#Non-background Run
 Github-webhook-b [shell script path]-s [password set by GitHub webhook]

#Background Run
 Nohup github-webhook-b [shell script path]-s [github webhook set password] & 

#Directed Log Output
 Nohup github-webhook-b ~/sh/your script.sh-s hook password > ~/logs/Webhook.log2>&1 &

Feature introduction

  • Run binaries directly
  • Custom script path
  • Custom Password
  • Custom ports. 0 ~ 65535
  • Quiet mode
   --bash value, -b value    Execute the script path. eg: /home/hook.sh
   --port value, -p value    http port (default: 2020)
   --secret value, -s value  github hook secret
   --quiet, -q               quiet operation (default: false)
   --verbose, --vv           print verbose (default: false)
   --help, -h                show help (default: false)
   --version, -v             print the version (default: false)
   --bash value, -b value    Execute the script path. eg: /home/hook.sh Custom script
   --port value, -p value    http port (default: 2020) Custom Port,Default 6666
   --secret value, -s value  github hook secret Custom Password, Empty is not allowed
   --verbose, --vv           print verbose (default: false) Print more details
   --quiet, -q               quiet operation (default: false) Quiet mode,Default shutdown. -q open,No information output
   --help, -h                show help (default: false) 
   --version, -v             print the version (default: false)

The use of several parameters is described above.Okay, back to the point:

Deployment scripting. The main purpose of this shell script is to pull code from github. The script content is simple, just make a brief judgment of the directory.

Code catalog is updated if it exists, clone warehouse if it does not exist. Replace working catalog with warehouse name and address with your own.

cd ~/  
mkdir sh 
vim webhook.sh



cd /www/wwwroot/Golang/src

if [ ! -d "easy-gin" ]; then
  git clone https://github.com/fantasylxh/easy-gin

cd easy-gin
git pull

Configure github webhook

  • Fill in the address of your server, http://ip:2020/web-hook
  • The password set must match the password set by the server running github-webhook-s.



Start the local sh script:
github-webhook -b ~/sh/webhook.sh -s webhook123 >> ~/logs/webhook.log 2>&1 &


Verify that the githup webhook connection is working:

Test whether the webhook is valid

After git push, you can see the information that github pushes, for example, I added a file to the local push:

After Commit->push, let's see if the server code is automatically updated:


Here it is: webhook communication is normal.The sh script does not validate the current branch, it only makes a simple pull, can force updates with -f, and interested blogs can perfect the sh.

Welcome to the brick message.

Tags: Go github git shell Linux

Posted on Sun, 24 May 2020 09:52:58 -0700 by Joost