Skip to content

Commit

Permalink
Install Node.js on deploy
Browse files Browse the repository at this point in the history
We use FNM instead of NVM. Reason: the setup seems easier with just
`eval "$(fnm env)"`.

So now, we try to install Node.js; if the command fails, it could be
because FNM isn't installed (and we need to install it) or because the
version of Node.js cannot be installed with the current version of FNM
(and we need to update FNM). After installing/updating FNM, we try to
install Node.js again.

Note we're using `fnm env` in the middle of the `fnm_setup_command`.
That way, the command will raise a `SSHKit::Command::Failed` exception
if `fnm` isn't installed, and it will give us an indicator that we need
to actually install it.
  • Loading branch information
javierm committed Oct 13, 2023
1 parent 8080f4c commit 9693e2d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def main_deploy_server

set :local_user, ENV["USER"]

set :fnm_path, "$HOME/.fnm"
set :fnm_install_command, "curl -fsSL https://fnm.vercel.app/install | " \
"bash -s -- --install-dir \"#{fetch(:fnm_path)}\""
set :fnm_update_command, "#{fetch(:fnm_install_command)} --skip-shell"
set :fnm_setup_command, -> do
"export PATH=\"#{fetch(:fnm_path)}:$PATH\" && " \
"cd #{release_path} && fnm env && eval \"$(fnm env)\""
end
set :fnm_install_node_command, -> { "#{fetch(:fnm_setup_command)} && fnm use --install-if-missing" }

set :puma_conf, "#{release_path}/config/puma/#{fetch(:rails_env)}.rb"

set :delayed_job_workers, 2
Expand All @@ -50,6 +60,7 @@ def main_deploy_server
Rake::Task["puma:smart_restart"].clear_actions

after :updating, "install_ruby"
after "bundler:install", "install_node"

after "deploy:migrate", "add_new_settings"

Expand Down Expand Up @@ -89,6 +100,26 @@ def main_deploy_server
end
end

task :install_node do
on roles(:app) do
with rails_env: fetch(:rails_env) do
begin
execute fetch(:fnm_install_node_command)
rescue SSHKit::Command::Failed
begin
execute fetch(:fnm_setup_command)
rescue SSHKit::Command::Failed
execute fetch(:fnm_install_command)
else
execute fetch(:fnm_update_command)
end

execute fetch(:fnm_install_node_command)
end
end
end
end

task :refresh_sitemap do
on roles(:app) do
within release_path do
Expand Down

0 comments on commit 9693e2d

Please sign in to comment.