node.js - 在 Heroku 推送上设置 `process.env.commit`

标签 node.js git heroku

当我们将应用程序推送到 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/

相关文章:

node.js - 读取 mongodb 游标会产生 - MongoError : clientcursor already in use?

javascript - 无法使用 mysql 包从我的 node.js 应用程序连接到数据库

git - 尾随空格。补丁不适用

git - SVN 1.8 merge 与 Git 和 Mercurial 等 DVCS 相比如何?

git - Jenkins 不再工作 : SSH executable not found

heroku - Heroku-如何撤销对Heroku的推送?

node.js - 在 web.config 中为 iisnode 中的 Express 设置规则

php - 使用 PHP 在 Heroku 上从非 www 重定向到 www

java - 如何在没有测试范围 jar 的情况下进行 Maven 安装

node.js - 使用 webpack-dev-server 运行 node express 服务器