在Web应用程序中显示最新的提交值? [英] Show the latest commit value within a web application?

查看:189
本文介绍了在Web应用程序中显示最新的提交值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Rails应用程序,我使用Git作为版本控制系统。我使用GitHub或Beanstalk作为存储库主机。



我想要的理论上很简单。以某种方式在Web应用程序的页脚中显示最新的提交ID号(散列?)。因此,当我在查看Web应用程序时,我可以检查它是否已提交并正确部署。



我可以想象有两种方法可以解决这个问题。第一个是Git的一个可能的特性,它允许输出提交ID。第二个是一个post-commit的web钩子(Beanstalk和GitHub都允许这样做)。



有没有人找到一种方法来做到这一点,或类似的东西?



谢谢,

Danny

解决方案<首先澄清: post-commit 钩子不能将commit-id添加到中的文件中,因为commit id取决于在顶层树(代表顶层目录)的提交时,顶层树的ID依次取决于其成员的ID,并且文件的ID取决于其内容......并且该内容将包括提交ID。不可能。



但让我们来看看不同的解决方案:



实时服务器端脚本



如果您的web应用程序是从非裸露的git存储库进行实时部署的(我希望您知道您正在做什么,并将其推入非裸仓库,即使用checkout / working tree ),那么你的web应用可以通过使用 git rev-parse HEAD (给出提交的SHA-1)或者更好 git describe --dirty - dirty 选项会使返回的字符串包含信息,不管您是否拥有在工作区内未改变的变化)或 git describe --always HEAD



git rev-parse HEAD 给出类似于 7611062b4ba6d1ebc4cf3e63c11204a4f5057660 的东西,而 git describe --dirty 例如 v1.7.3.2-95-g7611062 (这意味着提交的缩写SHA-1为 7611062 ,95次提交承诺ta后gged'v1.7.3.2'),但这取决于你使用带注释的标签来标记版本。



这个版本的一个变种是让web应用程序检查HEAD版本库在同一个文件系统上的其他地方,例如与 git --git-dir = / path / to / .git描述HEAD



Sidenote :如果你使用Ruby,你可能想使用砂砾库。

 <$  c $ c> require'grit'
include Grit

repo = Repo.new(/ var / git / app.git)
head = repo.commits(' HEAD',1)

app_version = head.id




从git checkout提供的实时,静态文件



编辑: 节添加2010-10-23 13:33 +0000

如果您从非裸露的git存储库的结帐(worktree)提供文件(而不是您的情况),您可以使用 filter 'smudge'和'clean'命令 gitattribute 可以在结账/签到时执行类似CVS的关键字扩展功能。

.gitattributes 文件中,您可以定义过滤器属性应该执行的文件:

 
* .rb filter = commitid

您可以在git config file(例如在 .git / config )中,例如

 
[过滤器commitid]
smudge = sed -es / \ $ Revision:?\ $ / \ $ Revision:$(git rev-parse HEAD)\ $ / 1
clean = sed -e s / \ $ Revision:?[^ $] * \ $ / \ $ Revision:\ $ / 1

使用例如 smudge 过滤器会替换'$ Revision:$' '$ Revision:v1.7.3.2-95-g7611062'(这意味着检出的文件将包含类似CVS的关键字扩展)。将文件内容存储在git对象数据库(在git存储库中)时, clean 过滤器将删除扩展;否则你在比较文件等方面会遇到问题。






使用<$ c进行部署如果您改为部署您的Web应用程序,那么它不会驻留在实时存储库中(这有其怪癖wrt推送到它,并有可能的安全缺陷),你可以在某处使用 git archive (例如,将zip应用程序上传到你的托管站点),你可以使用关键字替换



首先,您需要告诉Git您希望将文件中的关键字替换为 git archive 。您可以通过为给定文件设置 export-subst 来实现,例如添加到 .gitattributes 文件

 
* .rb export-subst

然后加入包含/生成页脚的文件例如:

 
$格式:%H $

它将被提交散列替换(请参阅 git-log manpage)。




部署,使用某些部署脚本



如果您使用某种脚本/脚本化机制来部署您的Web应用程序,则应遵循Jefromi建议让您的部署脚本嵌入版本信息。

