Skip to content

Instantly share code, notes, and snippets.

@EliseFitz15
Last active July 27, 2017 11:54
Show Gist options
  • Save EliseFitz15/8bf647f85c8bfbd779f8 to your computer and use it in GitHub Desktop.
Save EliseFitz15/8bf647f85c8bfbd779f8 to your computer and use it in GitHub Desktop.
sinatra-deployment.md

Deploy a Sinatra App with Heroku

##What does this mean? Your web application - out there on the Internets for all to see! We are leaving your local development environment and heading into staging and production.

We develop our apps in many different "environments" and so far we have discussed and worked with our test environment and development environment.

To follow Agile Development Methodologies we aim to build out some functionality through testing locally, then do a release (i.e. deploy the app) and test it out in production. Once it's up we check to make sure it's running the way we expect - through QA-ing (quality assurance testing). In this stage we may also get feedback from clients or other teams. Next, we make changes and updates to those features, re-releasing the next iteration to test. Then, once those features are finished, we begin with the next set of functionality.

This process is also known as "continuous delivery" or "continuous development" which is a software engineering approach in which teams produce software in short cycles, ensuring that the software can be reliably released at any time. It aims at building, testing, and releasing software faster and more frequently.

##What is Heroku? Heroku is a cloud platform as a service. That means you do not have to worry about infrastructure; you just focus on your application.

It's a cloud-based, scalable server solution that allows you to easily manage the deployment of your Rails (or other) applications. That means you do not have to worry about infrastructure; you just focus on your application. This service uses it's own conventions, for example, using a Postgres database.

##Let's Git Started

  1. Heroku uses git as a way of deploying our apps.
#initialize git repository
~ git init
~ git add .
~ git commit -m "initial commit"

# add to github (this command uses the hub gem)
~ hub create your-app-name

#create a heroku app
heroku create your-app-name
  • Note: Heroku app names are unique so you may get an error that it's taken. If not your url will be your-app-name.heroukapp.com. If you don't mind what your app name is you can just use heroku create in your root directory and get a random one that can be pushed to an owned domain name later on.
#check remotes
~ git remote -v
heroku	https://git.heroku.com/nyer-poetry-test.git (fetch)
heroku	https://git.heroku.com/nyer-poetry-test.git (push)
origin	git@github.com:EliseFitz15/nyer-poetry-test.git (fetch)
origin	git@github.com:EliseFitz15/nyer-poetry-test.git (push)
  • Here we can push this code to GitHub, so that other people can view and collaborate on this project, or we can push the code to Heroku.
  1. Make sure we have the required files
  • Gemfile:(root directory) For deploying in Sinatra we want to include the gem "puma" which is a Ruby web server.
  • config.ru: (root directory) A "rack up" file, to let heroku know we are using a Sinatra app.
    require './server'
    
    # disable buffering for Heroku Logplex
    $stdout.sync = true
    
    run Sinatra::Application  
    
  • Procfile: (root directory) Describes what services we will be running including web servers, worker, or other services.
    web: bundle exec puma -C config/puma.rb
    
  • config/puma.rb:(in the root directory create a config directory) Include the following specifications for web server.
    workers Integer(ENV['WEB_CONCURRENCY'] || 2)
    threads_count = Integer(ENV['MAX_THREADS'] || 5)
    threads threads_count, threads_count
    
    preload_app!
    
    rackup      DefaultRackup
    port        ENV['PORT']     || 3000
    environment ENV['RACK_ENV'] || 'development'
    
  1. Databases and Environments
  • When we ran the heroku create command, a number of actions occur, including the initialization of a Postgres database. The location of this database is stored as a URL in an environment variable.

We need to update our server file to configure our production database with this information.

```
  configure :production do
    uri = URI.parse(ENV["DATABASE_URL"])
    set :db_config, {
      host: uri.host,
      port: uri.port,
      dbname: uri.path.delete('/'),
      user: uri.user,
      password: uri.password
    }
  end
```
  1. When to Seed the database.
  • We typically seed the database if we are saving certain information as objects in our database that users are not generating. Some examples may be in you are displaying FAQs on your site. It all depends on architecture, you could also just use html and hard code the information on to a page.
  • How to: For a Sinatra app you can add data a few different ways.
    • Configure a ruby file to seed objects or to read from a csv file.
    • Run a sql file on your database with your insert statements.
  • Note: In rails there is a seed.rb file where you would describe objects you would want to insert.
  1. Deploy
  • Because Heroku leverages git you can runt he following commands to finish deploying your app.

     # commit our changes
    ~ git add .
    ~ git commit -m "app primed for deployment"
    
    # deploy!
    ~ git push heroku master
    
    # set up the production database schema
    ~ heroku pg:psql DATABASE_URL < schema.sql
    
    # seed the heroku database
    ~ heroku pg:psql DATABASE_URL < data.sql
    
    # open in the browser
    ~ heroku open
    
  1. Rinse & Repeat!
  • Add further features and deploy
  • After finishing a feature it would also include styling

####Notes

  • Heroku Dashboard, config vars
  • Custom domain name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment