简体   繁体   English

将服务器部署到 heroku 开发和生产

[英]Deploy server to heroku both dev and prod

I have a Heroku app ready and working, it is connected to my github repo, and I have a Procfile on my Node.js (NestJS to be precise) server project.我有一个 Heroku 应用程序准备就绪并且可以工作,它已连接到我的 github 存储库,并且我的Node.js 项目项目中有一个 Procfile Procfile准确地说是 NestJS.server) At the moment what happens is every time I make a push to my repo project the heroku deploys the server with:目前,每次我推送我的回购项目时,heroku 都会部署服务器:

web npm run start:prod

My goal as part of the CI/CD proccess is to create 2 servers that are deployed:作为 CI/CD 过程的一部分,我的目标是创建 2 个部署的服务器:

  1. When a push to development branch is made - and in that case run a development server version当推送到development分支时 - 在这种情况下运行开发服务器版本
  2. When a push to master branch is made and in that case deploy the production server (2 different servers).当推送到master分支时,在这种情况下部署生产服务器(2 个不同的服务器)。

My question is what is the way to achieve this?我的问题是实现这一目标的方法是什么?

I guess I should create another Heroku app and connect it to the development branch of my repo, but how do I make sure that here I run a development version?我想我应该创建另一个 Heroku 应用程序并将其连接到我的 repo 的开发分支,但是我如何确保我在这里运行开发版本? Should i hold 2 different Procfile s on those branches?我应该在这些分支上持有 2 个不同的Procfile吗? Do i actually have to create another Heroku app or is there a better approach to this?我真的必须创建另一个 Heroku 应用程序还是有更好的方法?

Ideally you should never deploy development version of your app to a public environment, because among other things it may expose flaws in your security that can be later exploited on your production servers.理想情况下,您永远不应该将您的应用程序的development版本部署到公共环境,因为除其他外,它可能会暴露您的安全漏洞,这些漏洞以后可能会在您的生产服务器上被利用。

What you probably are talking about is what we refer to as staging environment.您可能正在谈论的是我们所说的staging环境。 It is an environment configured exactly like production , but where we test code before we deploy it onto production .这是一个与production环境完全一样配置的环境,但我们在将代码部署到production环境之前对其进行测试。

In terms of Heroku - yes you would in fact need two Heroku apps - one for production and one for staging .Heroku 而言- 是的,您实际上需要两个 Heroku 应用程序 - 一个用于production ,一个用于staging

Note that the staging environment should be running with NODE_ENV=production .请注意, staging环境应与NODE_ENV=production一起运行。

In terms of CI/CD, I have configured my CD to deploy to a different Heroku app based on which branch pushes are made to.在 CI/CD 方面,我已将我的 CD 配置为部署到不同的 Heroku 应用程序,具体取决于分支推送到哪个应用程序。 development goes to our staging app and master goes to our production app. development转到我们的staging应用程序, master转到我们的production应用程序。 (Although for simplicity I renamed the branches to staging and production :D). (尽管为简单起见,我将分支重命名为stagingproduction :D)。

I cannot give you a ready-made script for GitHub Actions, as I'm using GitLab and although CI/CD is very similar there, there are subtle differences, but GutHub Actions should have if conditions that work similar to:我无法为您提供 GitHub 操作的现成脚本,因为我正在使用 GitLab 并且虽然 CI/CD 在那里非常相似,但存在细微的差异,但 GutHub 操作应该具有以下if

if: github.ref == 'refs/heads/master'
 - dpl --provider=heroku --strategy=api --app=$HEROKU_PROD_APP_NAME --api-key=$HEROKU_PROD_API_KEY

Furthermore, you shouldn't need two Procfile s because the environments should be configured identically if you want to be sure that your code will behave the same on production.此外,您不需要两个Procfile ,因为如果您想确保您的代码在生产环境中的行为相同,则环境应该配置相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM