The following guide details how I deploy apps with a Rails backend and a React client side.
-
We'll start with Rails, you'll need to have a Heroku account and the Heroku CLI installed
-
Create a new Heroku project, this also adds a
git remote
to push toheroku create
-
Make changes to your Rails code, commit and push to GitHub, ensure all the latest code changes are on your
master
branch, run this command:git push heroku master
-
You'll need to format your production database with
heroku run rails db:migrate
, if you needed to seed your database in production it'sheroku run rails db:seed
-
Add a
RAILS_MASTER_KEY
to your HerokuENV
variables to ensure your production environment can decrypt your credentials file -
Moving back to the React client ensure you have a
.env.production
file in the root of your React project, it should look something like this:REACT_APP_BACKEND_URL=https://aqueous-badlands-44167.herokuapp.com
-
Ensure your fetch requests are using the
REACT_APP_BACKEND_URL
variable correctlyfetch(`${process.env.REACT_APP_BACKEND_URL}/your-endpoint`)
-
Add a
netlify.toml
file to the root of your React project if you're using React Router[[redirects]] from = "/*" to = "/index.html" status = 200
-
Sign up to Netlify
-
Install the Netlify CLI, ensure that you run
netlify login
-
Add this script to your
package.json
"deploy": "yarn build && netlify deploy --prod --dir=build"
-
Make changes to your React code, commit and push to GitHub, ensure all the latest code changes are on your
master
branch, run this command:yarn run deploy
-
You'll get back a Netlify url that will be something like this:
https://brave-curie-671954.netlify.app
-
Add this url to your
cors.rb
file in your Rails projectRails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'https://brave-curie-671954.netlify.app' resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head] end end
-
Commit and push to GitHub, redeploy to Heroku
git push heroku master