Deploy a Subdirectory to Heroku as an app

BranchesThis is mainly to share a discovery a coworker and I made while trying to figure out how to deploy a project they were working on after a transition to the single git repo for this project.

Normally when working with heroku you just execute the following and bam, you’re up and serving your code on a dyno.

git push heroku master

But with the way things have been setup now, our git repository now looks like this:

↳ abracadabra-app
↳ fabulous-project
↳ spiderman-project

And we have a problem. Doing the deployment as before would push all these directories and our apps won’t show up the way we want. They might not even deploy nicely, which would be terrible!

Git is a pretty powerful tool and the things you can do with it are pretty much limitless. In the newer versions of git (at least git 1.8.x) has this feature called the git subtrees. They kind of evolved as an alternative way to git submodules, so they can be used for stuff like including project dependencies.

But git is just a tool, and while the regular use case for a subtree might be managing other dependencies, we can “trick” (if you want to call it that) heroku into thinking it’s a git repo. By specifying where the subtree is we can treat that as a repository and push that repository up to heroku.

One advantage of this single repo for the team I’m on is it gives us a single location to look at all our projects in progress, open pull requests and get feedback and using the subtree strategy allows each collaborator to deploy their respective project to heroku as easily as deploying a normal rails app, albeit with a few changes to the deployment incantation.

So, how do I deploy? essentially what you are going to do is deploy that prefixed subtree to heroku as master. This is achieved by composing a few discrete git commands:

git push heroku `git subtree split --prefix fabulous-project master`:master

Aside from the code inside the backticks, deployments look almost the same. All we’ve done is changed where our master branch is coming from, sending that off to our remote (heroku) in this case.

If you’re looking to learn more and perhaps get a stronger understanding of what’s going on when using git subtree you can check out some of the following links, that I semi-butchered: