preface

Develop a NodeJS project locally, and if you want to show it to others, you generally need to deploy it on the server. If you are using github or coding, just clone the project and start your NodeJS project using PM2 after installing the server environment and git.

However, if I update the code to the remote repository and the server is the same old code, you still need to log in to the server and use git to pull down the latest code and restart the project again. Over time, if code changes are frequent, having to log in to the server each time and deploy manually is not a good idea.

It just so happens that we are using some code managed services, and WebHook is such a great thing, why not use it?

Write automatic deployment scripts

After we decided to use WebHook for automatic deployment, we needed a script to execute some commands. On a Linux server we can use shell to write the automatic deployment script. Here I call the file auto_build.sh:

#! /bin/bash

git reset --hard origin/master
git clean -f
git pull origin master
npm install
npm run test
npm run startCopy the code

The above shell script will run the command we have written, so that the first step to write automatic deployment script is done, if you are not familiar with shell students can see: Shell scripting 30 minutes to get started

Processing WebHook

If we want to use WebHook, we also need to start an additional HTTP service in our project to handle the receiving requests. Assuming that we use Coding to manage our code, we can use the coding-webhook-handler package.

Coding-webhook-handler:

npm i -S coding-webhook-handlerCopy the code

Then we create a new js file named webhook.js with the code shown above. We use coding-webhook-handler to start the HTTP service separately, and then we listen for push and execute the callback function:

const http = require('http') const createHandler = require('coding-webhook-handler') const handler = createHandler({ path: '/', token: }) http.createserver ((req, res) => {handler(req, res, function(err) { res.statusCode = 404 res.end('no such location') }) }).listen(7777) handler.on('error', err => { console.error('Error:', err.message) }) handler.on('push', event => { console.log(event) })Copy the code

Having written the basic code, we then need to run our.sh file to implement automatic deployment. Write a function that can run the command. The function is also written in webhook.js:

. const rumCommand = (cmd, args, callback) => { const child = spawn(cmd, args) let response = '' child.stdout.on('data', buffer => response += buffer.toString()) child.stdout.on('end', () => callback(response)) } ...Copy the code

Handler. On (‘push’, event => {… }) callback function:

handler.on('push', event => {
  rumCommand('sh', ['./auto_build.sh'], txt => {
      console.log(txt)
  })
})Copy the code

Ok, we have webhook.js code written, then we need to use nginx to forward the port. Then configure Webhook on Coding.

Configure Coding WebHook Settings:

First of all, we need to enter the page of your coding warehouse, and then click “Settings” in the left sidebar to enter the project setting page. Then you can see the button of “WebHook”, and click “Go to the page of” WebHook “.

And then we can see on the right:

Then click New Hook to enter a page:

  • Url: is the domain name that accesses your webhook.js startup service.

  • Token: not required. If the token is used, it will be specified in createHandler.

Then you can choose which events you want to listen for. Here we’re just using it to automate the deployment of the project, just listen for the push.

Once you have set up the WebHook for your project, you should also set the deployment public key so that you do not need to enter the password when pulling code and clone the repository to the server using SSH. Then start the project service and start the webhook.js service.

conclusion

If you don’t want to have to manually log in to the server every time you update your code and use some code hosting service, you can use WebHook to implement a simple automatic deployment, which leaves a lot of manual deployment time.

If you are using github to manage code, you can use Github -webhook-handler, and then go to github project Settings to find webhook Settings.

If you have a better solution, let me know in the comments!!