如何在ruby代码中从github添加内部版本号和日期?(How to add build number and date from github in ruby code?)

如何从github自动导入最新的github提交号?

我们的目标是在您的网页页面上显示该数字,就像日期一样。

是什么结构

我有一个使用Capistrano部署的生产分支。 我想显示最新的github提交号,以及它的部署日期。

How can you automatically import the latest github commit number from github?

The goal would be to have that number visible on your webpage footer like SO does with date.

Whats the structure:

I have a production branch which is deployed using Capistrano. I want to show the latest github commit number, with the date when it was deployed.

最满意答案

假设您使用gem settingslogic进行应用程序设置,请将其放在初始化程序中:

git_log = `git log -1 --pretty="format:%H %ci"` if git_log =~ /^([\d\w]+?)\s(.+)$/ Settings[:git_revision] = $1 Settings[:git_update] = $2.strip end

您将在Settings.git_revision拥有最后一个git commit SHA,并在Settings.git_revision提交日期。

此外,您可以获得最后一个标记

git_tag = `git describe --tags --abbrev=0` Settings[:git_tag] = git_tag.strip if git_tag

它将在Settings.git_tag 。

更新:

我发布了一个小红宝石宝石git-revision 。 有了它你可以简单地做:

"commit: #{Git::Revision.commit} date: #{Git::Revision.date}"

Assuming you use gem settingslogic for app settings, put this in your initializers:

git_log = `git log -1 --pretty="format:%H %ci"` if git_log =~ /^([\d\w]+?)\s(.+)$/ Settings[:git_revision] = $1 Settings[:git_update] = $2.strip end

You will have last git commit SHA in Settings.git_revision and commit date in Settings.git_update.

Additionally you can get last tag:

git_tag = `git describe --tags --abbrev=0` Settings[:git_tag] = git_tag.strip if git_tag

It will be available in Settings.git_tag.

Update:

I released a small ruby gem git-revision. With it you can simply do:

"commit: #{Git::Revision.commit} date: #{Git::Revision.date}"

更多推荐