What is a simple deployment tool suited for nodejs

May 14, 2018 1.1k views
Node.js Deployment Ubuntu

I am just starting with nodejs and have a small app to test on an Ubuntu server. What is the "standard tool" to deploy the app and install the dependency modules? looking for the simplest one, just copy the files and install the modules. Thanks for any insight!

2 Answers

I use Capistrano for NodeJS deployments.

For starters, here's how one Cap job looks:

(my example uses Chef to find nodes to deploy to)

require "capistrano/chef"
require "chef/node"

set :user, "DEPLOY_USER_GOES_HERE"
set :chef_environment, ENV['CHEF_ENVIRONMENT'] if ENV['CHEF_ENVIRONMENT']

if fetch(:chef_environment).nil?
  abort "CHEF_ENVIRONMENT environment variable not set. Please set it and try again."
end

set :service_name, ENV['SERVICE_NAME'] if ENV['SERVICE_NAME']

if fetch(:service_name).nil?
  abort "SERVICE_NAME environment variable not set. Please set it and try again."
end

service = fetch(:service_name)
chef_env = fetch(:chef_environment)
stripe_key = fetch(:stripe_keyname)
stripe_account = fetch(:stripe_account)

chef_role :node_service, "roles:node-#{service} AND chef_environment:#{chef_env}", primary: true, attribute: "ipaddress"
set :application, service
set :repo_url, "https://GITHUB_KEY_GOES_HER:@github.com/USER_OR_ORG_GOES_HERE/#{service}.git"

set :gitBranch, ENV['BRANCH'] if ENV['BRANCH']
gitHubBranch = fetch(:gitBranch)
set :branch, gitHubBranch unless gitHubBranch.nil?

if gitHubBranch.nil?
  abort "BRANCH environment variable not set. Please set it and try again."
end

set :deploy_to, "/install_path/cap_#{service}/"
set :scm, :git
set :keep_releases, 5
set :linked_files, %w{.env}
set :default_env, {
  NODE_ENV: 'production'
}

namespace :deploy do

  desc 'Install the packages before running the build'
  task :install_npm_packages do
    on roles(:node_service) do
      execute "cd #{release_path} && npm install"
    end
  end

  desc 'Run the build before deploying'
  task :predeploy_build do
    on roles(:node_service) do
      execute "cd #{release_path} && npm run build:prod || npm run build"
    end
  end

  desc 'run tests'
  task :run_tests do
    on roles(:node_service) do
    execute "cd #{release_path} && npm run test"
    end
  end

  desc 'run lint'
  task :eslint_code do
    on roles(:node_service) do
      execute 'name' do
        command "cd #{release_path} && eslint **/src/**/*.js"
      end
    end
  end
  desc 'Prune packages for production'
  task :prune_production do
    on roles(:node_service) do
      execute "cd #{release_path} && npm prune --production"
    end
  end

  desc 'Restart service'
  task :restart_service do
    on roles(:node_service) do
      execute "sudo service #{service} restart"
    end
  end
end

after "deploy:run_tests", "deploy:prune_production"
after "deploy:install_npm_packages", "deploy:predeploy_build"
after "deploy:predeploy_build", "deploy:run_tests"
before "deploy:symlink:release", "deploy:install_npm_packages"
after "deploy", "deploy:restart_service"

If you need help, let me know.

Have another answer? Share your knowledge.