How to use Github hook for automatic deployment

Posted by CodeMama on Sun, 26 Apr 2020 04:50:10 +0200

Recently, I bought domain name and server by chance. It's not really a waste. In addition, I haven't owned my own personal website, so I plan to play on the server with hexo, so I don't have to worry about whether to use Github pages or Gitee pages. Of course, today's topic is not blog building, but how to use Github's hook to deploy blog code to the server.

After all, GitHub's hook has a long history, and there are many open source projects available online, so instead of making wheels, I went to look for 5K star's open source Go project webhook, the role of this tool is to receive Github warehouse changes notification, and then call your configured shell script, script can write code pull command or compile operation, and so on, according to individual needs. In short, it only works to get GitHub to work with your server.

webhook tool installation

Because webhook is developed in Go language, you need to install Go language first.

yum install -y golang

Then you can install webhook with the go command.

go get

The command installation location can be viewed through go env. GOPATH is the command installation path. For example, my command is installed in / root/go/bin/webhook.

go env

Generate ssh key

Before writing the script, make sure that the server has permission to pull github code. If you have configured it, skip this section Deployment scripting . ssh key is a code hosting platform (github, gitee, coding, gitlab, etc.) to identify whether you have the right to pull the identity of the code. It only needs one line of command and one way of carriage return to generate.


Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/
The key fingerprint is:
SHA256:M6sCf/J/hOu3zLxMkFUVmv3iWIa30CfbxiWqmWCt1YE root@iZwz96y36tk2ecnykzituxZ
The key's randomart image is:
+---[RSA 2048]----+
|            ..o. |
|           . o   |
|          . o    |
|       . o .     |
|      E S.  .    |
|  .  . ..Oo ..   |
|   oo o ==Boo .  |
|   .++.+o#== .   |
|    .=*+=+@o     |

After generation, you can view it through the command cat ~/.ssh/, and finally add the key to github. The addition will not be repeated. Please Google yourself.

cat ~/.ssh/

ssh-rsa AAAAB3NzaC1yc2EAAAADAQHBAAABAQCv7LGVJUFdcLL+HZyRFTQIQCdre61Gch76lDVpmWSX9BGGRU3iQS7EU5qApFn1VSvt+yf4rMt2LEkuxGCm1wIyBKZ6LYDViZBeTAfx4BcM1mcpxOX6I/+r07mQ4llTz+poQB1Zp9Y60uk0tbGOVWlCoDBEvf9qeEnQ0qEczEkv7wcawV6pVhlXjFKZgq0EOQbCYoWMvPUl+dwDbTcl/h+7At1nlgfF7IuRHlKf18qvgnTRT2wpiuz4pWdoAi8LcY1JiR1z5OB0oCJ2euhyDND39G2NxZRS1FIVdgCEvioHtdoHOSoWBlcSj0fLFSnscBfRBrCd7yhOP7fFKfrowHMj root@iZwz96y36tk2ecnykzituxZ

Deployment scripting

The main purpose of the shell script is to pull the code from github. The content of the script is very simple. It only makes a brief judgment of the directory. If the code directory exists, it will be updated. If it does not exist, it will clone the warehouse. Please change the working directory and the name and address of the warehouse to your own.


cd /home/www/website

if [ ! -d "go-home" ]; then
  git clone

cd go-home
git pull

webhook configuration and startup

Write the configuration file hooks.json in the following format.

    "id": "deploy-webhook",
    "execute-command": "",
    "command-working-directory": "/home"
  • id: the id of the hook, which can be customized
  • Execute command: the name of the script to be executed is the deployment script just written
  • Command working directory: the directory where the script is located

After the completion of the webhook command, you can see that the configuration with the id of deploy webhook has been loaded. We need to pay attention to the listening port and path, which will be used later.

/root/go/bin/webhook -hooks hooks.json -verbose

[webhook] 2020/04/22 15:18:22 version 2.6.11 starting
[webhook] 2020/04/22 15:18:22 setting up os signal watcher
[webhook] 2020/04/22 15:18:22 attempting to load hooks from hooks.json
[webhook] 2020/04/22 15:18:22 found 1 hook(s) in file
[webhook] 2020/04/22 15:18:22   loaded: deploy-webhook
[webhook] 2020/04/22 15:18:22 serving hooks on{id}
[webhook] 2020/04/22 15:18:22 os signal watcher ready{id}

Github Webhooks configuration

Now that the server has started the webhook program to listen to the 9000 port, you just need to tell Github the address and port.

Open the warehouse settings page and add webhook.

Configure webhooks. The Payload URL is the address to be notified. Fill in the port and path just printed out. Other defaults.

Now you can submit the code test. If the push fails, there will be an error prompt in Github. Similarly, the success can be seen not only in Github, but also in the server's print log.

I'm pingye. This is an open source project focusing on Gopher technology growth 「go home」

Thank you for watching. If you think the article is helpful, please pay attention to the official account of "Ping Yi" and focus on the principle of Go language and technology.

Topics: Go github ssh git JSON