75

I know you can deploy automatically to heroku from github, but I haven't found a way to only push a subfolder from github to heroku.

From the command-line I know it is possible to do this with:

git subtree push --prefix <subfolder> heroku master

However, I would like to know if there is a way to use the github integration with heroku to make it pull a specific subfolder automatically when a commit is added to a branch.

nkmol
  • 8,025
  • 3
  • 30
  • 51
Divino Neto
  • 798
  • 1
  • 6
  • 8

9 Answers9

201

2018 Update! To enable automated deployments with heroku, you need to have admin access for the github repo you want to deploy.

In the Heroku dashboard:

Step 1) - Connect github repository to Heroku

Inside the Deploy tab, scroll to Deployment method and connect your Github account. find the repo and hit connect. heroku should be authorized as an Oauth app in your github now.

If you cannot find the repo: Either your github has not authorized Heroku or you did not create this repo and need to make sure you have admin access to it. If this is not possible, invite an admin of the repo to your heroku app by going to Access tab in Heroku Dashboard and adding the admin as a collaborator. The admin then logs into heroku and completes Step 1 here. Afterwards you can finish the following steps.

Step 2) - Set Heroku Config Var PROJECT_PATH to your server folder

Inside the Settings tab, set a config var to tell Heroku the path to find the server code you want deployed.

Example: lets say your repo name is MyRepo and it has 2 sub-folders. back-end contains a Node.js server and front-end contains a React app. Your github directory looks like this:

MyRepo/front-end/package.json MyRepo/back-end/package.json

Then you should set your config var to PROJECT_PATH in the left box and back-end in the right box.

Step 3) - Set a Heroku Buildpack that will deploy the PROJECT_PATH folder

Again inside the Settings tab, you need to add a Buildpack that will tell heroku to look for your folder instead of deploying the repo root. There should already be 1 buildpack there to tell heroku what type of server it is (javascript/node.js, python/django, etc...).

Enter this url to add buildpack https://github.com/timanovsky/subdir-heroku-buildpack.git and make sure this is at the top of the buildpack chain (drag the lines on the left to make it above any other buildpacks you have added.

Step 4) - Enable auto deploy

Inside the Deploy tab, scroll to Automatic Deploys and click the black button to enable automatic deploys

Auto Deploy complete! Now check the build logs and make sure you don't have any errors

