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:

ember_version_with_hash

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:

ember_version_without_hash

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, 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. 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 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.