These instructions will help you deploy your Finstagram app to Railway (a web hosting service), so that the entire internet will be able to see and interact with your application! Before we get to deployment though, we need to make sure a couple of our files are setup properly.
If you've already deployed and want to continue making changes, see the Making Changes section near the bottom.
- In the terminal, type
git branch
- Take note of your current branch
- Type these commands:
git add -A
git commit -m "pre-deployment"
git push
git checkout master
git merge NAME_OF_NOTED_BRANCH
- Continue on with Pre-Deployment: Create Seed Data
git add -A
git commit -m "pre-deployment"
git push
- Create a
seeds.rb
file in thedb
folder - Copy the following into that file:
records = [
{
user: { username: "sharky_j", avatar_url: "https://live.staticflickr.com/65535/52358606250_01c667c5da_w.jpg", email: "sharky_j@gmail.com", password: "abcd" },
post: { photo_url: "https://live.staticflickr.com/65535/52358421508_786aa10e2c_c.jpg"}
},
{
user: { username: "kirk_whalum", avatar_url: "https://live.staticflickr.com/65535/52358421348_f34c7996b1.jpg", email: "kirk_w@gmail.com", password: "abcd" },
post: { photo_url: "https://live.staticflickr.com/65535/52357237337_1cc718f6a7_4k.jpg"}
},
{
user: { username: "marlin_peppa", avatar_url: "https://live.staticflickr.com/65535/52358415933_0a0e6bc35f_3k.jpg", email: "marlin_p@gmail.com", password: "abcd" },
post: { photo_url: "https://live.staticflickr.com/65535/52358494794_f88b160d15_4k.jpg"}
}
]
puts "=== Seeding database... ==="
# create Users and FinstagramPosts
records.each do |record|
# Create a User
user = User.create record[:user]
puts "-- Created User: #{user.username}"
# Create a FinstagramPost
finstagram_post = record[:post]
finstagram_post[:user_id] = user.id
FinstagramPost.create finstagram_post
puts "-- Created a FinstagramPost for User: #{user.username}"
end
puts "=== Seeding complete. ==="
Ensure your Gemfile
file contains the following code (should match exactly).
Pay attention to the new groups :development
, :test
and :production
.
source "https://rubygems.org"
gem 'rake'
gem 'activesupport'
gem 'activerecord', '< 6.1.0'
gem 'bcrypt'
gem 'sinatra', '~> 3.0.2'
gem 'sinatra-contrib'
gem 'sinatra-activerecord'
gem 'puma'
gem 'tux'
group :development, :test do
gem 'pry'
gem 'shotgun'
gem 'sqlite3', '~> 1.5.3'
end
group :production do
gem 'pg'
end
After saving the Gemfile
changes,
- Delete your
Gemfile.lock
file - In a terminal type
bundle install
Ensure your config/database.rb
file contains the following code (should match exactly).
Important things that are different: if/else
block for different database connection.
configure do
# Log queries to STDOUT in development
if Sinatra::Application.development?
ActiveRecord::Base.logger = Logger.new(STDOUT)
set :database, {
adapter: "sqlite3",
database: "db/db.sqlite3"
}
else
db = URI.parse(ENV['DATABASE_URL'])
set :database, {
adapter: "postgresql",
host: db.host,
username: db.user,
password: db.password,
database: db.path[1..-1],
encoding: "utf8"
}
end
# Load all models from app/models, using autoload instead of require
# See http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html
Dir[APP_ROOT.join('app', 'models', '*.rb')].each do |model_file|
filename = File.basename(model_file).gsub('.rb', '')
autoload ActiveSupport::Inflector.camelize(filename), model_file
end
end
Ensure your db/migrate/0_create_base_tables.rb
file contains the following code (should match exactly).
The only difference is the first line β specifically, the brackets with a version number.
class CreateBaseTables < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :username
t.string :avatar_url
t.string :email
t.string :password
t.timestamps
end
create_table :finstagram_posts do |t|
t.references :user
t.string :photo_url
t.timestamps
end
create_table :comments do |t|
t.references :user
t.references :finstagram_post
t.text :text
t.timestamps
end
create_table :likes do |t|
t.references :user
t.references :finstagram_post
t.timestamps
end
end
end
If you implemented proper password hashing, you'll have to add versioning [5.0]
to your other migration file as well.
- Create a
railway.toml
in the directory root - Copy the following into that file:
[build]
builder = "nixpacks"
[deploy]
startCommand = "bundle exec puma -p $PORT"
healthcheckPath = "/"
healthcheckTimeout = 100
restartPolicyType = "on_failure"
restartPolicyMaxRetries = 5
In a terminal type the following and press enter:
npm i -g @railway/cli
1. Create an account at Railway.
- You're able to login using your Github credentials. Make sure you're logged into Github first.
- When you log into the dashboard, confirm their terms to gain full access to the free tier.
2. Back in Gitpod in the terminal, log into Railway:
- Run:
railway login
and press any key - Open the URL that it prompts and confirm access
- If it worked, it will say something like
Logged in as you@youremail.com
4. In the terminal, initialize a Railway project:
- Run:
railway init
- Select
Empty Project
- Enter the project name:
Finstagram
5. In the terminal, initialize the DB:
- Run:
railway add
- Select
postgresql
6. Head over to Railway, create a new service:
- Visit: Railway
- Click on the project
Finstagram
- Click on the button
+ New
- Select
Empty Service
7. Back in Gitpod in the terminal, create some environment variables:
- Run:
railway variables set RAILS_ENV=production
railway variables set RACK_ENV=production
railway variables set PORT=3001
8. In the terminal, deploy Finstagram:
- Run:
railway up
9. In the terminal, run DB migration and seed:
- Run:
railway run bundle exec rake db:migrate
- Run:
railway run bundle exec rake db:seed
10. Back in Railway, expose your new Finstagram app:
- Click on the service (with a random name) that's next to PostgreSQL
- Go to Settings
- Enable the app by exposing it
11. Lastly, push your changes to Github:
git add -A
git commit -m "readies for deployment"
git push origin master
Once your app is live, you can still make changes to your code and add new features! You can make your changes in Gitpod as you would normally, then once you're done, make sure all your changes are saved.
If you're starting up your gitpod workspace again, you'll need to do a couple initial steps to connect Railway first:
- In the terminal, type:
npm i -g @railway/cli
- After it's installed, and still in the terminal, type:
railway login
, click on the link and confirm - Go to Railway, click into your Finstagram project and go to the Settings to find the Project ID
- Back in Gitpod, in the terminal, run:
railway link PROJECT_ID
(replace PROJECT_ID with the ID from the previous step)
After you've made some changes and tested them locally to ensure your app is working, follow the next steps to push your changes then deploy to Railway.
1. Open a terminal tab and commit your changes as you normally would: E.g.
git status
git add name_of_file.rb
git commit -m "describes your changes"
git push origin master
2. Still in the terminal tab, deploy your changes
- Run:
railway up
3. Now go to your Railway app, refresh the page, and your changes will be live! π€
When developing your app on Gitpod, you were able to run bundle exec tux
to view and edit records in the database. Once your app is deployed on Railway, using tux normally within Gitpod won't access the Railway database, it'll only make use of the local database.
To connect to the PostgreSQL database of your Railway app, type the following command in the terminal:
railway run bundle exec tux
(and then you can use tux as you normally would)
- Railway Docs: https://docs.railway.app/
- Railway Docs (CLI): https://docs.railway.app/reference/cli-api
- Railway Docs (PostgreSQL): https://docs.railway.app/databases/postgresql
- Railway Builder: https://nixpacks.com/docs/providers/ruby
- Sinatra Docs: http://www.sinatrarb.com/documentation.html