Herokuのアカウント作成
Heroku
GitHubに適当なリポジトリを作成してclone
$ git clone git@github.com:{account}/{repository}.git
railsアプリケーションを作成する
$ cd {repository}
$ rails new .
$ vi app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def hello
render html: 'hello, world'
end
end
$ vi config/routes.rb
Rails.application.routes.draw do
root 'application#hello'
end
Heroku用セットアップ
Gemfileに以下を追記
group :development, :test do
+ gem 'rspec-core'
+ gem 'rspec_junit_formatter'
+ gem 'sqlite3'
gem 'byebug', '9.0.6', platform: :mri
end
+ group :production do
+ gem 'pg'
+ end
localhost:3000 でrailsアプリケーションがちゃんと動いているか確認。
$ bundle install
$ rails s
Heroku CLIのインストール
ここから、それぞれの環境に合わせてインストール
Heroku CLI | Heroku Dev Center
Heroku CLIがインストール済みかどうか確認する。
$ heroku -v
heroku/7.0.22 darwin-x64 node-v10.0.0
Heroku コマンドで、ログインし、createコマンドでアプリケーションを作成する
$ heroku login
$ heroku create {heroku上のapp name(指定なしの場合はランダムな名前になる)}
CircleCIアカウントを作成
ここから
Continuous Integration and Delivery - CircleCI
GitHubと連携し、Add Project から対象のリポジトリを選択する
CircleCI用のconfigファイルを作成する
Create a folder named .circleci and add a fileconfig.yml (so that the filepath be in .circleci/config.yml).
指示どおりに.circleci/config.yml を作成し、
$ cd {repository dir}
$ mkdir .circleci && vi .circleci/config.yml
画面に表示されているサンプルをコピーペーストする
schema.rbがないとCircleCIのビルドにこけるので、db:migrateを実行しておく
$ rails db:migrate
GitHubへここまでの作業をpushする
$ git add -A
$ git commit -m 'init'
$ git push origin master
上記push後にCircleCI上でビルドが走ればOK。
CircleCIからHerokuへの自動デプロイ設定を行う
以下を.circleci/config.ymlの最下部に追加
- deploy:
name: Deploy Master to Heroku
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git master
fi
HEROKU_API_KEY、HEROKU_APP_NAME の環境変数をCircleCIダッシュボードから設定する。
environment variablesを開き、Add variablesを選択し、それぞれ値を設定する。
HEROKU_API_KEYは、Heroku上のaccount settingsから取得できる。(revealを押せば表示される)
ここまでできたら、最後に変更分をpush
$ git add -A
$ git commit -m '適当なコメント'
$ git push origin master
Deploy Master to Heroku タスクが成功していればOK。HerokuのアプリケーションURIにGETリクエストを投げてhello worldが表示されているか確認してみる
$ curl https://tic40-emperor.herokuapp.com/
hello, world