Skip to content

pyarbz/RailsTemplates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Rails -> Travis -> Heroku までの環境構築

##必要なもの

##RoRのひな形の作成

Overwrite Gemfile?と聞かれたらnと入力

$ git clone git@github.com:pyarbz/RailsTemplates.git アプリ名
$ cd アプリ名
$ rm -rf README.md
$ rm -rf .git
$ bundle install --path .bundle --without production --binstubs .bin
$ bundle exec rails new . --skip-bundle --skip-test-unit
$ bundle exec rails generate rspec:install
$ bundle exec spring binstub --all
$ bundle exec guard init rspec livereload

app/assets/javascripts/application.js

//= require turbolinksを消す。

//= require jquery
//= require jquery_ujs
//= require_tree .

app/views/layouts/application.html.erb

  • 変更前
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  • 変更後
  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag 'application' %>

Railsの起動

$ bundle exec spring rails s

##Githubの設定

リポジトリの作成をする

gitの設定をする

$ git init
$ git add .
$ git commit -m "first commit"
$ git remote add origin git@github.com:ユーザー名/リポジトリ名.git
$ git push origin master

##Herokuの設定

アプリケーションの作成をする

view画面を作成しないとエラーが出るので、以下のコマンドでview画面を作成する。

$ bundle exec rails g controller Top index

ルーティング先をTopControllerにする

config/routes.rb

Rails.application.routes.draw do
  # get 'top/index'
  root 'top#index'
end

###環境変数を読み込むよう変更する

/config/database.yml

production:
  adapter: postgresql
  encoding: utf8
  port: 5432
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_DATABASE'] %>
  pool: 5
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  timeout: 5000

/config/secrets.yml

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
$ git remote add heroku git@heroku.com:アプリ名.git
$ heroku login
$ heroku config

以下のように出てくる。5432はport番号。

postgres://ユーザー名:パスワード@ホスト名:5432/データベース名

以下のコマンドを入力して環境変数を設定

$ heroku config:set DB_HOST=ホスト名
$ heroku config:set DB_DATABASE=データベース名
$ heroku config:set DB_PASSWORD=パスワード
$ heroku config:set DB_USERNAME=ユーザー名
$ heroku config:set SECRET_KEY_BASE=secrets.ymlに書いてあった文字列
$ heroku config:set RACK_ENV=production

##Travisとの連携

githubから取得できるようにしておく。

$ heroku auth:token

Travis用のTokenを生成したので、travis.ymlに以下のコマンドで書き込む

$ bundle exec travis setup heroku

必要であれば、Travisに環境変数を設定する

$ bundle exec travis login --org
$ bundle exec travis env set DB_HOST ホスト名
$ bundle exec travis env set DB_DATABASE データベース名
$ bundle exec travis env set DB_PASSWORD パスワード
$ bundle exec travis env set DB_USERNAME ユーザー名
$ bundle exec travis env set DB_HOST ホスト名
$ bundle exec travis env list

/config/database.yml

test:
  adapter: postgresql
  encoding: utf8
  port: 5432
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_DATABASE'] %>
  pool: 5
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  timeout: 5000

##NewRelicでHerokuアプリをスリープさせなくする

ライセンスキーを取得しておく

$ heroku addons:create newrelic --app アプリ名
$ heroku config:set NEW_RELIC_APP_NAME=heroku上でのアプリ名
$ heroku config:set NEW_RELIC_LICENSE_KEY=NewRelicのライセンスキー

URLにアクセスして、Pingを飛ばすようにする

  1. newRelicのURL/アプリ名/ping_targetsにアクセス
  2. URL to monitorにherokuの起動URLを入力

##Rubyのバージョンの指定

2.1.2の場合

Gemfile

ruby '2.1.2'

.travis.yml

rvm:
- 2.1.2

.ruby-version

$ rbenv local 2.1.2

##その他参考URL ###Herokuのテンプレート https://github.com/heroku/ruby-getting-started

###Heroku Tutorial https://devcenter.heroku.com/articles/getting-started-with-ruby#introduction

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages