简体   繁体   English

如何使用环境变量在 Elastic Beanstalk EB 上运行 sequelize db:migrate? 如何在容器命令中访问 .env vars?

[英]How to run sequelize db:migrate on Elastic Beanstalk EB with env vars? How to access .env vars in container commands?

How can I run sequelize db:migrate on ElasticBeanstalk with env vars?如何使用环境变量在 ElasticBeanstalk 上运行 sequelize db:migrate?

Running sequelize migrate fails since it cannot find the .env file.运行 sequelize migrate 失败,因为它找不到.env文件。

{ Error: ENOENT: no such file or directory, open '.env'

my master.config looks like:我的 master.config 看起来像:

container_commands:
  00_node_binary:
    command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
  00_npm_binary:
      command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
  01_migrations:
    command: npm run migrate
    leader_only: true

and my package.json contains我的 package.json 包含

"migrate": "node_modules/sequelize-cli/bin/sequelize db:migrate"

Edit:编辑:

I just found out what's going on with environment variables.我刚刚发现了环境变量的情况。 Try running the migration script without npm .尝试在没有npm情况下运行迁移脚本。 It'll be something like:它会是这样的:

./node_modules/.bin/sequelize db:migrate

This way, you'll get all the environment variables as you expect.这样,您将获得您期望的所有环境变量。

Old answer:旧答案:

Are you sure your .env file is committed to your git repo?您确定您的.env文件已提交到您的 git 存储库吗? In general, it's not a good idea to commit a .env to git and use it in production.通常,将.env提交到 git 并在生产中使用它并不是一个好主意。 You should instead set environment variables in your Elastic Beanstalk dashboard under Software Configuration .您应该在 Elastic Beanstalk 仪表板中的Software Configuration下设置环境变量。

You could also use the eb command line utility as documented here .您还可以使用此处记录eb命令行实用程序。

Don't forget to include the first two commands, the file migration.config that worked for me in .ebextensions looks like this不要忘记包含前两个命令,在 .ebextensions 中对我有用的文件 migration.config 看起来像这样

container_commands:
  00_node_binary:
    command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
  00_npm_binary:
    command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
  50-run-database-migrations:
    command: "./node_modules/.bin/sequelize db:migrate"
    leader_only: true

It looks like the ./node_modules/.bin/sequelize uses /usr/bin/env/node and will give you the following error:看起来 ./node_modules/.bin/sequelize 使用 /usr/bin/env/node 并且会给你以下错误:

/usr/bin/env: node: No such file or directory

Because apparently node is called nodejs... the first two container commands will take care of that.因为显然 node 被称为 nodejs ......前两个容器命令将处理这个问题。

See https://github.com/nodejs/node-v0.x-archive/issues/3911 for further reference请参阅https://github.com/nodejs/node-v0.x-archive/issues/3911以获取进一步参考

I resolve this error through running command in .ebextension/config_file.sh node vsersion is same which is using in the EB console我通过在 .ebextension/config_file.sh 节点中运行命令解决了这个错误。

files: "/opt/elasticbeanstalk/hooks/appdeploy/pre/config_file.sh":文件:“/opt/elasticbeanstalk/hooks/appdeploy/pre/config_file.sh”:

mode: "000755"
owner: root
group: root
content: |
  #!/bin/bash
  
  curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -
  
  sudo yum -y install nodejs

then然后

run command on terminal in your app directory:在您的应用程序目录中的终端上运行命令:

./node_modules/.bin/sequelize db:migrate ./node_modules/.bin/sequelize db:migrate

its work for me!!它对我有用!!

None of the solutions worked for me.没有一个解决方案对我有用。 What worked was lazartravica 's answer on the following page:有效的是lazartravica下一页上的回答:

https://github.com/sequelize/sequelize/issues/12913#issuecomment-782254695 https://github.com/sequelize/sequelize/issues/12913#issuecomment-782254695

TLDR:域名注册地址:
Don't run npm install in the Deploy step, this is both a security and a performance issue.不要在 Deploy 步骤中运行npm install ,这既是一个安全问题,也是一个性能问题。 Run node_modules/sequelize-cli/lib/sequelize db:migrate in the Build step to fix the issue.在构建步骤中运行node_modules/sequelize-cli/lib/sequelize db:migrate以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM