Use Github webhook for automatic deployment

Github provides webhook functionality, which basically means that when you submit code, Git detects that you have made a push and can call up a URL you know about.

What does this feature do? For example, when a personal blog post is submitted, Github should immediately update the server version, this time Webhook can easily handle this matter.

This article mainly explains how to set up the whole process

Set the Github pull permission

You can configure the github access permission in either of the following ways: Directly add the Github account and password to the global configuration file of the server, or use the sshkey

Configuring the Account Password

At the root of the server

touch .git-credentials
vi .git-credentials
#The inputHTTP (s)://{your username}:{your password}@ your server addressCopy the code

The input terminal

git config --global credential.helper store
Copy the code

Look at ~/.gitconfig for an extra line

vi .gitconfig

[credential]
helper = store
Copy the code

Git-credentials are written to ~/.git-credentials when you enter the git-credentials file for the first time

Sshkey way

  1. Check whether the sshkey exists
cd ~/.ssh
ls
Copy the code

If it exists, you can use it directly, or you can create one. If you create a new one, you can simply configure one more line in the ~/.gitConfig file.

  1. createsshkey
ssh-keygen -t rsa -C "[email protected]"
Copy the code

After you press Enter, you are asked to enter the password. You can also enter the password based on security requirements. If you enter a password, you will be asked to verify the password when using the secret key.

Code parameter meaning:

  • -tSpecifies the key type. Default isrsaCan be omitted.
  • -CSet comment text, such as email.
  • -fSpecifies the name of the key file store. Can be omitted, running the above command will prompt you to enter a filename, otherwise the default isid

Two files, id_rsa and id_rsa.pub, are generated. One is the private key and the other is the public key.

If the file name is specified, you need to create a config file in the ~/.ssh directory and type it

Host github.com www.github.com
  IdentityFile ~/.ssh/othername
Copy the code
  1. Set the contents of public key information. Copy all contents in ID_rsa. pub and set the SSH key in Github, Settings, SSH and GPG keys

  2. Verify sshkey

ssh -T [email protected]
Copy the code

If you enter a password when generating the rsa key, you will be asked to verify the password.

At the end of the sentence you will see the following, indicating success. If access denied is denied, access is denied.

Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Copy the code

Now you can pull the code, try it, you don’t have to verify the account password.

However, it is still possible to verify the account password if possible. In this case, you need to make sure that the address you are using is not SSH address, if it is HTTPS then it will not be able to use sshkey. With git Clone HTTPS, the project will still be prompted when it is pulled to the local location. In this case, you need to modify the git configuration of the project.

Modified. The git/config

Url = https://github.com/Name/project.git a line to the url = [email protected]: Name/project. The git.Copy the code

You are advised to set your Git account and email address

git config --global user.name "name"
git config --global user.email "[email protected]"
Copy the code

Write webhook services

After trying to run webhook service using open source project Adnanh/Webhook failed, I think it is too rash to try to write and use Java. It is only a listening service, there is no need to use Java, such things as Python \node\ Go are very appropriate.

At first, I considered using Express to monitor services. The test was basically successful, but when I parse requests sent from Github, I found that parsing the Payload was very difficult. Behind and found an open source good thing github.com/rvagg/githu… This will help us parse the data github sends to us and automatically subcontract processing, such as push issues, etc.

Following the example provided by Rvagg/Github-webhook-Handler I changed it briefly; Creating multiple handlers is also described in issues. I’ve done a simple wrapper on this example.

var handlerOption = [
  { 
    path: '/hooks/publish_tennetcn'.secret: 'yoursecret'.shell: 'sh /home/publish/tennetcn/publish.sh'}]const currOption = handlerOption.filter(item= > item.path === url) || []
  if(currOption.length <= 0) {console.log('current url is not matchs')}else{
    exec(currOption[0].shell, function(err,stdout,stderr){
      if(! err) {console.log(stdout); }})}Copy the code

This way, when you use it, you only have to focus on handlerOption, which is very convenient. The project has been open source, open source address github.com/chfree/thin… Thought we could add a little star

Using projects is very simple

git pull
npm install
sh run.sh &
Copy the code

If necessary, modify the log path in run.sh and port and handleOption in index.js

Write a script

Script writing, relatively simple, is a simple shell. The main operations are as follows:

  1. Set the shell and working directory
  2. Start pulling code
  3. Compile and build, which should be used carefully when packagingnpm install --unsafe-permOtherwise it will be reportednode-sassPermission problems
  4. Compress and back up existing projects to make backups a good habit
  5. Copy the package path to the project release path

The complete shell is as follows:

#! /bin/bash
PRJ_WORK_DIR=/home/proj_source/think-vp-tennetcn

cd $PRJ_WORK_DIR

echo 'start think-vp-tennetcn update and publish'

echo 'git pull ...... '
git pull


echo 'npm install ...... '
#npm install --production --unsafe-perm=true --allow-root
npm install --unsafe-perm

echo 'npm run docs:build'
npm run docs:build

echo 'dist success'

echo 'copy starter'

BAK_DIR=/home/project/tennetcn/clientbak
PUB_DIR=/home/project/tennetcn/client
DATE=$(date +%Y-%m-%d-%H-%M-%S)

echo 'bak start '
zip -r $BAK_DIR/backup-tennetcn-$DATE.zip $PUB_DIR/*

rm -rf $PUB_DIR/ *echo 'bak end delete end'

cp -r $PRJ_WORK_DIR/docs/.vuepress/dist/* $PUB_DIR

echo 'cp end'

echo 'publish success'
Copy the code