Embedding Git Revisions in Ember Apps Deployed to Heroku

There are a number of reasons that it’s a good idea to embed exact version information (down to the source control revision) in your application. When deploying an Ember app to Heroku, this takes a little extra effort.

## Ember App Versioning

[Ember-CLI] provides version reporting out of the box via a module cleverly named [ember-cli-app-version], allowing you to see both an app’s version and its current Git hash in the [Ember Inspector]:


This is what it looks like in development, anyway. I recently noticed that the Git hash was absent in our deployed app. It looked more like this:


## Where did it go?
I’ll save you the research and skip ahead to my findings. As you might expect, the Ember app inspects the filesystem to read the Git hash. Though we deploy to Heroku with [Git][heroku_git], it turns out that the repository is not available at build-time, so Ember is unable to slurp up the revision number.

Heroku builds execute via [buildpacks][buildpack_api]. Fortunately, Heroku had the foresight to provide an environment variable: the Git revision being deployed is available in `SOURCE_VERSION`. This leads us to the next question: how can we get build-time information into an Ember app?

## Solution

After some searching, I used a module called [ember-cli-inline-content](https://github.com/gdub22/ember-cli-inline-content) to embed both the local filesystem Git hash (for development/test) and `SOURCE_VERSION` (for Heroku deploys):

// ember-cli-build.js
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
var GitRepoInfo = require('git-repo-info');
module.exports = function(defaults) {
  var app = new EmberApp(defaults, {
    inlineContent: {
      'build-time-git-hash': { // not available on Heroku
        content: GitRepoInfo().sha || "-"
      'source-version': { 
        content: process.env.SOURCE_VERSION || "-"
  return app.toTree();

I then tucked them into a comment in index.html like this:

## Conclusion
If you’re not already embedding exact revision information in your app, I recommend that you do!

The version number alone may be enough for many. If you don’t always rigorously maintain your version number, though, or if you deploy to staging environments between releases, embedding the Git hash will likely prove useful.

Longer-term, it’d probably be better to reconcile Ember-CLI’s preferred versioning method with the reality of Gitless deployment environments, but for now this does the job.

[git-repo-info]: https://github.com/rwjblue/git-repo-info
[ember-cli-app-version]: https://github.com/embersherpa/ember-cli-app-version
[heroku_git]: https://devcenter.heroku.com/articles/git
[buildpack_api]: https://devcenter.heroku.com/articles/buildpack-api
[ember inspector]: https://chrome.google.com/webstore/detail/ember-inspector/bmdblncegkenkacieihfhpjfppoconhi?hl=en
[Ember-CLI]: http://ember-cli.com/