是否可以配置EBS使用yarn包管理器而不是NPM来安装我的NodeJS应用程序?
我已经找到了办法,但它有点笨拙。
.ebextensions/yarn.config
文件。 (这个名字不一定是'纱'。)files:
# Runs right before `npm install` in '.../50npm.sh'
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
#!/bin/bash
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
# install node
curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -;
# install yarn
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo;
yum -y install yarn;
# install node_modules with yarn
cd "${app}";
yarn --production;
这个ebextension创建了一个文件,它可以做三件事:
为了使Elastic Beanstalk在运行yarn install
之前运行npm install
,该文件在/opt/elasticbeanstalk/hooks/appdeploy/pre
下创建。这会将文件转换为预部署挂钩,这意味着Elastic Beanstalk将在部署的第一阶段运行它。默认情况下,此目录中有另一个名为50npm.sh
的文件,它运行npm install
。由于Elastic Beanstalk按字母顺序运行此目录中的文件,因此49yarn.sh
(我们的文件)将在50npm.sh
(默认文件)之前运行,导致yarn install
在npm install
之前运行。
一个潜在的问题是,在部署阶段的此时,Elastic Beanstalk UI(在Configuration
> Software Configuration
下)中设置的环境变量不可用。如果您有一个用于安装私有npm模块的npm身份验证令牌,这是一个大问题。
另一个潜在的问题是,这会手动安装节点,因此您在Elastic Beanstalk UI中指定的“节点版本”(在Configuration
> Software Configuration
下)将不会影响您的应用程序使用的节点版本;你需要在这个ebextension中指定它。 Elastic Beanstalk的50npm.sh
都安装节点并运行npm install
。由于我们必须在该文件运行之前运行yarn install
,我们还必须手动安装节点。然后,当Elastic Beanstalk进入安装节点时,它会检测到该节点已经安装但未验证它是否是正确的版本,因此它会跳过节点安装。
作为参考,纱线安装说明来自这里:https://yarnpkg.com/docs/install#linux-tab
我按照https://yarnpkg.com/lang/en/docs/install/的说明做了这个
commands:
01_install_yarn:
command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - && sudo yum install yarn -y"
通过这种方式,我可以通过Elastic Beanstalks Dashboard控制节点版本。
谢谢你的回答!没有它,就不可能找到这个解决方案:D
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script
# (https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663).
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script.
# But their default script actually doesn't work at all, since the app
# staging dir, where they try to run `npm install`, doesn't exist during
# config deploys, so ebnode.py just aborts:
# https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L140
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
tmp="$(mktemp || bail)";
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
version="$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)";
echo $version
major="$(cut -d'.' -f1 <<<${version})"
yum -y install python26 python26-libs
wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo;
wget "https://rpm.nodesource.com/pub_${major}.x/el/7/x86_64/nodejs-${version}-1nodesource.x86_64.rpm" -O "${tmp}";
rpm -i --nosignature --force "${tmp}";
rm -f "${tmp}";
yum -y install yarn;
cd "${app}";
yarn --production;
EB默认支持npm,所以我建议通过.ebextensions中的部署脚本安装yarn。那应该做。