How can I display the “last deployed” timestamp from my Heroku Rails application?

I would like to update an environment variable or something to keep track of the deployment time that is currently active. Is there a way to do this automatically from my Heroku application, or do I need to do this as part of a script deployment? Ideally, I would like something that will work with me using TDDium for CI, and letting them do the same for Heroku when the build goes through.

+6
source share
3 answers

1. When deploying to Heroku, there is no automatic method unless you deploy the script / task. (I searched for this also in June 2012). I have a rake task that performs a deployment, part of which sets GIT_TAG and my webpage (application layout in rails) prints this on the page.

Here's how I write to the Heroku GIT_TAG var configuration configuration (using the Rake-based Rake task):

tag = `git describe master --always`.strip `heroku config:add GIT_TAG=#{tag} --app XXXX` 

2. With tddium: tddium now supports "post build hook", and I am increasing their standard version to set GIT_TAG during this process. first read and execute http://blog.tddium.com/2012/05/09/heroku-continuous-deployment/ , and add something to the post_build_hook task to read the tag and set the heroku var configuration as shown:

  namespace :tddium do def cmd(c) system c end desc "post_build_hook to deploy to dfc-site-qa" task :post_build_hook do ...use code verbatim from above URL (https://www.tddium.com/support/reference#customization) ... current_tag = `git describe master --always`.strip cmd "heroku config:add GIT_TAG=#{current_tag} --app XXXX" or puts "could not set GIT_TAG to #{current_tag}" ... end 

Notes:

  • If necessary, replace the “master” in the above. In my tasks, expand the rake (happy to share), I allow deployment based on a branch or tag (convenient for bypassing the hero only for deployment from the wizard).

  • For tddium to launch your post_build_hook, you must deactivate the URL after deployment by doing the following: tddium suite --edit

    and use the current value for "pull url", but set the "push url" to empty (or by default). This step was not mentioned in the blog link.

  • On your tddium homepage for the current build, you will see a link to the post_deploy_hook log file (at the very bottom of the page) that you can open and see how it arrived (for example, debug your rake task).

+1
source

Use environment variables on Heroku

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

 heroku config:add DEPLOY_TIMESTAMP=$(date) 

You can then access this environment variable from your application or from the command line. The value can be accessed using ENV['DEPLOY_TIMESTAMP'] from the Rails application or by analyzing the output of heroku config from the local project directory.

Alias ​​Automation

If you want to automate this somewhat, you can create a Git alias to click on Heroku and update DEPLOY_TIMESTAMP at the same time. Note that you cannot overwrite the names of real Git commands, such as push, but you can add a custom action such as pushstamp. For instance:

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

see also

git -config (1)

+8
source

You can find the time of the last deployment by looking at the timestamps of the files in the read-only Heroku file system.

You can verify this by looking at these timestamps directly with ls. Example run 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 the last application was deployed, you can use File.mtime and return the actual 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 
+1
source

Source: https://habr.com/ru/post/919090/


All Articles