当我们将应用程序推送到 Heroku 时,我使用 NPM 的 scripts.postinstall
来使用 GulpJS 构建我们的应用程序:
"postinstall": "./node_modules/.bin/gulp build --require coffee-script/register"
我希望此时能够使用提交哈希值设置一个环境变量。然后,这将在文件中作为缓存破坏器引用,例如:
<script type="text/javascript" src="/index.{{ process.env.commit }}.js"></script>
我可以使用 git-rev 轻松异步获取此值,但我希望将其存储在环境级别。 如何设置自定义 Node 环境变量?
如果我以错误的方式解决这个问题,并且有更好的解决方案,请告诉我(我只能找到设置 NODE_ENV
的方法,而不是 process.env
对象本身)。
最佳答案
您无法通过 postinstall
脚本执行此操作:heroku
命令在构建环境中不可用。您可以创建自己的轻量级部署脚本,以便在成功构建时在您的计算机上运行该命令:
#!/bin/sh
git push heroku master && heroku config:set COMMIT=$(git rev-parse --short HEAD)
<小时/>
需要注意的是,我直接从 Heroku 支持处得到了确认。这是他们的回应:
There isn't an installation of heroku toolbelt on the slug compiler. If you need to set an environment variable as part of the push then you'll have to do it on your machine rather than the Heroku compiler.
关于node.js - 在 Heroku 推送上设置 `process.env.commit`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23919013/