“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Why write this article

Recently, I used Ant-Design-Pro and KOA2 to build a set of background management system for managing personal projects. Since you need to deploy koA2 code to a server, pM2 is a natural choice to publish and manage. In fact, I have used PM2 in my work before, but I used it in the environment set up by others, without a good grasp of the essence. In the practice of deploying a personal project, I control the whole deployment process from beginning to end, so there are a lot of harvest, so I will prepare to write this article, and put what I have learned into writing, which is not only convenient for me to review later, but also can help friends who want to understand this knowledge.

I met pm2

First of all, we need to know what m2 is. Pm2 is a monitoring and management tool for node applications. It has the following functions:

  1. Running our Node application in the background as a daemon
  2. Provides an easy way to start or stop node applications
  3. Monitors the running status of node applications and automatically restarts the node application when it crashesnode app.js!)
  4. After cluster mode is enabled, node applications run on multiple cpus based on load balancing to maximize the performance of multi-core cpus
  5. With nerf.config.js, one-click deployment is easy

The common pm2 commands are as follows:

Pm2 start app.js --name my-app # Cluster mode pm2 start app.js --name my-app # Cluster mode pm2 start app.js Pm2 start app.js -i Max # # Listing pm2 list # lists all current node applications in the form of a table pm2 jlist # Lists all current node applications in the form of native JSON pm2 prettylist # Pm2 describe 0 # Logs pm2 Logs [--raw] # Flush # Pm2 reloadLogs # Reload all log files # Actions pm2 stop all # Stop all node applications pm2 restart all # Restart all node applications pm2 reload all # Reload all node applications, Pm2 stop 0 # restart node 0 # restart node 0 # delete node 0 # delete node 0 Pm2 delete all # Delete all node applicationsCopy the code

Deploy node applications using pM2

In addition to operating pm2 directly on the command line as described above, a more common way to use it is to use the convention configuration file (nerf.config.js) to manage it. With this file, we can easily and efficiently manage and distribute multiple Node applications. The common configurations are listed below:

Module. Exports = {module. Exports = {module. Exports = {module. './ SRC /index1.js', // specify node application entry file exec_mode: 'cluster', // Enable cluster mode instances: '0', // Run the maximum number of Node instances based on the number of available CPU cores watch:true, // Enable listening mode. Ignore_watch: [], // List of files ignored in listening mode: {// the default environment variable NODE_ENV: "production", MODE: "default"}, env_production: Env production NODE_ENV: "production", MODE: "pro"}, env_pre_production: Env pre_production NODE_ENV: "pre_production", MODE: "pre"}},{name: 'my-app2', script: './ SRC /index2.js', exec_mode: 'fork', // The default mode, which only runs node applications on single-core cpus, is not recommended to use env: {NODE_ENV: "production", MODE: "default" }, env_production: { NODE_ENV: "production", MODE: "pro" }, env_pre_production: {NODE_ENV: "pre_production", MODE: "pre"}}], // Deploy: {// deploy to the production environment. {user: 'root', // server username host: '8.8.8.8', // server IP address ref: 'origin/main', // repository branch to pull repo: '[email protected]:test/test.git', // repository address path: '/var/test', // need to publish to the server path 'pre-setup': ', // command 'post-setup' to be executed before the installation process starts: "NPM install && pm2 start nery.config.js --env production", // execute the 'pre-deploy' directive after pull the code: '', 'post deploy', 'post deploy', 'post deploy', 'post deploy', 'post deploy', 'post deploy', 'post deploy', 'post deploy', 'post deploy', 'post deploy' 'pm2 reload nery.config. js --env production'}, // preproduction: {user: 'root', host: '8.8.8.8', ref: 'origin/main', repo: '[email protected]:test/test.git', path: '/var/test', 'pre-setup': '', 'post-setup': "npm install && pm2 start ecosystem.config.js --env pre_production", 'pre-deploy': '', 'post-deploy': 'pm2 reload ecosystem.config.js --env pre_production' } } }Copy the code

The complete directive for deploying with pm2 is pm2 deploy
, and the default

is nery.config.js, So you can omit this parameter. The deploy key identifies different publishing environments and specifies the instructions to be executed. For example, to publish to a pre-production environment, the instruction is pm2 deploy pre_production. The important thing to note here is that for the first release, pm2 deploy pre_production setup needs to be executed to go through the pre-setup and post-setup hooks and execute the initial installation command that we setup. It is also important to note that because of the behavior of pulling down the repository code, we need to configure SSH no-password login for the server in order to successfully pull down the code. After publishing, you can log in to the server, type pm2 list, and check the running status of the node application. If the node application is online, congratulations!

conclusion

In fact, CI/CD involves a lot of knowledge. Although many problems may be encountered in the process of practice, only through continuous practice can we truly understand the essence of ci/ CD. In the future, I will try more CI/CD tools, and at the same time, I will write and share what I have learned to help people in need.

Personal blog

www.carlblog.site has recently set up a personal blog, which will regularly publish articles about the summary of technical experience. I hope you are interested in it and don’t run away! ~