Deployment Guide
Deploying to the Internet
When should I deploy?
- When you start an application, it’s a good idea to set up your deployment right away, and then intermittently deploy changes through your development process
- Deploy recent changes to the production environment when your application has reached a new apex in feature availability - in other words, when your
main
branch has been updated to where you are confident that you want it to be available on the internet. - When deploying, these platforms will take your remote copy, you’ll be able to select which branch (we recommend
main
), and deploy it to your server. So, follow good git practice and only merge tomain
once your team has decided the code quality is good enough to deploy a new version of the app.
Deplying an API written in Rails
Guide to Render
These instructions were compiled from Render.com’s existing documentation and resources. You are free to ignore this page and follow directions on Render’s website to deploy your application, if you wish. The instructions and links below have been tested as of March 2023.
Turing is not associated or affiliated with Render.com in any way; this page was created as a reference for students to deploy their applications for the first time.
Getting Started
-
Create an account on Render, if you haven’t already. (You can use GitHub to create an account to make your sign-in even easier.)
-
Locally in the repository you’ll be deploying, update the secret key before deploying:
- Delete the existing
config/credentials.yml.enc
file. - In Terminal in this repository, run
EDITOR="code" bin/rails credentials:edit
.- You do need to specify
EDITOR="code"
for it to work unless it already appears in your Terminal’s config file, otherwise it may give you errors. - When successful, it will generate 2 files called
credentials.yml.enc
andmaster.key
. Themaster.key
file will be ignored by git - this is normal, you do not want to share this key with other teammates.
- You do need to specify
- Commit and push the change that added the new
credentials.yml.enc
file to your remote.
- Delete the existing
Deploying an Existing Rails App
Create DB Service
- After you sign in, it will take you to your Dashboard.
- Click the button to Create a New PostgreSQL.
- Give the PostgreSQL service a name (recommend to use the same name as your repo for sanity)
- You can keep the Database and User fields blank, they will be randomly generated.
- Change the PostgreSQL Version to 14.
- Keep the Instance Type set to Free, and then click
Create Database
.
- Once the database is created, click the
Connect
dropdown on the top-right, and copy the Internal Database URL. You’ll use this link to connect to the production DB after you create the web service.
Create Web Service
- Click the button to Create a New Web Service. If you’ve connected your GitHub account, it will display some of your repositories that you can connect to for this web service. Click
Connect
next to the repo you’d like to deploy. - Give the service a name (anything, you can use the same name as the repo)
- In the Build Command step, add
rails db:{migrate,seed}
to the end of the string.- Note that this command will be run each time your application is deployed. As such, your
seeds.rb
file should contain{Model}.destroy_all
lines at the top so that running theseed
command in production does not cause duplicate data.
- Note that this command will be run each time your application is deployed. As such, your
- Make sure your Instance Type is Free, then click the
Create Web Service
button. - On the next screen, you’ll need to add 2 Environment Variables, so click Environment.
- Key =
DATABASE_URL
, Value = your PostgreSQL service’s Internal Database URL, from the previous step. - Key =
RAILS_MASTER_KEY
, Value = your repo’s localmaster.key
file.
- Key =
If the steps are all working, it will then attempt to build your application and copy it to its container, this will take several minutes. You can watch the service’s terminal for updates. If the build is successful, it will display ==> Build successful 🎉
then ==> Deploying...
. When this step finishes, you can click the URL of the application displayed at the top of the page to visit your site.
At any time, you can see your applications on Render from a high-level by going to the Render Dashboard.
Troubleshooting
- If the build fails, you will see a message like
==> Build failed 😞
in the dashboard terminal, and it will send you an e-mail notification. Any build errors or deployment issues will be listed in the terminal above this message. - If during your deploy you see a message in Render’s console that says
rails: command not found
, run the following command in your local terminal:bundle lock --add-platform x86_64-linux
. This will allow Render’s server architecture to run your application. - Note that the Free tier of the Web Service does not allow for SSH console, so you can’t use
rails c
on the dashboard unfortunately. - You can read more about the Free tier limitations if you encounter other issues.
- Further troubleshooting documentation can be found on Render’s website.
Guide to Fly.io
These instructions were compiled from Fly.io’s existing documentation and resources. You are free to ignore this page and follow directions on Fly.io’s website to deploy your application, if you wish. The instructions and links below have been tested as of December 2022.
Turing is not associated or affiliated with Fly.io in any way; this page was created as a reference for students to deploy their applications for the first time.
Getting Started
- Install Fly’s command line tools
brew install flyctl
- This may take a few minutes, please be patient.
- Log into Fly from your Terminal (Documentation link)
flyctl auth login
- This will open your browser and prompt you to log in.
- If you haven’t made an account yet, click the link to make one. (You can also use GitHub to create an account, in order to sign in even easier.)
- NOTE: It will look like you have to enter a credit card number to complete registration. There is a small link below the form that says “Try Fly.io for Free –>” Click that link to proceed with the free plan.
- If for some reason you must enter a credit card to complete registration, please reach out to your instructors for assistance.
Deploying an Existing Rails App
-
In Terminal, navigate to the app that you want to launch, in its root folder (e.g.
/turing/2module/projects/relational_rails
). - Follow Fly’s instructions to launch an existing Rails app.
fly launch
- It will “scan” your code, detect a Rails app, and ask you some questions.
- Leave “App Name” blank, or type one in. The name should be unique to your account.
- Important: When asked, choose the default region by pressing
Enter
.- Note: Some users have had weird errors and issues when they choose a non-default region. In this scenario, it may be easier to delete the deployment on your Fly.io Dashboard than to fiddle around with it for too long.
- When asked to select a configuration, select
Development
(this is the first option). - When asked, “
Would you like to set up a Postgresql database now? y/n"
enterY
for yes. - Copy to a file or screenshot the given postgresql Username, Password, Hostname, and Ports data, as you may need this later. Save this to your desktop for safe keeping. You should not commit this to your repository.
- When asked, “
Would you like to set up an Upstash Redis database now? y/n"
entern
for no.
-
When the above steps are completed, double-check that you are on a local branch that you want to deploy (usually this is the
main
branch). Then, runfly deploy
to deploy a copy of the files/folders in this branch to the newly-created server. At the end of this process, Fly will automatically run yourdb
setup tasks like create, migrate, and seed. - If your deployment is successful, it will display
"Monitoring deployment..."
and---> v0 deployed successfully.
. Thisv0
number will increment with each subsquent deployment. Runfly open
to open your deployed application in your browser.
At any time, you can see your applications on Fly from a high-level by going to the Fly Dashboard.
Troubleshooting
- If you visit your app in the browser and see some generic errors, those messages will probably be vague and unhelpful. This is because you’re looking at the production environment, which is meant to be seen by users and not developers. To see helpful debugging information, run
fly logs
from your local Terminal in order to see logs of your deployments. - To open a rails console on your server, run
fly ssh console -C "/app/bin/rails console"
.- Be careful of using this, as you are accessing the Rails console directly in production! Also, this may incur more usage of the application which can run up your usage on Fly’s service. If you’ve provided a credit card number, it may end up charging you.
- Problem: an error message in deployment that ends with:
ERROR: [stage-3 7/7] RUN bin/rails fly:build … rails aborted! ExecJS: RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
- Solution: Open the
Dockerfile
in the root of the project. Around line 76, findARG DEPLOY_PACKAGES
. At the end of this string value, addnodejs
. It should now look like this:ARG DEPLOY_PACKAGES="postgresql-client file vim curl gzip libsqlite3-0 nodejs"
Finally, run
fly deploy
again.
- Solution: Open the
- Further troubleshooting documentation can be found on Fly’s website.