0

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. At the moment what happens is every time I make a push to my repo project the heroku deploys the server with:

web npm run start:prod

My goal as part of the CI/CD proccess is to create 2 servers that are deployed:

  1. When a push to development branch is made - and in that case run a development server version
  2. When a push to master branch is made and in that case deploy the production server (2 different servers).

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? Should i hold 2 different Procfiles on those branches? Do i actually have to create another Heroku app or is there a better approach to this?

Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
Gabi Gatzki
  • 83
  • 1
  • 8
  • look for heroku pipelines https://devcenter.heroku.com/articles/pipelines – gui3 Aug 11 '20 at 12:46
  • That is slightly different, Heroku pipelines are about promoting new code to production, not two separate branches, although it still a decent solution. – Itay Grudev Aug 11 '20 at 12:47

1 Answers1

0

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.

What you probably are talking about is what we refer to as staging environment. It is an environment configured exactly like production, but where we test code before we deploy it onto production.

In terms of Heroku - yes you would in fact need two Heroku apps - one for production and one for staging.

Note that the staging environment should be running with 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. development goes to our staging app and master goes to our production app. (Although for simplicity I renamed the branches to staging and production :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:

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 Procfiles because the environments should be configured identically if you want to be sure that your code will behave the same on production.

Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
  • Hey thanks for the answer, but if I set the staging to production as well what is the difference? What I'm trying to achieve at the end is a server url which I can make api calls to and I don't care spamming it with data since it will be connected to a "testing" db, while the production will be connected to the real db – Gabi Gatzki Aug 12 '20 at 13:02
  • The idea for `staging` is to have a testing environment that is identical to `production`, but uses a different database, so you can test your releases before deploying them onto `production`. Often individual developer machines have different setups, different libraries. The database lives on the same machine, file storage is local instead of on the cloud. There are many problems you can't catch in development, but you can in a `staging` environment. We set the `NODE_ENV=production` to make sure the production-like code runs there, but we connect it to a separate staging database. – Itay Grudev Aug 13 '20 at 09:57
  • How do i make it connect to a different db? Iam using config to set the db data cardentials (host, port, username etc.) and when i run it with NODE_ENV=production it will connect to a specific db, how do i connect it to a differnt db in staging with NODE_ENV=production? – Gabi Gatzki Aug 13 '20 at 10:05
  • You should never place your access credentials in your code - it's a security risk. Instead use environment variables. You can set environment variables through the app settings in Heroku and there you can specify a different `DATABASE_URL` for each app. This way your different apps (`staging` and `production)` will have different database configurations even though they are running the same code. Additionally a mistake in your code won't cause the `staging` app to use the `production` database, because it wouldn't have a clue as to what its access credentials are. – Itay Grudev Aug 13 '20 at 10:38