Deploy two separate heroku apps from same git repo

The solution suggested by rdegges unfortunately does not work anymore. See:

The web process type is special as it’s the only process type that will receive HTTP traffic from Heroku’s routers. Other process types can be named arbitrarily.

from the Heroku documentation. So you won't be able to have api and web in a Procfile both exposing web apps.

Up-to-date solution

The correct way to tackle this is to use this buildpack provided by the Heroku team: Heroku Multi Procfile buildpack:

Imagine you have a single code base, which has a few different applications within it... or at least the ability to run a few different applications. Or, maybe you're Google with your mono repo?

In any case, how do you manage this on Heroku? You don't. Heroku applications assume one repo to one application.

Enter the Multi Procfile buildpack, where every app gets a Procfile!

I've been using this buildpack for multiple months now on a repository using yarn workspaces (multiple Node and React apps in one repo) and everything works fine.


My understanding of your question is that you have one Git repository, that contains two entirely separate programs: one API server, and one web server.

With this assumption in mind, here's what you'll want to do, step-by-step:

  1. Go into your project folder.
  2. Define a Procfile at the root of your project. This will tell Heroku how to run your web server and your API server.

Here's how you might want your Procfile to look (an example):

web: node web/index.js
api: node api/index.js

In my example above: I'm defining two types of Heroku dynos -- one called web and one called api. For each one, you'll need to tell Heroku what command to run to start the appropriate server. In this example, I would run node web/index.js to start up my website, and node api/index.js to start up my API service.

  1. Create two new Heroku applications. You can do this by running heroku create <desired-app-name> --remote <desired-app-name> multiple times. NOTE: The --remote flag will tell Heroku to create a Git remote for each of your applications in the same repo.

  2. Next, you'll need to tell Heroku to run your actual web application on one Heroku app, and your API service on another Heroku app. You can do this by using the Heroku CLI:

    $ heroku ps:scale web=1 --remote webserver-app-name
    $ heroku ps:scale api=1 --remote apiserver-app-name
    

These commands will:

  • Run a single web dyno for your webserver Heroku app.
  • Run a single API dyno for your apiserver Heroku app.

As you can see above, using the ps:scale command you can control what types of commands Heroku will run from your Procfile, and how many instances of each you'd like to have.

Hopefully this helps!

Tags:

Git

Heroku