Last Updated: September 30, 2021
·
48.91K
· djm

Heroku deployment without the app being at the repo root (in a subfolder)

In the words of Heroku themselves:

Heroku apps expect the app directory structure at the root of the repository. If your app is inside a subdirectory in your repository, it won’t run when pushed to Heroku.

This is obviously a problem if you've already created your project structure and don't particularly feel it's ok that Heroku dictate you change it.

So..to fix it, we use the new(ish) git subtree module which was committed to the mainline git branch in April 2012. This basically allows us to push just a subdirectory of your repo to heroku during deployment.

Given this project structure:

--/
----/.git
----/build
----/docs
----/pythonapp
----/----/requirements.txt

From the root of your repo, you can run the following command:

git subtree push --prefix pythonapp heroku master

This will push just the pythonapp folder to the remote.

If you wish to read more about the subtree module, this article is a great start as it's from the author of the module itself.

2 Responses
Add your response

If you come across the "Updates were rejected because the tip of your current branch is behind. Merge the remote changes (e.g. 'git pull')" problem when you're pushing (due to whatever reason, esp screwing about with git history) then you'll need to nest git commands so that you can force a push to heroku. e.g, given the above example:

git push heroku `git subtree split --prefix pythonapp master`:master --force
over 1 year ago ·

Being lazy i created a pushy.sh script in my ruby app to repeatedly call this command. However, it will only work from the root of the project, and not the sub folder your ruby app is in. So for ruby/app/pushy.sh to run it had to call the following.

(cd ../../ && git subtree push --prefix www/tutapp heroku master)

The git commands to run from a different directory didn't work for me. i.e. i couldn't get the following to work.

git --git-dir=../../.git --work-tree=../../ subtree <etc. etc>
over 1 year ago ·