从同一个 git repo 部署两个单独的 heroku 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41461517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:31:46  来源:igfitidea点击:

Deploy two separate heroku apps from same git repo

githeroku

提问by Don P

Within one git repo, I have two separate applications (web server and API server).

在一个 git repo 中,我有两个单独的应用程序(Web 服务器和 API 服务器)。

How can I deploy each application to its own Heroku app?

如何将每个应用程序部署到它自己的 Heroku 应用程序?

(So there are 2 heroku apps, one for the web server and one for the api server)

(所以有 2 个 heroku 应用程序,一个用于 Web 服务器,一个用于 api 服务器)

Note (before marking as duplicate):There are several questions similar to this. Most deal with deploying oneapp to twoheroku apps - typically for the purpose of staging vs. production. I am looking to deploy twoapps to twoheroku apps. (Question about staging vs prod)

注意(在标记为重复之前):有几个与此类似的问题。大多数处理将一个应用程序部署到两个heroku 应用程序 - 通常用于分期与生产。我希望将两个应用程序部署到两个heroku 应用程序。(关于 staging 与 prod 的问题

采纳答案by rdegges

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.

我对您的问题的理解是,您有一个 Git 存储库,其中包含两个完全独立的程序:一个 API 服务器和一个 Web 服务器。

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 Procfileat the root of your project. This will tell Heroku how to run your web server and your API server.
  1. 进入你的项目文件夹。
  2. Procfile在项目的根目录定义 a 。这将告诉 Heroku 如何运行您的 Web 服务器和 API 服务器。

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

以下是您可能想要的Procfile外观(示例):

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

In my example above: I'm defining two types of Heroku dynos -- one called weband 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.jsto start up my website, and node api/index.jsto start up my API service.

在我上面的例子中:我定义了两种类型的 Heroku dynos——web一种叫做api. 对于每一个,你都需要告诉 Heroku 运行什么命令来启动适当的服务器。在此示例中,我将运行node web/index.js以启动我的网站,并node api/index.js启动我的 API 服务。

  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 --remoteflag 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
    
  1. 创建两个新的 Heroku 应用程序。您可以通过heroku create <desired-app-name> --remote <desired-app-name>多次运行来做到这一点。注意:该--remote标志将告诉 Heroku 为同一个 repo 中的每个应用程序创建一个 Git 远程。

  2. 接下来,您需要告诉 Heroku 在一个 Heroku 应用程序上运行您的实际 Web 应用程序,并在另一个 Heroku 应用程序上运行您的 API 服务。你可以使用 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.
  • 为您的网络服务器 Heroku 应用程序运行单个网络 dyno。
  • 为您的 apiserver Heroku 应用程序运行单个 API dyno。

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

正如您在上面看到的,使用该ps:scale命令您可以控制 Heroku 将从您的 运行哪些类型的命令Procfile,以及您希望拥有的每个实例的数量。

Hopefully this helps!

希望这会有所帮助!

回答by laugri

The solution suggested by rdeggesunfortunately does not work anymore. See:

不幸的是,rdegges 建议解决方案不再适用。看:

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.

Web 进程类型很特殊,因为它是唯一可以从 Heroku 路由器接收 HTTP 流量的进程类型。其他进程类型可以任意命名。

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

来自Heroku 文档。所以,你将不能够有apiweb在Procfile都暴露Web应用程序。

Up-to-date solution

最新的解决方案

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

解决这个问题的正确方法是使用 Heroku 团队提供的这个 buildpack: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!

想象一下,您有一个代码库,其中包含几个不同的应用程序……或者至少能够运行几个不同的应用程序。或者,也许您是 Google 的单存储库?

无论如何,你如何在 Heroku 上管理这个?你没有。Heroku 应用程序对一个应用程序假设一个 repo。

进入 Multi Procfile buildpack,其中每个应用程序都会获得一个 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.

我已经在使用纱线工作区(一个存储库中的多个 Node 和 React 应用程序)的存储库上使用这个 buildpack 几个月了,一切正常。

回答by farincz

When you have to separate application then you can simply push repository sub-tree to each.

当您必须分离应用程序时,您可以简单地将存储库子树推送到每个应用程序。

Setup remotes once

设置遥控器一次

heroku git:remote --remote heroku-client -a client-app
heroku git:remote --remote heroku-server -a server-app

And then you can deploy through pushing sub-tree to the remote

然后你可以通过推送子树到远程部署

git subtree push --prefix client heroku-client master
git subtree push --prefix server heroku-server master

(where --prefix points to app's root folder)

(其中 --prefix 指向应用程序的根文件夹)