简体   繁体   English

Hubot 启动时自动加载 .env

[英]Automate loading .env when Hubot starts

I'm developing an app with the Hubot framework, using Heroku.我正在使用 Heroku 开发带有 Hubot 框架的应用程序。 I have some config vars set on Heroku, and when I deploy my code, my process.env references work fine.我在 Heroku 上设置了一些配置变量,当我部署代码时,我的process.env引用工作正常。

However, I've had trouble getting my local development system set up with the config vars for testing.但是,我在使用配置变量设置本地开发系统进行测试时遇到了麻烦。 I have a .env file and have fetched all the config vars from Heroku.我有一个 .env 文件,并从 Heroku 获取了所有配置变量。 However, the .env file does not seem to be loaded when I start my app at the command line.但是,当我在命令行启动我的应用程序时,似乎没有加载 .env 文件。

I've added hubot-env (as suggested at Hubot - load environmental variables from a file ) and can load my .env file manually each time I start my app with the command我已经添加了hubot-env (如Hubot所建议的- 从文件加载环境变量)并且可以在每次使用命令启动我的应用程序时手动加载我的 .env 文件

hubot env load --filename=[filename]

I'd like to automate this, so this command is automatically executed when I start my bot.我想自动化这个,所以当我启动我的机器人时会自动执行这个命令。 Where could I configure this?我可以在哪里配置这个?

Regarding this issue as I understood Hubot doesn't read .env file.关于这个问题,据我了解 Hubot 不读取 .env 文件。 Instead of exporting variables each time my solution was to create bash-script run.sh file:每次我的解决方案是创建 bash-script run.sh文件时,而不是导出变量:

#!/bin/bash

export HUBOT_OWM_APIKEY=MY_OWM_API_KEY;
export HUBOT_WEATHER_UNITS=metric;

HUBOT_SLACK_TOKEN=xoxb-xxxx-MY_SLACK_TOKEN ./bin/hubot --adapter slack

then in bash然后在 bash

$ chmod +x run.sh   # provides the permissions
$ ./run.sh          # starts the bot with necessary variables

This is a really old question but I'm working on Hubot now and need to save this for posterity.这是一个非常古老的问题,但我现在正在研究 Hubot,需要为后代保存它。

This is how I'm doing it now.这就是我现在的做法。 It works without adding additional files or packages:它无需添加额外的文件或包即可工作:

"scripts": {
    "start": "export $(cat .env | xargs) && ./bin/hubot -a slack"
  }

Change your adapter from slack to whatever you are using.将您的适配器从slack更改为您正在使用的任何适配器。

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

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