noxasaxon
  • 2,263
  • 1
  • 8
  • 14
  • 1
    How can the front end reach the back end? – Lupyana Mbembati Jun 14 '19 at 09:16
  • 2
    @LupyanaMbembati this guide is strictly related to continuously deploying a single directory in a multi-directory github repository. If you want to connect a front end and back end, you could use a heroku pipeline or multiple heroku apps, serving up front-end and back-end from different apps and using REST to connect the two. Personally I use netlify for front-end and heroku for back-end, but sometimes i like to have all the code in a single repository. – noxasaxon Jul 03 '19 at 17:03
  • Hi how can I use heroku pipeline to connect front end and backend - if I'm uploading all in one repo with two folders as above? @noxasaxon – Akhila Dec 25 '20 at 20:18
  • 1
    @Akhila as noxasaxon mentioned, best to host frontend off of Netlify, and backend off of Heroku. Both can be in the same git repository, as Netlify also supports subfolders: https://docs.netlify.com/configure-builds/file-based-configuration/#sample-file (see "base" prop) – Marko Bjelac Jan 08 '21 at 15:45
  • from the [readme of the buildpack repo](https://github.com/timanovsky/subdir-heroku-buildpack.git): `The buildpack takes subdirectory you configured, erases everything else, and copies that subdirectory to project root. Then normal Heroku slug compilation proceeds.` . isn't it a little risky? it might remove uncommitted frontend changes if we try to deploy a backend – ansh sachdeva Aug 21 '21 at 10:45
  • For what it's worth, our team (Ruby on Rails) found that Rails + HerokuCI didn't work. The testing database on HerokuCI has errors and issues that we couldn't get past. We also tried a bunch of other Heroku monorepo plugins to no avail. We had to abandon Heroku + Monorepo if we wanted to keep HerokuCI. – Will Taylor Apr 06 '23 at 15:19
9

I was able to make it work. I have a server subfolder with Python Flask app and I wanted to deploy it automatically using GitHub integration.

Heroku uses buildpacks to detect the language & framework of your project. More about that here.

I found the source code for my buildpack here. Then you just need to look at the detection script. For python it checks the requirements.txt file, so I made a symlink using ln -s server/requirements.txt requirements.txt.

My Procfile looks like this: web: gunicorn --pythonpath server/api app:app.

Everything works now!

sap1ens
  • 2,877
  • 1
  • 27
  • 30
6

What I did in order to have automatic deployments from a subfolder was to create a new branch in GitHub and push JUST the subfolder and then set Heroku to auto-deploy to that branch.

Use git subtree push --prefix <subfolder> origin <branch> to push the subfolder into that branch

Baruch
  • 2,381
  • 1
  • 17
  • 29
  • 1
    Worth noting that this causes issues with applications that depend on sibling packages/workspaces. – Jay Figaro Mar 31 '21 at 01:57
  • 1
    Worth noting that this was a solution from 4 years ago and should be taken with a grain of salt. – Baruch Mar 31 '21 at 08:16
  • 2
    Apologies if my tone was hostile–left comment here because googling for solutions to this problem today results in (current/to-date) use of `git subtree x`. Figured it was worth the mention for other wanderers considering it. – Jay Figaro Apr 01 '21 at 18:06
3

Test this solution:

In the enviroment variables set

PROJECT_PATH 

to

<repo relative path to the subfolder>

Why? I notice that

git subtree push --prefix <subfolder> Heroku master

does this automatically, and there is a high chance that Heroku does this to set the path of the subfolder. With this you can set the repo to auto-deployment and declare the subfolder path. They should really work on making this more obvious.

Royer Adames
  • 868
  • 9
  • 13
  • You're on the right track, but that won't work without installing a buildpack that supports this first. This one worked for me: https://github.com/timanovsky/subdir-heroku-buildpack – Fred Sep 22 '22 at 17:36
1

I think right now there is an issue in this answer from noxasaxon

On step 2) in the PROJECT_PATH you shouldn't put MyRepo/front-end/package.json . but instead in the path variable just front-end and it will work. More info here https://github.com/timanovsky/subdir-heroku-buildpack/issues/5

João G.
  • 43
  • 4
0

The easy way to deploy a repo with a server in subdirectory:

  • Copy package.json from server directory to root directory
  • Change “node server.js” to “node subdirectory/server.js”

Now, you can choose your repo in Heroku/Deploy to deploy and use.

  • 1
    This won't help in a monorepo. What if I have several sub-directories that I want to deploy into different apps? – Fred Sep 22 '22 at 17:29
0

Heroku cli Subfolder Deployment :

In my full-stack project the front and backend are in separated folders, I build the frontend in a public folder inside the backend folder and I needed to deploy the backend folder, to do so:

First, create a subtree for the backend folder to a separate branch (master) in my case.

git subtree split -P almarket/almarket-api -b master

Then push it to heroku using --force annotation

git push --force heroku master

Don't forget to delete the subtree git branch at the beginning of each deployment using.

git branch --delete -D master

Note: Heroku uses only main or git master branches for deployment, so if your root git branch is main use the following commands, but if your root git branch is master replace master with main

mkamal
  • 199
  • 2
  • 6
0

Here is the simplest solution:

First, download subdir-heroku-buildpack using Heroku CLI:

heroku buildpacks:add -a *YOUR_APP_NAME* https://github.com/timanovsky/subdir-heroku-buildpack

Second, set a new env variable named PROJECT_PATH with the relative directory

heroku config:set -a *YOUR_APP_NAME* PROJECT_PATH=./sub-directory

In Heroku Settings (UI) - Make the subdir buildpack first

enter image description here

That's it, you are done!

Or Nakash
  • 1,345
  • 1
  • 9
  • 22
-1

The simplest way by far to deploy a subdirectory is to instead use Netlify -- Log in, then drag your root folder into drag and drop area under "Sites", then under Settings -> Build & Deploy -> Continuous Deployment -> Build settings (connect Netlify to Github repo first) youll enter the subdirectory name into Base Directory field (no slashes), then CI= npm run build for Build Command, and finally yourSubdirectoryName/build for Publish Directory field. Save and then trigger another deployment.

xv8
  • 183
  • 2
  • 11