Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a 'last-deployed' timestamp from within my Heroku Rails app?

I'd like to update an environment variable or something to track the time of the deploy that is currently active. Is there any way to do this automatically from within my app on Heroku, or do I have to do it as part of a deploy script? Ideally, I'd like something that would work with me using TDDium for CI, and letting them do the push to Heroku for me when the build passes.

like image 894
Matt Van Horn Avatar asked Jun 27 '12 04:06

Matt Van Horn


2 Answers

Use Environment Variables on Heroku

You could use Heroku's config-vars. These are really just environment variables that you configure through the Heroku CLI. For example, you could store the current date in an environment variable named DEPLOY_TIMESTAMP.

heroku config:add DEPLOY_TIMESTAMP=$(date)

You should then be able to access this environment variable from within your application or from the command line. The value can be accessed with ENV['DEPLOY_TIMESTAMP'] from your Rails application, or by parsing the output of heroku config from your local project directory.

Automate with Aliases

If you want to automate this somewhat, you can create a Git alias to push to Heroku and update DEPLOY_TIMESTAMP at the same time. Please note that you can't overwrite the names of real Git commands like push, but you can add a custom action such as pushstamp. For example:

git config alias.pushstamp \
    '! git push heroku master; heroku config:add DEPLOY_TIMESTAMP=$(date)'

See Also

git-config(1)

like image 162
Todd A. Jacobs Avatar answered Sep 30 '22 12:09

Todd A. Jacobs


You can find the last deploy time by looking at the timestamps on the files in the read-only Heroku filesystem.

You can verify this by looking at those timestamps directly with ls. Example from running heroku run rails c:

irb(main):003:0> puts `ls -la`
total 96
drwx------ 14 u51199 51199  4096 May 14 22:49 .
drwxr-xr-x 15 root   root   4096 Mar 20 09:43 ..
drwx------ 10 u51199 51199  4096 May  7 02:12 app
drwx------  2 u51199 51199  4096 May  7 02:17 bin
drwx------  2 u51199 51199  4096 Mar 14 22:12 .bundle
drwx------  5 u51199 51199  4096 May  7 02:12 config
-rw-------  1 u51199 51199   226 May  7 02:12 config.ru
drwx------  3 u51199 51199  4096 May  7 02:12 db
-rw-------  1 u51199 51199  1138 May  7 02:12 Gemfile
-rw-------  1 u51199 51199 11456 May  7 02:12 Gemfile.lock
-rw-------  1 u51199 51199   542 May  7 02:12 .gitignore
drwx------  5 u51199 51199  4096 May  7 02:12 lib
drwx------  2 u51199 51199  4096 May  7 02:17 log
-rw-------  1 u51199 51199    57 May  7 02:12 Procfile
drwx------  2 u51199 51199  4096 May  7 02:13 .profile.d
drwx------  3 u51199 51199  4096 May  7 02:17 public
-rw-------  1 u51199 51199   249 May  7 02:12 Rakefile
-rw-------  1 u51199 51199   613 May  7 02:12 README.md
-rw-------  1 u51199 51199    31 May  7 02:12 .rspec
drwx------  7 u51199 51199  4096 May  7 02:12 spec
drwx------  3 u51199 51199  4096 May  7 02:18 tmp
drwx------  6 u51199 51199  4096 May  7 02:13 vendor

As a result, if you want to know when your app was last deployed, you can use File.mtime and get back a real Time object:

irb(main):009:0> File.mtime("app")
=> 2015-05-07 02:12:57 +0000
irb(main):010:0> File.mtime("app").class
=> Time
like image 35
Eliza Brock Marcum Avatar answered Sep 30 '22 12:09

Eliza Brock Marcum