From 8ab66dd12f96057a3e716ccf0a1da4dc4784b278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 21 Jul 2023 03:22:58 +0200 Subject: [PATCH] Install Node.js on deploy 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. --- config/deploy.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/config/deploy.rb b/config/deploy.rb index b2e89a59a..3a45f2968 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -37,6 +37,16 @@ set :keep_releases, 5 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 > /dev/null && 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 @@ -49,6 +59,7 @@ namespace :deploy do Rake::Task["delayed_job:default"].clear_actions Rake::Task["puma:smart_restart"].clear_actions + after :updating, "install_node" after :updating, "install_ruby" after "deploy:migrate", "add_new_settings" @@ -89,6 +100,26 @@ task :install_ruby do 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