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.
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.
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)'
git-config(1)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With