LoginSignup
3
2

More than 1 year has passed since last update.

Fly.ioでデプロイする手順(Window)

Last updated at Posted at 2022-10-15

参考サイト:https://qiita.com/00000000/items/237ee0fba66ca5a3f12e

STEP1:Gitを利用できるようにしよう

Gitを利用するためにユーザー名とemailを登録する必要があります。
以下のコマンドの"名前"と"メールアドレス"の部分を変更して、実行してください!
名前は自分の氏名のローマ字で大丈夫です。

ターミナル
 git config --global user.name “名前”
ターミナル
 git config --global user.email “メールアドレス”

STEP2:リリースするための準備を仕込もう

ターミナル
 cd desktop
 cd (自分のアプリのフォルダ名)

Gemfileの編集

まず、「gem 'sqlite3', '~> 1.4'」と書いてある行(9行目付近にあります)を
コメントアウトしましょう。
(これらは、自分のパソコンの中のrails app内で用いるデータベースのgemです。) 

#変更前
8-2-1.png

#変更後
8-2-2.png

次に、Gemfileの31行目付近にあるgroup :development, :test do ~ endの間に
gem 'sqlite3'を追加してください。

#変更前

Gemfile
 group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

#変更後

Gemfile
group :development, :test do
  gem 'sqlite3' # 開発環境ではsqlite3を利用
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

最後に以下3行をそのままGemfileの最後の行に追加してください。

Gemfile
group :production do
  gem 'pg'
end

Gemfileを書きかえて保存したら、自分のアプリの階層で、下記のコマンドによりGemfileの変更を反映させましょう!!

ターミナル
 bundle install --without production

database.ymlの編集

以下のように本番環境を表している「production」の項目を変更しましょう。
ymlファイルのインデント(字下げ)は必ず半角スペース2つで行い、全角の空白は使わないように注意してください。

#変更前

config/database.yml
 default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/test.sqlite3

#変更前

config/database.yml
default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  pool: 5

#  インデントは必ず半角スペース2つ

トップページ(ルーティング)の設定

config/routes.rb
root 'tweets#index' # 追記(indexページをトップページにする場合)

config/environments/production.rbの設定

これが最後の編集です。

動的な画像を表示させるために、以下の記述をfalseからtrueに変えてください。
31行目あたりにあると思います。

config/environments/production.rb
config.assets.compile = true

STEP3:いざリリースへ!!

参考:リリースマニュアル
Getting Started
https://fly.io

Fly.ioのCLIのインストール
Fly.ioのコマンドを使うためにFly.ioのCLI(コマンド使えるようにするアプリ的な物)であるflyctlが必要です。

インストール手順のマニュアルも貼っておきます。
https://fly.io

コマンドプロンプトでは一部Fly.ioのコマンドが使えないためPowerShellを利用します。
開き方はキーボードで「Windowsキー」+「R」と打つと検索バーが出てくるので
「PowerShell」と打ち込んでEnterです。

PowerShellにて以下のコマンドを打ってください(階層はどこでも👌)

PowerShell
 iwr https://fly.io/install.ps1 -useb | iex

Fly.ioのアカウント作成

初めてFly.ioに登録する人はこちら

PowerShell
 flyctl auth signup

Fly.ioに登録したことがある人はこちら

PowerShell
 flyctl auth login

リリース

※自分のアプリの階層であることを確認!!!

PowerShell
 gem install fly.io-rails
PowerShell
 flyctl launch

アプリ名を設定するが出てきます。

PowerShell
Creating app in /Users/ユーザ名/Desktop/アプリ名
Scanning source code
Detected a Rails app
? App Name (leave blank to use an auto-generated name): 

アプリ名を入れてEnterを押してください
英語の小文字 (記号は×、ハイフンは大丈夫)

PowerShell
Automatically selected personal organization: xxxxx@gmail.com
? Select region:  [Use arrows to move, type to filter]
  cdg (Paris, France)
  dfw (Dallas, Texas (US))
  ewr (Secaucus, NJ (US))
  fra (Frankfurt, Germany)
  gru (São Paulo)
  hkg (Hong Kong, Hong Kong)
  iad (Ashburn, Virginia (US))
  lax (Los Angeles, California (US))
  lhr (London, United Kingdom)
  maa (Chennai (Madras), India)
  mad (Madrid, Spain)
  mia (Miami, Florida (US))
> nrt (Tokyo, Japan)
  ord (Chicago, Illinois (US))
  phx (Phoenix, Arizona (US))

居住地?を聞いてくる画面が出てきます。
大人しくnrt (Tokyo, Japan)を選びます。

Postgresqlを使うか聞かれるので、y を入力します。

PowerShell
? Would you like to set up a Postgresql database now? [yN]

無料アカウントなのでSingle node, 1x shared CPU, 256MB RAM, 1GB diskを選択

PowerShell
? Select configuration:  [Use arrows to move, type to filter]
> Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
  Production - Highly available, 1x shared CPU, 256MB RAM, 10GB disk
  Production - Highly available, 1x Dedicated CPU, 2GB RAM, 50GB disk
  Production - Highly available, 2x Dedicated CPU's, 4GB RAM, 100GB disk
  Specify custom configuration

デプロイ

ターミナル
flyctl deploy

無事リリースできたアプリを見てみましょう!

flyctl open

以上になります!!

補足

もしデプロイするとき「ruby」「sqlite3」関係のエラーが発生したら
以下のようにGemfileを編集してください!
4行目から9行目の範囲です!!

Gemfime
#ruby '3.0.3' #←編集する箇所

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.7'
# Use sqlite3 as the database for Active Record
gem 'sqlite3' #←編集する箇所

bundle installを忘れないで!!

ターミナル
bundle install

再デプロイしてみましょう

Flyエラー解決の記事

3
2
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
3
2