nvm support for Capistrano 3.x
Add this line to your application's Gemfile:
gem 'capistrano', '~> 3.1'
gem 'capistrano-nvm', require: false, github: 'BuyerQuest/capistrano-nvm'
And then execute:
$ bundle install
Require in Capfile
to use the default task:
require 'capistrano/nvm'
The task then uses command maps to run nvm.sh
when necessary, as controlled by the setting :nvm_map_bins
.
In your capistrano config/deploy.rb
:
:nvm_node
- Accepts: String
- Default: None
- Notes: The version number must be prefixed with a
v
if:nvm_verb
is set to:use
. See examples below.
-
:nvm_type
- Accepts:
:user
,:system
- Default:
:user
- Notes: Used to determine the location of
nvm.sh
, similar to setting$NVM_HOME
in your shell.
- Accepts:
-
:nvm_custom_path
- Accepts: String
- Default: Unused
- Notes: Setting this effectively overrides
:nvm_type
.
-
:nvm_map_bins
- Accepts: Ruby word array
- Default:
%w{node npm yarn}
- Notes: This is a list of commands that will be executed with NVM support by capistrano.
-
:nvm_verb
- Accepts:
:use
,:install
- Default:
:use
- Notes: Determines whether the deploy invokes
nvm use
ornvm install
.
- Accepts:
Using a version of NodeJS that is already installed and managed by NVM:
set :nvm_node, 'v8.10' # Version of node.js to use with NVM
Using a version of NodeJS that we want to download and install:
set :nvm_node, 'lts/carbon' # Friendly names work well...
set :nvm_verb, 'install' # ...but only with the "install" verb
When NVM is installed at the system level:
set :nvm_node, 'lts'
set :nvm_verb, 'install'
set :nvm_type, :system
When NVM is installed in a custom location:
set :nvm_node, 'lts'
set :nvm_verb, 'install'
set :nvm_custom_path, '/usr/local/src/nvm' # This overrides :nvm_type so we won't bother setting it
In config/deploy.rb
, check for .nvmrc
and use its value, or a sensible default like v8.10:
set :nvm_node, File.exist?('.nvmrc') ? File.read('.nvmrc').strip : "v8.10"
In your rakefile where you execute :npm
, :node
, or :yarn
, add this conditional first:
if test "[ -e #{release_path}/.nvmrc ]"
download! "#{release_path}/.nvmrc", '.nvmrc'
SSHKit.config.default_env[:node_version] = File.read('.nvmrc').strip
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request