Heroku - 显示当前提交的散列 [英] Heroku - Display hash of current commit

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

问题描述

我想在浏览器中显示当前git提交的散列,以便测试团队(不具有运行heruko命令的访问权限)能够在bug报告中包含相应的提交散列。



首先我尝试了一下,但有些东西坏了,在Heroku上不起作用(在本地它效果很好,我不知道它为什么在Heroku上失败)。 >

所以我发现Heroku上有两个环境变量:

  ENV [COMMIT_HASH] 
ENV [LAST_COMMIT_BY]

但它们都不可用(都是零)。



我也检查过:

  heroku config 

但是,这两者都没有设置。



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

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



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



您至少还有两个选项。


  1. 添加提交后挂钩到更新散列值。

    创建或编辑文件 .git / hooks / post-commit
    >
    b)添加如下的shell脚本代码:

    lockquote
    hash_name = HEAD_HASH

    hash = $( git rev-parse HEAD)

    $ b $ echo设置$ hash_name为$ hash

    heroku config:set $ hash_name = $ hash --app yourappname
    blockquote>

    (你可以使用你想要的任何代码作为git钩子;这只是一个选项)。 :


    • HEAD_HASH 是heroku环境变量的名称。随心所欲地调用它。您将在主应用程序中查找并显示在页面上。

    • git rev-parse HEAD 当前HEAD提交。自定义此行以显示任何内容。



    现在,当您提交git HEAD_HASH env var会每次更新。这可行,但可能有点慢,因为你会等待heroku每次提交时设置env var。如果您的网络连接已关闭等变量将不会更新。 谣言,git 1.8.2将允许预先推送'hook',你可以把这段代码放在这里。

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


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.

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).

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).

I also checked with:

heroku config

But again, neither is set.

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

解决方案

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.

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. Add a post-commit hook to update the hash value.

    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 Setting $hash_name to $hash
    heroku config:set $hash_name=$hash --app yourappname

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

    Explanation:

    • 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.


    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.

  2. Use a script to push your code

    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天全站免登陆