Deploy - snake-npms/snake-on-koa GitHub Wiki

Use shipit-deploy

Client:

$ npm install -g shipit-cli
$ npm install --save-dev shipit-deploy
$ npm install --save-dev shipit-shared
  1. Client: Create a shipitfile.js at the root of your project
module.exports = shipit => {
	// Load shipit-deploy tasks
	require('shipit-deploy')(shipit)
	require('shipit-shared')(shipit);

	shipit.initConfig({
		default: {
			deployTo: '/mnt/www/app-name',
			repositoryUrl: 'https://github.com/user/super-project.git',
			keepReleases: 8,
                        ignores: ['.git', 'node_modules', 'logs', 'public', 'test', 'config/database.json', 'config/env.json'],
                        shared: {
				overwrite: true,
				dirs: ['logs', 'public', 'node_modules'],
				files: [
					'config/env.json',
					'config/database.json',
                                        'pm2.config.json'
				],
			}
		},
		staging: {
			servers: '[email protected]',
			branch: 'develop',
		},
		production: {
			servers: [{
				host: 'app1.myproject.com',
				user: 'john',
			}, {
				host: 'app2.myproject.com',
				user: 'rob',
			}],
			branch: 'master'
		}
	})
        
        // 监听部署事件,在部署中不同生命周期中插入任务
	shipit.on('deploy', function () {
	  shipit.on('published', async function () {
			await shipit.remote('npm install --production', { cwd: `${shipit.config.deployTo}/current`})
	                // await shipit.start('pm2:start');
	  })
	})

	// shipit staging pm2:start
	shipit.task('pm2:start', function() {
		console.log('======= - pm2 start - =======')
		console.log('--- before status ---')
		shipit.remote('pm2 list', {cwd: `${shipit.config.deployTo}/current`});
		console.log('--- run start or restart task ---')
		shipit.remote('pm2 startOrGracefulReload pm2.config.json', {cwd: `${shipit.config.deployTo}/current`});
		console.log('--- end status ---')
		setTimeout(() => {
			shipit.remote('pm2 list', {cwd: `${shipit.config.deployTo}/current`});
		}, 5000)
	})
}
  1. Server: config nginx, eg: your app run port is 3001
upstream my_nodejs_upstream {
    server 127.0.0.1:3001;
    keepalive 64;
}

server {
    listen 80;
    server_name my_nodejs_server;
    root /home/www/project_root;
    
    location / {
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	proxy_set_header Host $http_host;
    	proxy_set_header X-NginX-Proxy true;
    	proxy_http_version 1.1;
    	proxy_set_header Upgrade $http_upgrade;
    	proxy_set_header Connection "upgrade";
    	proxy_max_temp_file_size 0;
    	proxy_pass http://my_nodejs_upstream/;
    	proxy_redirect off;
    	proxy_read_timeout 240s;
    }
}
from [here](http://pm2.keymetrics.io/docs/tutorials/pm2-nginx-production-setup)
  1. Server: create pm2.config.json
{
  "name": "app-name",
  "script": "start.js",
  "cwd"   : "/mnt/www/app-name/current",
  "log_date_format": "YYYY-MM-DD HH:mm Z",
  "instances": 2,
  "max_memory_restart": "300M",
  "merge_logs": true,
  "exec_mode": "cluster",
  "kill_timeout" : 5000,
  "ignore_watch": ["node_modules", "logs", "public"],
  "watch_options": {
    "followSymlinks": false
  },
  "error_file" : "./logs/pm2-err.log",
  "out_file"   : "./logs/pm2-out.log",
  "env": {
    "NODE_ENV": "production"
  },
  "args": [
    "--color"
  ]
}

more pm2 info look here 部署

$ [npx] shipit staging deploy
$ [npx] shipit staging pm2:start