0
0

More than 1 year has passed since last update.

【Github Actions】本番環境だけコンフリクトが起こる

Posted at

はじめに

某プログラミングスクールで作成した、ポートフォリオを改善しようとした際に躓いたので備忘録。
デプロイのおさらいも書いています。
スクール卒業後で、メンターに質問できずに困っている初学者に参考になれば幸いです。

また間違っているところがあればご教授お願いします。

環境

Ruby 2.6.3
Rails 5.2.6
本番環境 Nginx/Puma/Linux2/AWS EC2/MySQL

エラーの状況

railsアプリをGithub ActionsでCI/CD設定し、デプロイ済み。
修正したい箇所があったので、
topicブランチで修正→developブランチにmerge&push→mainブランチにmerge&pushは問題なく成功している。

が、Github Actionsの画面で確認すると以下のエラーが生じている。

Run echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
Warning: Permanently added '***' (ECDSA) to the list of known hosts.
error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
Error: Process completed with exit code 128.

解決策

ec2上のアプリディレクトリを削除してデプロイし直す。

(cloud9上ではコンフリクトが起きていないので、ec2上のものとコンフリクトが起きていると推測しました)

【手順】

  1. ec2にログイン
  2. アプリディレクトリを削除
  3. cloneしてアプリを再度ec2へ配置
  4. サーバー起動の準備
  5. サーバー起動

【手順1】ec2にログイン

sshでログインします。

cloud9
$ ssh -i ~/.ssh/キーペア名.pem ec2-user@パブリック IPv4 アドレス

【手順2】アプリディレクトリを削除

ec2
$ rm -rvf ディレクトリ名

オプション解説

  • -r, -R
    ディレクトリも削除の対象とする
  • -v
    削除の詳細表示
  • -f, --force
    エラーメッセージを表示しない

【手順3】cloneしてアプリを再度ec2へ配置

ホームディレクトリにいるか確認してから配置しましょう

ec2
[ec2-user@ip-xx-xx-xx-xx ~]$ cd
[ec2-user@ip-xx-xx-xx-xx ~]$ git clone GitHubのリポジトリのURL

【手順4】サーバー起動の準備

cloud9から"config/master.key"と".envファイル"をEC2君に送ってあげましょう。

cloud9
username:~/environment/アプリケーション名 $ scp -i ~/.ssh/practice-aws.pem config/master.key ec2-user@IPアドレス:GitHubのリポジトリ名/config
username:~/environment/アプリケーション名 $ scp -i ~/.ssh/practice-aws.pem .env ec2-user@IPアドレス:GitHubのリポジトリ名/

次はec2でbundleやmigrationを設定してあげましょう。

ec2
[ec2-user@ip-xx-xx-xx-xx ~]$ cd アプリケーション名
[ec2-user@ip-xx-xx-xx-xx アプリケーション名]$ bundle install --path vendor/bundle --without test development
[ec2-user@ip-xx-xx-xx-xx アプリケーション名]$ bundle exec rails assets:precompile RAILS_ENV=production
[ec2-user@ip-xx-xx-xx-xx アプリケーション名]$ bundle exec rails db:migrate RAILS_ENV=production

seedにデータを書いている人はこれも↓

[ec2-user@ip-xx-xx-xx-xx アプリケーション名]$ bundle exec rails db:seed RAILS_ENV=production

【手順5】サーバー起動

念の為
nginxをrestartしてあげて、起動してやります。

ec2
[ec2-user@ip-xx-xx-xx-xx アプリケーション名]$ sudo systemctl restart nginx
[ec2-user@ip-xx-xx-xx-xx アプリケーション名]$ rails s -e production

これで再デプロイが完了できました!
以降はCI/CDで設定しているトリガー(一般的にmainにpushする)で運用できます!!!

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