You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+118Lines changed: 118 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,7 @@ We've used this `builderbook` project to build [saas](https://github.com/async-l
23
23
-[Add a new book](#add-a-new-book)
24
24
-[Add your own styles](#add-your-own-styles)
25
25
-[Deploy](#deploy)
26
+
-[Deploy to Heroku](#deploy-to-heroku)
26
27
-[Scaling](#scaling)
27
28
-[Screenshots](#screenshots)
28
29
-[Built with](#built-with)
@@ -236,6 +237,9 @@ We also specified styles for all content inside a `<body>` element:
236
237
237
238
238
239
## Deploy
240
+
241
+
IMPORTANT: Now v1 is depreciated for new users. See the next section about deploying to Heroku.
242
+
239
243
- Install now: `npm install -g now`.
240
244
- Point your domain to Zeit world nameservers: [three steps](https://zeit.co/world#get-started).
241
245
- Create `now.json` file. Make sure to add actual values for `GA_TRACKING_ID`, `StripePublishableKey` (production-level) and `alias`. Read more about how to [configure now](https://zeit.co/docs/features/configuration).
@@ -263,6 +267,120 @@ We also specified styles for all content inside a `<body>` element:
263
267
- Now outputs your deployment's URL, for example: `builderbook-zomcvzgtvc.now.sh`.
264
268
- Point successful deployment to your domain with `now alias` or `now ln NOW_URL mydomain.com` (`NOW_URL` is URL of your deployment).
265
269
270
+
## Deploy to Heroku
271
+
272
+
In this section we will learn how to deploy our app to [Heroku cloud](https://www.heroku.com/home). We will deploy our React-Next-Express app to lightweight Heroku container called [dyno](https://www.heroku.com/dynos).
273
+
274
+
Instructions are for app located at `/book/8-end`.
275
+
Adjust route if you are deploying app from the root of this public repo.
276
+
277
+
We will discuss the following topics in this section:
278
+
1. installing Heroku on Linux-based OS
279
+
2. creating app on Heroku dashboard
280
+
3. preparing app for deployment
281
+
4. configuring env variables
282
+
5. deploying app
283
+
6. checking logs
284
+
7. adding custom domain
285
+
286
+
Let's go step by step.
287
+
288
+
1. Install Heroku CLI (command-line interface) on your OS. Follow the [official guide](https://devcenter.heroku.com/articles/heroku-cli). In this book we provide instructions for Linux-based systems, in particular, a Ubuntu OS. For Ubuntu OS, run in your terminal:
289
+
<pre>sudo snap install --classic heroku</pre>
290
+
To confirm a successful installation, run:
291
+
<pre>heroku --version</pre>
292
+
As example, my output that confirms successful installation, looks like:
293
+
<pre>heroku/7.22.7 linux-x64 node-v11.10.1</pre>
294
+
295
+
2.[Sign up](https://signup.heroku.com/) for Heroku, go to your Heroku dashboard and click purple <b>New</b> button on the right:
3. As you can see from the above screenshot, you have two options. You can deploy the app directly from your local machine using Heroku CLI or directly from GitHub.
305
+
In this tutorial, we will deploy a `builderbook/builderbook/book/8-end` app from our public [builderbook/builderbook](https://github.com/builderbook/builderbook) repo hosted on GitHub. Deploying from a private repo will be a similar process.
306
+
307
+
Deploying from GitHub has a few advantages. Heroku uses git to track changes in a codebase. It's possible to deploy app from the local machine using Heroku CLI, however you have to create a [Git repo](https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository) for `builderbook/builderbook/book/8-end` with `package.json` file at the root level. A first advantage is that we can deploy from a non-root folder using GitHub instead of Heroku CLI.
308
+
309
+
A second advantage is automation, later on you can create a branch that automatically deploy every new commit to Heroku. For example, we have a [deploy branch](https://github.com/async-labs/saas/tree/deploy) for our demo for [SaaS boilerplate](https://github.com/async-labs/saas/). When we commit to `master` branch - there is no new deployment, when we commit to `deploy` branch - new change is automatically deployed to Heroku app.
310
+
311
+
Let's set up deploying from GitHub. On `Deploy` tab of your Heroku app at Heroku dashboard, click <b>Connect to GitHub</b>, then search for your repo, then click <b>Connect</b> next to the name of the proper repo:
If successful, you will see green text `Connected` and be offered to select a branch and deploy app automatically or manually. Automatic deployment will deploy every new commit, manual deployment requires you to manually click on <b>Deploy Branch</b> button. For simplicity, we will deploy manually from `master` branch of our `builderbook/builderbook` repo.
315
+
316
+
Before we perform a manual deployment via GitHub, we need Heroku to run some additional code while app is being deploying. Firstly, we need to tell Heroku that `8-end` app in the `builderbook/builderbook` repo is not at the root level, it's actually nested at `/book/8-end`. Secondly, Heroku needs to know that our app is Node.js app so Heroku finds `package.json` file, properly installs dependencies and runs proper scripts (such as `build` and `start` scripts from `package.json`). To achieve this, we need to add so called `buildpacks` to our Heroku app. Click `Settings` tab, scroll to `Buildpacks` section and click purple <b>Add buildpack</b> button:
Next, scroll up while on `Settings` tab and click purple <b>Reveal Config Vars</b> button, create a new environmental variable `PROJECT_PATH` with value `book/8-end`:
The above variable will be used by the first buildpack `subdir-heroku-buildpack` to deploy app from repo's subdirectory.
326
+
327
+
4. If we deploy app at this point, our app will deploy with errors since we did not add environmental variables. Similar to how you added `PROJECT_PATH` variable, add all environmental variables from `book/8-end/.env` file to your Heroku app. Remember to add:
328
+
-`MONGO_URL`,
329
+
-`Google_clientID`,
330
+
-`Google_clientSecret`,
331
+
-`EMAIL_SUPPORT_FROM_ADDRESS`,
332
+
-`Github_Test_ClientID`,
333
+
-`Github_Test_SecretKey`,
334
+
-`Github_Live_ClientID`,
335
+
-`Github_Live_SecretKey`,
336
+
-`Stripe_Test_SecretKey`,
337
+
-`Stripe_Live_SecretKey`,
338
+
-`MAILCHIMP_API_KEY`,
339
+
-`MAILCHIMP_PURCHASED_LIST_ID`,
340
+
-`SESSION_SECRET`.
341
+
342
+
343
+
5. While on `Settings` tab, scroll to `Domains and certificates` section and note your app's URL. My app's URL is: https://builderbook-8-end.herokuapp.com
344
+
Let's deploy, go to `Deploy` tab, scroll to `Manual deploy` section and click <b>Deploy branch</b> button.
345
+
After deployment process is complete , navigate to your app's URL:
In your terminal, you will see your most recent logs and be able to see a real-time logs.
363
+
364
+
You can output certain number of lines (N) for retrieved logs by adding `--num N` to the `heroku logs` command.
365
+
You can print only app's logs by adding `--source app` or system's logs by adding `--source heroku`.
366
+
367
+
7. Time to add a custom domain. The Heroku app that we created is deployed on `free dyno`. Free dyno plan does not let you to add a custom domain to your app. To add custom domain, go to `Resources` tab and click purple <b>Change Dyno Type</b> button:
Type your custom domain name, I added `heroku.builderbook.org` as a custom domain, click <b>Save changes</b> button.
376
+
377
+
Heroku will displa you a value for CNAME record that you have to create for your custom domain. For me, custom domain is `heroku.builderbook.org and I manage DNS records at Now by Zeit.
378
+
379
+
After you create a CNAME, ACM status on Heroku's dashboard will change to `Ok`:
It's important that you remember to manually add your custom domain to the settings of your Google OAuth app (Chapter 3) and GitHub OAuth app (Chapter 6). If you forget to do it, you will see errors when you try to log in to your app or when you try to connect GitHub to your app.
383
+
266
384
## Scaling
267
385
268
386
You may want to consider splitting single Next/Express server into two servers:
0 commit comments