Heroku - 在浏览器中显示当前提交的哈希值 [英] Heroku - Display hash of current commit in browser

查看:21
本文介绍了Heroku - 在浏览器中显示当前提交的哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在浏览器中显示当前 git 提交的哈希值,以便测试团队(无权运行 heruko 命令)能够在错误报告中包含相应的提交哈希值.

I want to display the hash of the current git commit in the browser so that testing team (which does not have an access to run heruko commands) will be able to include the corresponding commit hash in bug reports.

首先我尝试了grit,但有些东西坏了,它在 Heroku 上不起作用(在本地它工作得很好,我不知道为什么它在 Heroku 上失败了).

First I tried grit, but something is broken and it doesn't work on Heroku (on local it works great, I don't know why it fails on Heroku).

于是我发现Heroku上有两个环境变量:

So I found out that there are two environment variables on Heroku:

ENV["COMMIT_HASH"]
ENV["LAST_COMMIT_BY"]

但它们都不可用(都为零).

But neither of them is available (both are nil).

我还检查了:

heroku config

但同样,两者都没有设置.

But again, neither is set.

有没有办法检索散列信息?有没有办法获得更多的 git 信息,例如日期?

Is there a way to retrieve the hash information? Is there any way to have more git information, such as date for example?

推荐答案

首先,由于 heroku "remove[s]未使用的文件,包括 .git 目录 在 slug 编译期间,您将无法从应用程序目录中(在 heroku dyno 上)执行某些 git 命令.这包括诸如 git rev-parse HEAD 之类的东西,这通常是获取当前哈希的简单方法.

Firstly, since heroku "remove[s] unused files, including the .git directory" during slug compilation, you won't be able to execute some git commands from inside your app's directory (on the heroku dyno). This includes things like git rev-parse HEAD, which is normally an easy way to get the current hash.

其次,尝试在 heroku dyno 上使用 git ls-remote 检索信息将调用 ssh,您将看到消息说 主机 'heroku.com (50.19) 的真实性.85.132)'无法建立,因为heroku公钥没有安装在heroku dynos上.您将无权安装 heroku 公钥.

Secondly, trying to retrieve information with git ls-remote on the heroku dyno will invoke ssh, and you'll see messages that say The authenticity of host 'heroku.com (50.19.85.132)' can't be established, since the heroku public key is not installed on heroku dynos. You won't have permission to install the heroku public key.

你至少还有两个选择.

  1. 添加 post-commit hook 以更新哈希值.

a) 创建或编辑文件 .git/hooks/post-commit
b) 像这样添加一些 shell 脚本代码:

a) Create or edit the file .git/hooks/post-commit
b) Add some shell script code like this:

hash_name=HEAD_HASH
hash=$(git rev-parse HEAD)
echo 将 $hash_name 设置为 $hash
heroku 配置:设置 $hash_name=$hash --app yourappname

hash_name=HEAD_HASH
hash=$(git rev-parse HEAD)
echo Setting $hash_name to $hash
heroku config:set $hash_name=$hash --app yourappname

(你可以为 git hooks 使用任何你想要的代码;这只是一种选择)

(you can use whatever code you want for git hooks; this is just one option)

说明:

  • HEAD_HASH 是 heroku 环境变量的名称.随心所欲地称呼它.您将在主应用中查找此内容并将其显示在页面上.
  • git rev-parse HEAD 获取当前 HEAD 提交的哈希值.根据您想要显示的内容自定义此行.
  • HEAD_HASH is the name of the heroku environment variable. Call it whatever you want. You'll look this up in your main app and display it on the page.
  • git rev-parse HEAD grabs the hash of the current HEAD commit. Customize this line for whatever you want to display.


现在,当您提交 git 时,HEAD_HASH 环境变量将每次更新.这有效,但可能有点慢,因为每次提交时您都将等待 heroku 设置 env var.如果您的网络连接断开等,则不会更新该变量.传闻 git 1.8.2 将允许预推送"' 钩住您可以放置​​此代码的位置.


Now when you make commits to git the HEAD_HASH env var will be updated each time. This works, but might be a bit slow, as you'll be waiting for heroku to set the env var each time you commit. If your network connection is out etc. the variable won't be updated. Rumour is that git 1.8.2 will allow a 'pre-push' hook where you could put this code instead.

使用脚本推送你的代码

您可以编写一个包含选项 1. 中的行并添加 git push 的 shell 脚本,而不是键入 git push heroku master 来推送您的代码heroku master 最后.然后为了部署你的代码,你运行这个 shell 脚本.这将仅在推送之前(而不是在每次 git 提交之后)更新 HEAD_HASH,并且它很好地将所有内容保存在一个地方.您可能还想将脚本添加到您的 .slugignore 文件中.

Instead of typing git push heroku master to push your code, you could write a shell script that contains the lines from option 1. and adds git push heroku master at the end. Then to deploy your code you run this shell script. This will update the HEAD_HASH only before pushing (instead of after each git commit), and it nicely keeps everything in one place. You'll probably want to add the script to your .slugignore file too.

这篇关于Heroku - 在浏览器中显示当前提交的哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