Application scenarios

If you want to do a local Git push to a Git server, then pull code directly from the target server and deploy it, check out this tutorial.

background

UI students usually put the material package in the Git. UI Git repository. After several iterations, the size of the entire repository has reached 1.7 GB, and it is very difficult to clone each time.

Until one day, someone in the group suggested that you could hang directly on the service and access it directly…

Train of thought

To break down this requirement, there are two points to achieve:

  1. Deployed on the target machine, static files can be accessed.
  2. When the warehouse is pushed, the target machine is automatically notified to executeStep 1.

Further split, from these two perspectives, the first point has many implementation schemes, listed here several implementation schemes:

  1. Mount to the target node, use Node to set the static file path, using the framework to generate the corresponding file directory.
  2. Upload the static content of the project to the CDN and generate all the static content into the file index entry.
  3. When users access a Url, stash API pulls relevant directory data and generates directory structure or static file to spit out to users to update the target file in real time.

Here, take the first scheme as an example to realize it concretely.

Therefore, the whole process needs to be divided into three parts: client (new change push), Git server (Git mount server), and target server (automatic deployment server). Considering the second point, the message is automatically triggered when the warehouse is pushed. The natural solution is to use Githook (see this article for an introduction to Githook). However, both client and server hooks in Githook can only be bidirectionally linked between client and server, so you need to use the Webhook function of Git server (Webhook introduction). The whole process is shown as follows:

implementation

From the above workflow, the Node service needs to distinguish the types of requests:

  • GET request: Static file redirection based on the URL
  • POST request: Receives a Webhook request, executes the script, and pulls the Git repository

For static file deployment, two NPM packages were investigated: ST and serve-Handler. Both can implement static resource deployment. However, after testing, ST does not support Chinese directory names, resulting in garbled characters. Serve-handler supports Chinese directories better, so serve-handler is selected.

As for executing the script pull repository, there are two recommended schemes. One is child_process, which is used to execute scripts or command lines. The other is third-party shellJS, which is encapsulated on the basis of child_process and calls system commands more easily. So choose the second option.

Therefore, the specific code is as follows:

var http = require('http'); Var handler = require('serve-handler'); Var shell = require('shelljs');

http.createServer(function(request, response) {
  var urlMethod = request.method;
    if(urlMethod === 'POST') {// Get the POST request, print the content, and execute git pull on console.log("POST request");

      var content = "";
      request.on('data'.function (chunk) {
        content += chunk;
      });

      request.on('end'.function () {
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("You've sent: " + content);
        response.end();
        console.log("POST data:",content); }); Try {// pull the latest code shell. Exec ("git pull");
        console.log("POST success");
      }catch(e) {
        console.log("POST fail");
      }
      return;
    }else{// GET request, call serve-handler directly to static resource redirection console.log("GET request");
    	console.log("GET url", request.url);
    	return handler(request, response, {
 		  "public": "git.ui"
   		});
    }
}).listen(8080);

console.log('Server started');
Copy the code

Pm2 is used to deploy services on the target server, and the trigger address of Webhook is set on the Git server to complete the deployment of the target server.

subsequent

You can then register services and domain names, bind machines to domain names, or configure Nginx mappings using existing services.

If you are interested, try implementing the second and third deployment scheme for accessing static files.