【问题标题】:Set `process.env.commit` on Heroku push在 Heroku 推送上设置 `process.env.commit`
【发布时间】:2014-07-18 02:42:54
【问题描述】:

当我们将其推送到 Heroku 时,我正在使用 NPM 的 scripts.postinstall 使用 GulpJS 构建我们的应用程序:

"postinstall": "./node_modules/.bin/gulp build --require coffee-script/register"

我希望此时能够使用提交哈希的值设置环境变量。然后,这将在文件中作为 cachebuster 引用,例如:

<script type="text/javascript" src="/index.{{ process.env.commit }}.js"></script>

我可以使用git-rev 轻松地异步获取此值,但我希望它存储在环境级别。 如何设置自定义 Node 环境变量?

如果我以错误的方式解决这个问题并且有更好的解决方案,请告诉我(我只能找到设置 NODE_ENV 而不是 process.env 对象本身的方法。

【问题讨论】:

    标签: node.js git heroku


    【解决方案1】:

    您现在可以使用 this labs feature from heroku 添加此元数据!

    heroku labs:enable runtime-dyno-metadata -a <app name>
    

    【讨论】:

      【解决方案2】:

      您不能通过postinstall 脚本执行此操作:heroku 命令在构建环境中不可用。您可以创建自己的轻量级部署脚本,当构建成功时在您的机器上运行命令:

      #!/bin/sh
      
      git push heroku master && heroku config:set COMMIT=$(git rev-parse --short HEAD)
      

      请注意,我直接从 Heroku 支持人员那里得到了确认。这是他们的回应:

      在 slug 编译器上没有安装 heroku 工具带。如果您需要设置环境变量作为推送的一部分,那么您必须在您的机器上而不是 Heroku 编译器上进行。

      【讨论】:

        【解决方案3】:

        我找到的解决方案是使用heroku config系统来设置自定义环境变量:

        git = require "git-rev"
        exec = require "exec-sync"
        
        gulp.task "commit", ->
          git.long (hash) ->
            exec "heroku config:set COMMIT=#{ hash }"
        

        需要进一步测试,但目前它可以工作(另外,因为命令是在服务器上运行的,所以不需要使用heroku config:push)。

        【讨论】:

        • 非常有趣的方法!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-22
        • 1970-01-01
        • 2016-12-23
        • 1970-01-01
        相关资源
        最近更新 更多