3

I have some source code managed by git that I want to push to Heroku. The source code contains multiple different products, some of which will have their own heroku instance. So something like this:

src/
 lib-files/
 app1/
 app2/
 app3/
 other-files/
 package.json

So in heroku, I have an instance for app1 and app2. Both of these rely on ./lib-files and package.json but not on app2 or app3 or other-files.

I don't really want to push the entire git repository containing all the files to heroku for each app. In other words, I don't want the app1 instance to contain the code for app2 or app3 or other-files because there is a lot of data that it doesn't need and it takes for ever to copy it up.

I got around this by creating new git repos for each app (one that just contains the stuff app1 needs and one for app2, etc) and then pushing those individual repos to heroku. This works, but seems a bit hacky because I have all these other extra repositories that I have to manage. Also, if other devs want to be able to push to heroku I need to make those repos available to them, maybe by checking them into git (seems bizarre).

What is a good way to accomplish this?


Update:

What if I created the heroku repos so they only contained the appropriate stuff for each app then just let heroku be the primary remote for those repos (as opposed to github)? I would still have multiple repositories to deal with, but at least all the devs would have a standard place from which to clone them (the source code repo being in github and the heroku deployment repos being in heroku). Thoughts on that?

d512
  • 32,267
  • 28
  • 81
  • 107

3 Answers3

5

The solution I would choose is branching. You have the master branch that is containing all files, and then a branch for each application that only contain what it needs.

For example:

$ git init
$ git add .
$ git commit -m "Initial commit"
$ git checkout -b app1   # create a new branch for app1 derived from master
$ git rm what-you-dont-need-for-app1
$ git commit -m "Initial app1 commit"
$ git checkout master    # switch to master again
$ git checkout -b app2
$ # repeat same steps as for app1

You can also have a unique .gitignore-file in each branch to tell git what you don't want to commit.

Jocke
  • 673
  • 3
  • 8
1

The way I ended up doing this was to let heroku house the primary remotes for the versions of the apps that are deployed up there. Meanwhile, the main source code repo that everyone works on is up in github. Assume that the code looks like this:

src/
 app1/
 app2/
 app3/
 lib-files/
 other-files/
 package.json

Then I have two heroku apps for app1 and app2. These house just the code needed for each app:

app1:

src/
  app1/
  lib-files/
  other-files/
  package.json

app2:

src/
  app2/
  lib-files/
  other-files/
  package.json

When you want to work on the source code you clone the full version from github. When you want to deploy to heroku you clone one of the app repositories from there.

I created a script for deploying each app that does the following:

  1. Clone the git repo from heroku if it doesn't already exist on the local box
  2. Copy just the files needed for just that app from the main source repo to the heroku repo
  3. Commit the changes
  4. Push the repo to heroku

One issue that I ran into is that each of my apps has their own Node.js application and I normally start each one in my development environment by going into that directory and running the node command:

cd app1
node server.js

However, this doesn't quite work that way on Heroku because it expects the Procfile and package.json file to be in the outer most directory of the git repo. My package.json file is already located there, but I had to create separate Procfiles for each application and copy them around depending on which one I'm deploying. They each look something like this inside:

web: node app1/app.js

Anyway, this setup seems to work.

d512
  • 32,267
  • 28
  • 81
  • 107
0

I had the exact same problem (as I guessed many do), and chose differently: I have a separate repository for each app, and a repository for the shared code. I share code between the apps locally on disk and on Heroku via requirements.txt. Here is my SO thread:

Sharing Python code between Heroku apps

Community
  • 1
  • 1
Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54