让 Capistrano 运行 shell 任务的问题(nodejs 部署)

发布于 2024-12-02 21:21:22 字数 293 浏览 3 评论 0原文

我正在使用 capistrano 部署 node.js 应用程序,并且有一个 设置 shell 任务时出现问题。 例如,我认为我已经安装了 npm,但失败了:

运行“npm install” 未找到 npm

,当我使用

run "/opt/nvm/'cat /opt/nvm/alias/default'/bin/npm install" 错误是找不到节点

部署由特殊用户进行部署管理。

您能告诉我什么可能导致这个问题以及如何解决它吗?

I am using capistrano to deploy a node.js application, and have a
problem with setting the shell tasks.
For instance, thought I have npm installed this fails:

run "npm install"
npm not found

and when I use

run "/opt/nvm/'cat /opt/nvm/alias/default'/bin/npm install"
the error is node not found

The deploy is managed by a special user for deploy.

Could you please tell what might cause this problem and how to solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

日久见人心 2024-12-09 21:21:22

通过 bash 运行命令并首先获取 nvm.sh 文件,使用 NVMCapistrano 对我来说很有效。

我的 NVM 安装在 /opt/nvm 中,因此 npm install 任务可能如下所示:

namespace :npm do

  desc 'Install the current npm environment.'
  task :install do
    invoke_command "bash -c '. /opt/nvm/nvm.sh && cd #{current_path} && npm install'", :via => run_method
  end

end

因此无需通过读取别名来手动设置二进制文件的路径来自 NVM 的文件。

Using NVM and Capistrano is working for me by running the command through bash and sourcing the nvm.sh file first.

My NVM is installed in /opt/nvm, so the npm install task could look something like this:

namespace :npm do

  desc 'Install the current npm environment.'
  task :install do
    invoke_command "bash -c '. /opt/nvm/nvm.sh && cd #{current_path} && npm install'", :via => run_method
  end

end

So no need of manually setting the path to the binaries by reading the alias file from NVM.

你是年少的欢喜 2024-12-09 21:21:22

听起来 npm/node 可执行文件不在执行 Capistrano 脚本的远程用户的 $PATH 上。

您应该仔细检查 Capistrano 正在运行的用户以及 $PATH 是什么(并根据需要进行更正)

Sounds like the npm/node executables are not on the $PATH for the remote user that is executing the Capistrano script.

You should double check which user Capistrano is running as and what the $PATH is (and correcting as required)

烟柳画桥 2024-12-09 21:21:22

我最终将其添加到我的 Capfile

set :default_environment, {
  'PATH' => "$PATH:/usr/local/ruby/bin/:/home/ubuntu/.nvm/v0.10.21/bin"
}

I ended up adding this to my Capfile

set :default_environment, {
  'PATH' => "$PATH:/usr/local/ruby/bin/:/home/ubuntu/.nvm/v0.10.21/bin"
}
桜花祭 2024-12-09 21:21:22

我能够通过在我的 capistrano 部署脚本中添加如下行来影响节点版本更改:

execute :'. ~/.nvm/nvm.sh && nvm use vOLDER_VERSION && nvm alias default vOLDER_VERSION'
execute :npm, 'install', '--silent --no-progress'
execute :npm, 'run', 'build'
execute :'. ~/.nvm/nvm.sh && nvm use stable && nvm alias default stable'

其中 vOLDER_VERSION 是选择的版本。

它的工作方式是它获取 nvm,将其设置为使用指定版本,并将该版本设置为默认版本。然后,它会在 npm 步骤运行后恢复这些更改。这样部署的其他部分或其他部署仍然可以使用最新版本。

如果您不关心其他部署,您只需为部署者用户运行该步骤一次:

su deployerUser
nvm use vOLDER_VERSION
nvm alias default vOLDER_VERSION

I was able to affect node version changes by adding lines like the following to my capistrano deploy scripts:

execute :'. ~/.nvm/nvm.sh && nvm use vOLDER_VERSION && nvm alias default vOLDER_VERSION'
execute :npm, 'install', '--silent --no-progress'
execute :npm, 'run', 'build'
execute :'. ~/.nvm/nvm.sh && nvm use stable && nvm alias default stable'

Where vOLDER_VERSION is the version of choice.

The way this works is it sources nvm, sets it to use the specified version, and sets that version as default. It then reverts those changes after the npm steps have been run. This way other parts of the deploy or other deploys still get to use the latest version.

If you don't care about other deploys you could just run that step once for your deployer user:

su deployerUser
nvm use vOLDER_VERSION
nvm alias default vOLDER_VERSION
沉鱼一梦 2024-12-09 21:21:22

与此同时(一年多前)我创建了一个 Capistrano 扩展,以便轻松使用 nvm: https: //github.com/koenpunt/capistrano-nvm

该扩展默认会映射 nodenpm,但您可以添加任何需要 nvm 运行的可执行文件到它(例如咕噜声)。

通过将以下内容添加到您的 deploy.rb 中即可进行基本设置:

require 'capistrano/nvm'

set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v0.10.21'
set :nvm_map_bins, %w{node npm}

In the meantime (more than a year back tho) I've created a Capistrano extension for easy nvm usage: https://github.com/koenpunt/capistrano-nvm

The extension will map node and npm by default, but you can add any executable that needs nvm to run to it (eg. grunt).

A basic setup would work by adding the following to your deploy.rb:

require 'capistrano/nvm'

set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v0.10.21'
set :nvm_map_bins, %w{node npm}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文