0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[学習記録]Railsチュートリアル[1.4.x~1.5.x][2019/12/18]

Posted at

TL;DR

  • n(n∈N≧2)番煎じの「Railsチュートリアルの備忘録的Something」

  • Railsチュートリアル1章の1.4「Gitによるバージョン管理」から1.5「デプロイする」を参考に、Git及びBitbucketのセットアップを行い、Herokuにデプロイするまでの手順をまとめた。

  • コマンドの詳細な説明は省き、このコマンドは何をするためのコマンドであるかについてだけ書いておく。


1.4 Gitによるバージョン管理

1.4.1 インストールとセットアップ

//systemセットアップ
git config --global user.name "Your Name"
git config --global user.email your.email@example.com

//git initでセットアップ
cd Railsアプリケーションのルートディレクトリ
git init

//プロジェクトのファイルをリポリトジに追加
git add -A

//待機中のプロジェクトファイルの状態を確認する
git status

//ステージングエリアで控えている変更をリポリトジにコミットする
git commit -m "commit message"

//commit messageの確認
git log

1.4.2 Gitのメリット

重要なディレクトリやファイルを削除してしまっても復旧が容易にできる

//ある重要なディレクトリ(app/controllers/)を削除してしまった
rm -rf app/controllers/

//現在の状態を確認
git status
 On branch master
 Changed but not updated:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

      deleted:    app/controllers/application_controller.rb

 no changes added to commit (use "git add" and/or "git commit -a")

//変更が行われたのは現在の作業ツリー内のみなので、以前のコミットをチェックアウトする
git checkout -f

//app/controllers/が存在するか確認する
ls app/controllers/
 application_controller.rb  concerns/

//app/controllers/が存在していることが分かったので復旧終了

1.4.3 Bitbucketを利用するための準備

//作成した公開鍵を出力する
cat ~/.ssh/id_rsa.pub

//Bitbucketへのリポリトジの追加
git remote add origin git@bitbucket.org:ユーザー名/Railsアプリケーションの名前.git

//リポリトジへのプッシュ
git push -u origin --all

1.4.4 gitの操作の練習

例.README.mdファイルについて操作する

Branch

//トピックブランチ(短期間だけ使う一時的なブランチ)を作成し、それに切り替える
git checkout -b modify-README

//すべてのローカルブランチを表示
git branch
//現在使用中のブランチには*がつく
  master
* modify-README

Edit

//Railsアプリケーションのルートディレクトリに移動
cd Railsアプリケーションのルートディレクトリ

//READMEを編集(変更内容はなんでもいい)
vi README.md
[README.md]
# Ruby on Rails Tutorial
## "hello, world!"
This is the first application for the
[*Ruby on Rails Tutorial*](https://railstutorial.jp/)
by [Michael Hartl](http://www.michaelhartl.com/). Hello, world!

Commit

//ブランチの状態を確認
git status

//現存するすべてのファイルへの変更を一括でコミットする
//commit messageは現在形かつ命令形で書くようにすること
//例. Improve the README file
git commit -a -m "commit message"

Merge

//masterブランチに移動
git checkout master
//masterブランチにmodify-READMEの変更をマージする
git merge modify-README
//トピックブランチを削除する(任意)
git branch -d modify-README

Push

//Bitbucketに変更を反映させる
git push

1.5 デプロイする

1.5.1 Herokuのセットアップ

//本番環境(production)にpg gemをインストール
vi Gemfile
[Gemfile]
group :development, :test do
  gem 'sqlite3', '1.3.13'
end
group :production do
  gem 'pg', '0.20.0'
end
を追記

//本番用以外のgemをインストール
bundle install --without production

//変更した内容をコミットする
git commit -a -m "Update Gemfile for Heroku"

Herokuのアカウントを作成 -> Heroku公式サイト

//Heroku CLIをインストールする(Cloud9限定)
source <(curl -sL https://cdn.learnenough.com/heroku_install)

//Herokuのバージョンを確認(= Herokuが正常にインストールされたかを確認)
heroku --versionw
  heroku/7.35.0 linux-x64 node-v12.13.0

//Herokuにログインする
heroku login --interactive

//HerokuにSSHキーを追加
heroku keys:add

//Herokuサーバーにサンプルアプリケーションの実行場所を追加
heroku create

1.5.2 Herokuにデプロイする(1)

//Gitを用いてHerokuにリポリトジをプッシュする
git push heroku master

1.5.3 Herokuにデプロイする(2)

heroku createを実行した際に生成されたアドレスをブラウザで表示する。

ブラウザはなんでもいいと思う(要検証)

1.5.4 Herokuコマンド

herokuコマンド一覧

//アプリケーションの名前を変更する
//ただし、Application-nameはユニークな(他と被らないような)名前にすること
heroku rename Application-name

//小文字のアルファベットからランダムに7つ選んだ文字列を生成する
('a'..'z').to_a.shuffle[0..7].join

//Herokuアプリのログをリアルタイムで確認する
heroku rails --tail
or
heroku logs -t

参考

vdeep / Herokuでよく使うコマンドまとめ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?