Automated heroku deploy from subfolder

GitHerokuGithubDeploymentWeb Deployment

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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/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

Solution 2 - Git

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!

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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.

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDivino NetoView Question on Stackoverflow
Solution 1 - GitnoxasaxonView Answer on Stackoverflow
Solution 2 - Gitsap1ensView Answer on Stackoverflow
Solution 3 - GitBaruchView Answer on Stackoverflow
Solution 4 - GitJoão G.View Answer on Stackoverflow
Solution 5 - GitRoyer AdamesView Answer on Stackoverflow
Solution 6 - Gitxv8View Answer on Stackoverflow