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

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

问题描述

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

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.

首先,我尝试使用砂砾,但某些东西坏了,并且在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 删除[s] slug编译期间未使用的文件,包括.git目录",您将无法从应用程序目录(在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,并且您将看到显示The authenticity of host 'heroku.com (50.19.85.132)' can't be established的消息,因为heroku dynos上未安装heroku公钥.您无权安装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.

您仍然至少有两个选择.

You still have at least two options.

  1. 添加提交后挂钩以更新哈希值.

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 config:set $ 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 hook;这只是一个选择)

(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 env var将每次更新.这可行,但是可能有点慢,因为您每次提交时都会等待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.

使用脚本推送代码

代替键入git push heroku master来推送代码,您可以编写一个shell脚本,其中包含选项1.中的行,并在末尾添加git push heroku master.然后,要部署您的代码,请运行此Shell脚本.这只会在推送之前(而不是在每次git commit之后)更新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天全站免登陆