Deploy Git subdirectory to Heroku
Here’s how you can deploy only your git subdirectory/subfolder to Heroku using git-subtrees 🌳.
The Situation
So why not just deploy the whole repo?
You might want to only deploy a subdirectory if the rest of the repo contains folders that are unrelated or unnecessary for your app to run. (Make sure that the subdirectory you’re building is truly independent first!)
Or in my case, you might need to only deploy the subdirectory because the repository might not be something you can actually build:
remote: Building source:
remote:
remote: ! No default language could be detected for this app.
remote: HINT: This occurs when Heroku cannot detect the buildpack to use for this application automatically.
remote: See https://devcenter.heroku.com/articles/buildpacks
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to app-name.
remote:To https://git.heroku.com/app-name.git
Feels bad to be rejected…
I even tried to tell Heroku which default language to use:
heroku buildpacks:set heroku/nodejs
For Node.js specifically, this means that Heroku couldn’t find a package.json:
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> App not compatible with buildpack: https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/nodejs.tgz
remote: Node.js: package.json not found in application root
remote:
remote: More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to my-app.
remote:To https://git.heroku.com/my-app.git
The Solution
Probably what you’re here for
Heroku Setup
npm install -g heroku
heroku login
heroku git:remote -a my-app
🌳 Git-subtree 🌳
Make sure to run this from the top level of the tree:
git subtree push --prefix path/to/subdirectory heroku master
For example, if the subdirectory I want to deploy my-app is in the root my-repo, then I run:
git subtree push --prefix my-app heroku master
And then…
remote: Building source:
remote:
remote: -----> Node.js app detected
...
remote: Verifying deploy... done.
To https://git.heroku.com/my-app.git
🎉🎉 Beautiful 🎉🎉
Other Resources
- https://stackoverflow.com/questions/7539382/how-can-i-deploy-push-only-a-subdirectory-of-my-git-repo-to-heroku
- https://coderwall.com/p/ssxp5q/heroku-deployment-without-the-app-being-at-the-repo-root-in-a-subfolder
- https://www.atlassian.com/blog/git/alternatives-to-git-submodule-git-subtree
- https://medium.com/@porteneuve/mastering-git-subtrees-943d29a798ec