使用Shipit+Gulp+PM2+Nginx自动部署Nodejs应用

Shipit自动化部署

Shipit的使用类似capistrano,先本地全局安装

npm install -g shipit-cli  
npm install -g gulp


# 这里用的几个插件
npm install shipit-deploy     --save-dev # 默认的部署任务  
npm install shipit-npm         --save-dev # 自动在服务器上运行 npm install  
npm install shipit-pm2         --save-dev # 使用pm2启动应用,下面会说明  
npm install shipit-shared     --save-dev # 指明哪些文件/文件夹是共享的,比如上传文件夹

shipitfile.js内容如下:

module.exports = function (shipit) {  
  require('shipit-deploy')(shipit);
  require('shipit-npm')(shipit);
  require('shipit-pm2')(shipit);
  require('shipit-shared')(shipit);

  shipit.initConfig({
    default: {
      repositoryUrl: 'git@github.com:foo/bar.git',
      ignores: ['.git', 'node_modules'],
      rsync: ['--del'],
      keepReleases: 3,
      workspace: './.deploy_build',
      shallowClone: true,
      shared: {
        overwrite: true,
        dirs: [],
        files: []
      }
    },

    staging: {
      deployTo: '/tmp/staging_path',
      servers: 'deploy@staging_server.com',
            branch: "dev",
      pm2: {
        json: "app_staging.json"
      }
    },
    production: {
      deployTo: '/tmp/production_path',
      servers: 'deploy@production_server.com',
            branch: "release",
      pm2: {
        json: "app_production.json"
      }
    }
  });


   shipit.blTask("gulp_deploy", function () {
    shipit.log('gulp_deploying');
    return gulp_clean()
      .then(gulp_build)
      .then(cp2deploy_build)
      .then(function () {
        shipit.log('gulp_deployed');
        shipit.emit('gulp_deployed');
      })
  });

  function gulp_clean() {
    shipit.log('gulp_cleaning.');
    return shipit.local( "gulp clean")
      .then(function () {
        shipit.log('gulp_cleaned.');
      });
  }

  function gulp_build() {
    shipit.log('gulp_building.');
    return shipit.local("gulp build --env=" + shipit.environment)
      .then(function () {
        shipit.log('gulp_built.');
      });
  }

  function cp2deploy_build() {
    shipit.log('copy public to deploy_build.');
    return shipit.local("cp -r ./public ./.deploy_build/")
      .then(function () {
        shipit.log('files copied.');
      });
  }

  // listen deploy:fetch event and start build task
  shipit.on("fetched", function(){
    shipit.start("gulp_deploy");
  });

};

PM2

# 看详情
pm2 show 0

# 看log
pm2 logs [app_name]  

app.json来配置运行app

{
  "apps": [
    {
      "name": "shijie_frontend", 
      "script": "app.js",
      "args": "--env=production",
      "instances": 1,
      "cwd": "/tmp/production_path/current",
      "env": {
        "NODE_ENV": "production",
        "PORT": "9001"
      }
    }
  ]
}

然后用pm2来启动,我们已经用shipit-pm2来让他在最后更新完代码后自动启动,所以这里不用手动启动。

pm2 startOrRestart app.json  

相应的Nginx配置

server {  
    listen 80;

    server_name your_domain.com;

    location / {
        proxy_pass http://127.0.0.1:001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        add_header Access-Control-Allow-Origin *;
    } 
}

Guimin Lin

Read more posts by this author.