How to Deploy a Subdirectory to Heroku

Oct 19, 2020

A quick guide on how to deploy a subdirectory of a repo to Heroku

Over the weekend, I decided to make some docs for a GraphQL API I had made. However, I wanted the repo for the documentation site to be in the same GitHub repo as my API's repo since they're directly related. My server was originally deployed on Heroku, but the build failed as soon as I moved the repo into a subdirectory. How do you fix this?

Heroku CLI

First, make sure you have the Heroku CLI installed. You can find the installation instructions here. Once you have it installed, log into your Heroku account:

BASH
heroku login

You'll be redirected to the Heroku website. After a successful login, you can return to your terminal. Now we need to remotely set up our application on Heroku:

BASH
heroku git:remote -a my-app

Replace my-app with the name of your application. The build on Heroku is going to fail since Heroku can't handle a subdirectory on its own. To fix that, we can use git subtree:

BASH
git subtree push --prefix my-subdirectory heroku master

Replace my-subdirectory with the name of the folder you want to deploy that is in the root of your repo.

If your main deploy branch is main instead of master, adjust the script accordingly. If you need to switch from master to main, you can follow the Heroku instructions here.

Let's go to the browser and make everything worked. Navigate to your app's dashboard on Heroku and navigate to the "Deploy" tab. Double check and make sure the "Deployment method" selection is "Heroku Git".

If all looks good, you'll see a "Build succeeded" message on the "Overview" page under the "Latest Activity" feed. Click on the "Open App" button in the upper righthand corner to check out your newly deployed subdirectory!