Skip to content

Instantly share code, notes, and snippets.

@caprosset
Last active August 17, 2020 13:57
Show Gist options
  • Save caprosset/b64d84677dbca20bf9642160367656c4 to your computer and use it in GitHub Desktop.
Save caprosset/b64d84677dbca20bf9642160367656c4 to your computer and use it in GitHub Desktop.
Heroku deployment - M2

PROJECT2 DEPLOYMENT ON HEROKU

1. COMMIT AND PUSH YOUR LAST CHANGES

1.1. While on develop branch - Commit on develop

$ git add .
$ git commit -m 'Commit message'

1.2. Checkout to master

$ git checkout master

1.3. Merge from develop to master and push to your Github repo

$ git merge develop
$ git push origin master

   

2. IN YOUR CODE, CHANGE THE VARIABLES

2.1. Make sure you have the package 'dotenv' installed
In package.json, it should be installed it as a 'dependency'. If you don't see it in the manifest, install it again with the following command:

$ npm i dotenv --save

 

2.2. In the .env file, create the following variables:

PORT=3000
MONGODB_URI=mongodb://localhost:27017/replace-with-your-db-name
SESSION_SECRET=replace-with-your-session-secret

 

2.3. In app.js (and in your seed file if you have one)

  • require the 'dotenv' module on top of your file:
require('dotenv').config();
  • and replace your database name and session secret with the corresponding env variables names:
// MONGOOSE CONNECTION
mongoose.connect(process.env.MONGODB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
// SESSION MIDDLEWARE
app.use(
  session({
    secret: process.env.SESSION_SECRET,
    resave: true,
    saveUninitialized: false,
    store: new MongoStore({
      mongooseConnection: mongoose.connection,
      ttl: 60 * 60 * 24 * 7,
    }),
  })
);

 

2.4. Make sure that the '.env' file has been added to your '.gitignore' file !!

   

3. REGISTER AND CONFIGURE YOUR HEROKU ACCOUNT

3.1. Signup for a Heroku account
You will need to verify your email.

 

3.2. Download and install Heroku
In your terminal, run the command that you will find here corresponding to your OS, or download the installer. There is another way to install it with NPM here but it is not recommended, so try to install it with one of the above methods.

 

3.3 Check if everything is working correctly
In your terminal, run:

$ heroku --version

You should get the printout of the installed version of Heroku on your machine.
You are now able to run every heroku related command in your terminal (CLI) with the 'heroku' keyword.

 

3.4. Connect your CLI to your Heroku account
Run the following command:

$ heroku login

Type 'enter' and you will be redirected to a browser window where you'll be able to login to your Heroku account.

   

4. CREATE THE APP IN HEROKU

4.1. Go to your dashboard, and click on 'New app'

  • Give a name to your app (will be used as a url to your deployed app, so choose something meaningful)
  • Choose region: 'Europe'
  • Click on "Create"

You'll get to the application panel of your app: https://dashboard.heroku.com/apps/{NAME-OF-YOUR-APP-HERE}/deploy/heroku-git

 

4.2. Connect Heroku to your local repo
In the root folder of your project in the terminal, run the following command to add heroku remote (connect your local to your app on heroku):

$ heroku git:remote -a name-of-the-app

Check the remotes that have been added to your project. You should see a new remote 'heroku' in addition to 'origin' (Github):

$ git remote -v

 

4.3. Deploy to Heroku

First, commit your last changes on 'master' branch and push them to Github:

$ git add .
$ git commit -m 'Update env variables'
$ git push origin master

then deploy them as well to Heroku pushing them to your 'heroku' remote:

$ git push heroku master

 

4.4. Setup the config variables in Heroku
The code of your application is now pushed to Heroku. The only thing that has not been pushed is your '.env' file (because you have added it to the .gitignore file).
From your Heroku dashboard, go to > Settings > Reveal Config Vars: only add the SESSION_SECRET with its corresponding value.

To get the DB NAME variable from the MongoDB Cloud solution (MongoDB Atlas) we will use on our deployed app, follow these steps:

  • In your terminal, run the following command:
$ heroku addons:create mongolab:sandbox
  • When your run this command for the first time, Heroku will ask your credit card details. It will not charge you (free account) but you still need to do it.
  • Click on the link to fill up your credentials, Heroku will need to verify your credentials.
  • Run this command one more time:
$ heroku addons:create mongolab:sandbox
  • Once run, Heroku will create the new database. You should get a success message: "Created mongolab-..... as MONGODB_URI"
  • In your Heroku dashboard, go to > Settings > Reveal Config Vars: you should see that Heroku has added this new var for your, in the value field of MONGODB_URI.
  • Click on "Open app" (in the top right menu in Heroku dashboard) to see your deployed app

If this doesn't work, see "workaround for getting MONGODB_URI" at the end of this file.

   

USEFUL COMMANDS

  • If the deployed app throws some errors, run the following command in your terminal:
$ heroku logs --tail
  • To open the terminal in the container of the running app:
$ heroku run bash

   

Workaround for getting MONGODB_URI

If you are a brand new user:

  • Signup: Register on MongoDB Atlas
  • Build your first cluster:
    • Select "Create a cluster" (FREE option)
    • Select "AWS" as cloud provider, Europe (Frankfurt) as the region, "Cluster tier" (free) as the solution, and click on "Create cluster"
  • Create your database user:
    • You should have been redirected to the console panel > Select "Database Access" in the left side menu and there > click on "Add new user database"
    • In the section Password authentication, add a username and click on "augogenerate Secure Password" and copy-paste it in a secure place right away (you will need it for later)
    • In the "Database User Privileges", select "Read and write to any database" (give username and pwd)
    • Click on "Create new user"
  • Whitelist your IP address:
    • Go to "Network Access" in the left side menu
    • Once on that page, click on the big "Add IP Address" green button
    • In the pop-up that appears, select "Allow Access From Anywhere" and click "Confirm" (it you already had it setup from before, the IP Address should appear already in the IP Whitelist)
  • Connect to your cluster:
    • Go to "Clusters" in the left side menu: in the sandbox, click on "Connect" and in the pop up that opens, click on "Connect your application" > Select DRIVER: "Node.js" and VERSION: "3.0 or later" > Copy the "connection string" that has been prepared for you
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment