Principle: The local Git repository is associated with the remote repository (github, code cloud, etc.), and pM2 logs in to the server according to the specified configuration and pulls the code update of the remote repository.
1. Environment construction
Install the pm2
npm install pm2@latest -g
Copy the code
Official website documents:Pm2. Keymetrics. IO/docs/usage /…
2. Create a local project and associate it with a remote repository
1. Create a local repository
git init
2. Create a file
let express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
var proxy = require('http-proxy-middleware');
app.use('/ajax', proxy({
target: "http://m.maoyan.com/",
changeOrigin: true
}));
app.listen(3000,function(){
console.log('server [email protected]:3000')})Copy the code
Using the app. Use (express. Static (path. Join (__dirname, ‘public’))); Use the public tree as a static file directory
3. Create a remote repository on Github
Created warehouse address:
[email protected]:cooleye/daviesite.git
Copy the code
4. Associate with the remote warehouse
git remote add origin [email protected]:cooleye/daviesite.git
Copy the code
3. Configure the pM2 automatic deployment file
pm2 ecosystem
Copy the code
Json file is generated in the current directory of the command to edit file. json:
{
"apps":[
{
"name": "daviesite"."script": "index.js"."env": {
"COMMON_VARIABLE": "true"
},
"env_production": {
"NODE_ENV": "production"}}]."deploy": {
"production": {
"user":"root"."host": ["47.108.78.24"]."port": "22"."ref": "origin/master"."repo": "[email protected]:cooleye/daviesite.git"."path": "/mnt/daviesite"."ssh_options": "StrictHostKeyChecking=no"."pre-setup": "echo 'This is a pre-setup command'"."post-setup": "ls -la"."pre-deploy-local": "echo 'This is a pre-deploy-local command'"."post-deploy" : "npm install && pm2 start 0"}}}Copy the code
Parsing configuration files
- User: indicates the login user name
- Host: indicates the target server or domain name to be deployed
- Ref: Branches used when deploying code
- Repo: Git repository address
- Path: indicates the address of the file directory deployed on the target server
- Post-deploy: indicates the script to be started after deployment
4. Configure the server environment
1. Install the PM2 on the server
npm install pm2@latest -g
Copy the code
2. SSH public and private keys
Generate SSH public and private keys on the server
ssh-keygen -t rsa -C "youremail"
Copy the code
3. View the public key:
cat ~/.ssh/id_rsa.pub
Copy the code
4. Add the public key to Github
5. Upload files in the future/mnt/daviesite
Check whether the write permission is available. If no, add the write permission
Deployment of 5.
1. Push native code to Github
git add .
git commit -m 'update'
git push origin master
Copy the code
2. Configure the encryption-free login
SCP ~ /. SSH/id_rsa. Pub [email protected]: / root /. SSH/authorized_keysCopy the code
3. Install and deploy in remote mode
pm2 deploy ecosystem.json production setup
Copy the code
After this step, the server will clone the code on Github and run it using PM2
Deployment of 4.
pm2 deploy ecosystem.json production
Copy the code
When you do this, the server installs dependencies according to package.json
Update 5.
pm2 deploy production update
Copy the code
Update the code
In practice, I find that the update is not successful, but it is ok to execute this sentence on the server side, but I do not want to execute this sentence on the server side, I need to update locally. To do this, modify file.json
"post-deploy" : "git pull origin master && npm install && pm2 start 0"
Copy the code
The update is downloaded from Github when the PM2 deploy file.json production is executed
6. Rolled back
pm2 deploy production revert 1
Copy the code
7. Mandatory submission
pm2 deploy production --force
Copy the code
8. Modify the package. Json
Modify the package.json configuration file by adding the command:
{
"name": "server"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
"start": "pm2 start index.js"."sp": "pm2 deploy ecosystem.json production setup"."dp": "pm2 deploy ecosystem.json production"."ud": "pm2 deploy production update"."test": "echo \"Error: no test specified\" && exit 1"
},
"author": ""."license": "ISC"."dependencies": {
"express": "^ 4.17.1"."http-proxy-middleware": "^ 0.19.1." "}}Copy the code
In this way, later installation and deployment can be performed
npm run sp
Copy the code
Update deployment executes:
npm run dp
Copy the code