你必须问别人如何设置 Capistrano (假设您将其用于部署)发布:部署将'@@ VERSION @@'占位符替换为' app.rb'文件,结果为 git describe --always HEAD ... Git project Makefile 为此使用 sed


I have a few Rails applications and I use Git as the version control system. I use either GitHub or Beanstalk as the repository hosts.

What I am trying to is quite simple in theory. Somehow show the latest commit ID number (hash?) in the footer of the web application. So that when I am looking at the web application I can check that it's committed and deployed correctly.

I can imagine there are two methods to tackle this. The first would be a possible feature of Git that allows the output of the commit ID. The second would be a post-commit web hook (both Beanstalk and GitHub allow this).

Has anyone ever found a way to do this, or something similar?

Thanks,

Danny

解决方案

First, a clarification: post-commit hook cannot add commit-id to file in a commit, because commit id depends on commit of the top tree (representing top directory), id of top tree in turn depends on ids of its members, and id of a file depends on its contents... and this content is to include commit id. Not possible.

But let's take a look at different solutions:

Live, server side scripting

If your web app is deployed live from non-bare git repository (I hope you know what you are doing wrt. pushing into non-bare repository, i.e. repository with checkout / working tree), then your web app can check HEAD by using git rev-parse HEAD (gives SHA-1 of commit), or better git describe --dirty (the --dirty option would make returned string contain information whether you have uncomitted changes in working area), or git describe --always HEAD.

git rev-parse HEAD gives something like 7611062b4ba6d1ebc4cf3e63c11204a4f5057660, while git describe --dirty gives something like v1.7.3.2-95-g7611062 (which means commit with abbreviated SHA-1 of 7611062, 95 commits after commit tagged 'v1.7.3.2'), but it depends on you tagging releases using annotated tags.

A variant of this would be to have web app to check HEAD from repository which is somewhere else on the same filesystem, e.g. with git --git-dir=/path/to/.git describe HEAD.

Sidenote: if you use Ruby, you probably want to use grit library. The equivalent of git rev-parse HEAD version would probably be (untested!):

require 'grit'
include Grit

repo = Repo.new("/var/git/app.git")
head = repo.commits('HEAD', 1)

app_version = head.id


Live, static files served from git checkout

Edit: section added 2010-10-23 13:33 +0000
If you serve your files from checkout (worktree) of non-bare git repository (not your situation), you can use 'smudge' and 'clean' commands of filter gitattribute to perform CVS-like keyword expansion on checkout / checkin.

In .gitattributes file you would define files on which filter attribute should act:

*.rb filter=commitid

You define filter in git config file (e.g. in .git/config), for example

[filter "commitid"]
        smudge = sed -e "s/\$Revision: ?\$/\$Revision: $(git rev-parse HEAD)\$/1"
        clean =  sed -e "s/\$Revision: ?[^$]*\$/\$Revision: \$/1"

The smudge filter would replace '$Revision: $' with e.g. '$Revision: v1.7.3.2-95-g7611062' on checkout (this means that checked outfiles would contain this CVS-like keyword expanded). The clean filter would remove expansion when storing file contents in git object database (in git repository); otherwise you would have problems with comparing file etc.


Deployed with use of git archive

If you instead deploy your web app, so it doesn't reside in live repository (which has its quirks wrt. pushing into it, and which has possible security drawbacks), and you use git archive somewhere (e.g. to zip app to upload it to your hosting site), you can make use of keyword substitution.

First you need to tell Git that you want keywords in a file replaced by git archive. You do that by setting export-subst for given file, for example by adding to .gitattributes file

*.rb export-subst

and then adding to your file that contains/generates page footer e.g

$Format:%H$

which will be replaced by commit hash (see pretty-formats description e.g. in git-log manpage).


Deployed, using some deployment script

If you use some kind of script / scripted mechanism to deploy your web app, you should follow Jefromi advice of having your deploy script embed version information.

You would have to ask somebody else how to set Capistrano (assuming that you use it for deployment) to post:deployment replace '@@VERSION@@' placeholder in your 'app.rb' file with result of git describe --always HEAD... Git project Makefile uses sed for that.

这篇关于在Web应用程序中显示最新的提交值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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