デプロイまでの流れ
現役エンジニアの方のアドバイスで「未完成でもデプロイした方がいい」と言われたので 先にデプロイしました。
以下、自分がどのようにデプロイしたのかを書かせていただきます。
デプロイとは(自分の認識)
ローカルで作成したものを皆さんにお見せできるように 独自のURLを設定すること。 (少し意味が違うかもしれない😅)
何でデプロイしたか
herokuでやりました。
ちなみにSQLはPostgreSQLを使用しました。 (自分で設定する項目が少ないので、初心者にはいいらしい。)
どのタイミングでやるか
rails newの段階でやることを強くお勧めします‼️ 理由は、以下の通りです。
・途中でやるとエラーが多発して なかなかデプロイできないので まっ白な状態でやると成功する確率が高いから ・初期にすれば、不確実性も下がりしインフラ設定が がこけたと認識できる。 ・初期にすると、早いうちにみんなに自分のアプリをさわってもらえる。
ちなみに、自分はGithubでリポジトリを作成してgit cloneしたのちに rails newを行いました。 (Githubと連携できるようにしたかった為)
どのようにしたのか
基本的に以下のqiitaの記事を参考に作りました。
まずは上記の記事を参考に作られたらいいのでないかと思います。
補足
ここからは上記の記事にプラスして、自分が苦戦したところを 追加で書きたいと思います。
git config --global user.email 〇〇 # 〇〇の部分にギットハブに登録したemailを記載する。 git config --global user.name 〇〇 # 〇〇の部分にギットハブに登録したユーザーネームを記載する。
最初、heroku run rails db:migrateというコマンドを打った時 以下のようになった。
Usage: rails new APP_PATH [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated engines) [--skip-collision-check], [--no-skip-collision-check] # Skip collision check -r, [--ruby=PATH] # Path to the Ruby binary of your choice # Default: /Users/iwakitoshiyuki/.rbenv/versions/2.6.6/bin/ruby -m, [--template=TEMPLATE] # Path to some application template (can be a filesystem path or URL) -d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/postgresql/sqlite3/oracle/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc) # Default: sqlite3 [--skip-gemfile], [--no-skip-gemfile] # Don't create a Gemfile -G, [--skip-git], [--no-skip-git] # Skip .gitignore file [--skip-keeps], [--no-skip-keeps] # Skip source control .keep files -M, [--skip-action-mailer], [--no-skip-action-mailer] # Skip Action Mailer files [--skip-action-mailbox], [--no-skip-action-mailbox] # Skip Action Mailbox gem [--skip-action-text], [--no-skip-action-text] # Skip Action Text gem -O, [--skip-active-record], [--no-skip-active-record] # Skip Active Record files [--skip-active-job], [--no-skip-active-job] # Skip Active Job [--skip-active-storage], [--no-skip-active-storage] # Skip Active Storage files -P, [--skip-puma], [--no-skip-puma] # Skip Puma related files -C, [--skip-action-cable], [--no-skip-action-cable] # Skip Action Cable files -S, [--skip-sprockets], [--no-skip-sprockets] # Skip Sprockets files [--skip-spring], [--no-skip-spring] # Don't install Spring application preloader [--skip-listen], [--no-skip-listen] # Don't generate configuration that depends on the listen gem -J, [--skip-javascript], [--no-skip-javascript] # Skip JavaScript files [--skip-turbolinks], [--no-skip-turbolinks] # Skip turbolinks gem [--skip-jbuilder], [--no-skip-jbuilder] # Skip jbuilder gem -T, [--skip-test], [--no-skip-test] # Skip test files [--skip-system-test], [--no-skip-system-test] # Skip system test files [--skip-bootsnap], [--no-skip-bootsnap] # Skip bootsnap gem [--dev], [--no-dev] # Set up the application with Gemfile pointing to your Rails checkout [--edge], [--no-edge] # Set up the application with Gemfile pointing to Rails repository [--master], [--no-master] # Set up the application with Gemfile pointing to Rails repository main branch [--rc=RC] # Path to file containing extra configuration options for rails command [--no-rc], [--no-no-rc] # Skip loading of extra configuration options from .railsrc file [--api], [--no-api] # Preconfigure smaller stack for API only apps [--minimal], [--no-minimal] # Preconfigure a minimal rails app -B, [--skip-bundle], [--no-skip-bundle] # Don't run bundle install --webpacker, [--webpack=WEBPACK] # Preconfigure Webpack with a particular framework (options: react, vue, angular, elm, stimulus) [--skip-webpack-install], [--no-skip-webpack-install] # Don't run Webpack install Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist Rails options: -h, [--help], [--no-help] # Show this help message and quit -v, [--version], [--no-version] # Show Rails version number and quit Description: The 'rails new' command creates a new Rails application with a default directory structure and configuration at the path you specify. You can specify extra command-line arguments to be used every time 'rails new' runs in the .railsrc configuration file in your home directory, or in $XDG_CONFIG_HOME/rails/railsrc if XDG_CONFIG_HOME is set. Note that the arguments specified in the .railsrc file don't affect the defaults values shown above in this help message. Example: rails new ~/Code/Ruby/weblog This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
なぜこのような記載が出たかというと 以下のコマンドを打ち忘れていたからです。
$git add -A #保存するファイルの選択。-Aは全部 $git commit -m "first commit" #addしたファイルの保存の確定
デプロイした後でも変更があれば 以下の流れですればいいと思います。
(herokuの場合)
1 git checkout -b 〇〇 (新しいブランチを作る。) ↓ 2 git add 〇〇 (自分は〇〇の所を-Aにしないと色々とエラーが出た。) ↓ 3 git commit -m 〇〇 ↓ 4 git push ↓ 5 git checkout master(or main) ↓ 6 git pull ↓ 7 git push heroku master(or main) ↓ 8 heroku run rails db:migrate や heroku run rails db:seed などをする。 #heroku runをつけるとheroku上でrailsコマンドを打つことが可能になる。
以上です。
参照
https://qiita.com/kazukimatsumoto/items/a0daa7281a3948701c39
Heroku:Buildpackエラーでハマった - Qiita
Herokuへのデプロイができないエラー:Failed to install gems via Bundler. の改善。 - Qiita
heroku pushしたらFailed to install gems via Bundler. | OFF.TOKYO
HerokuにデプロイでRubyバージョンサポート外エラー - Qiita
git pullでerror: The following untracked …やYour local changes…が出たときの原因と対処 - はんなりと、ゆるやかに
追記 (10/25) デプロイの更新に苦戦したので、追記します。 まず以下のコマンドを忘れずやる。
git checkout main #mainブランチに移動 git merge ブランチ名 #mainにブランチの情報が入る。 git pull origin main #リモートからマージしたファイルを取得する
上記をやらなかったから、git push heroku mainをやっても [Everything up-to-date]というメッセージが出た。
その後、git push heroku mainをしたら 以下のエラーが出た。
There was an error parsing your Gemfile, we cannot continue remote: ! The Ruby version from /tmp/build_96a58b51/Gemfile.lock could not be parsed. Try remote: ! running bundle update --ruby to resolve this.
以下のコマンドで解決させた。
bundle update --ruby git add Gemfile.lock git commit -m "update ruby version"
Gemfile を変更した後に Gemfile.lock が更新されなかったかもしれない。 少なくとも、あれから別のgemを入れたのは間違いない。
参考2
gitを使用したブランチ作成からpushまでの簡単な流れ - Qiita
Ruby バージョンの指定 | Heroku Dev Center
【heroku】「Everything up-to-date」が出て、デプロイできないときの解決方法 - 気ままに気ままのエンジニアブログ
追記3 (2022/04/23)
Username for 'https://git.heroku.com': Password for 'https://〇〇@git.heroku.com':
上記の画面が出た。
この時はheroku loginとターミナルに打ち込む。 そしたら、herokuにpushします。
参考