LoginSignup
1
1

More than 1 year has passed since last update.

Githubにプッシュ&Herokuにデプロイまで

Last updated at Posted at 2022-01-08

開発環境
mac OS バージョン11.6
エディタ
VScode

% rails new toy_app
% cd toy_app

group :production do
gem 'pg', '1.1.4'
end

% bundle config set --local without 'production'
% bundle install

bundle install(もしくはbundle update)が完了したら、1.2.2でセットアップしたYarnを使ってWebpackerをインストールします。もしWebpackerを既にインストール済みの場合は設定を上書きするかどうかを聞かれるので、そのときは「no」と入力してください。(自分は表示が出ませんでした)

% rails webpacker:install

最後に、GitでこのToyアプリケーションをバージョン管理下に置きます。

% git init
% git add -A
% git commit -m "Initialize repository"

ここまではOK

次に、1.4.3と同じ手順でGitHubで新しいリポジトリを作成します(このときリポジトリを図 2.1のようにprivateにしておくことをお忘れなく)。←やってなかった
続いて、生成したファイルをこの新しいリモートリポジトリにプッシュします。

% git remote add origin https://github.com/<あなたのGitHubアカウント名>/toy_app.git

(実際にやったの→git remote add origin https://github.com/ctmajor07@gmail.com/toy_app.git)
Gihubアカウント名のところを間違えて、メールアドレスで実行してしまう。

% git push -u origin master

fatal: unable to access 'https://github.com/ctmajor07@gmail.com/toy_app.git/': The requested URL returned error: 400

ここでエラーが起きる

ローカルgitリポジトリでリモートのリポジトリURL確認方法
ローカルにクローンしたリポジトリのリモートURLを確認する方法はいくつあります。

% git remote -v
origin  https://github.com/ctmajor07@gmail.com/toy_app.git (fetch)
origin  https://github.com/ctmajor07@gmail.com/toy_app.git (push)

% git remote add origin https://github.com/tomoya007/toy_app.git

% git remote -v
origin  https://github.com/tomoya007/toy_app.git (fetch)
origin  https://github.com/tomoya007/toy_app.git (push)

% git push -u origin master 
remote: Repository not found.
fatal: repository 'https://github.com/tomoya007/toy_app.git/' not found

何が起きているかというと、、、
エラーの原因は→プライベートリポジトリへのアクセス認証に失敗しているためです。gitの接続プロトコルを https にしているのが原因のことが多いです。githubから普通にcloneすると大体 https になっているのですが、仕事とプライベートで複数のgitアカウントを切り替えて使っていると、いつのまにかgithubの認証情報が切り替わって、アクセスできなくなるようです。

remote.origin.url が https になっているかを確認します。

% git config -l | grep remote.origin.url

remote.origin.url=https〜 # httpsから始まるURLがセットされているか
↓実際にやってみたら

% git config -l | grep remote.origin.url
remote.origin.url=https://github.com/tomoya007/toy_app.git

②リモートURLをSSHのパスに書き換え

git remote set-url origin [リポジトリのSSHパス]
git remote set-url origin git@github.com:xxx/abc.git # 例

SSHパスは、以下の画面よりコピーすると簡単です。

% git remote set-url origin git@github.com:tomoya007/toy_app.git

実行後、もう一度 git config -l をして、remote.origin.url が git@ で始まるSSHパスに変わっているかを確認しておきましょう。

% git config -l | grep remote.origin.url
remote.origin.url=git@github.com:tomoya007/toy_app.git

③公開鍵をgithubに登録
自分のローカルマシンの公開鍵をgithubのアカウントに登録します。(既に登録済の場合はスキップして構いません。)
https://github.com/settings/ssh

まず自分の公開鍵をクリップボードにコピーします。Macの場合は pbcopy < ~/.ssh/id_rsa.pub でコピーできます。
↓実際に実行した結果

% pbcopy < ~/.ssh/id_rsa.pub
zsh: no such file or directory: /Users/chiharatomoya/.ssh/id_rsa.pub

試しにペーストしてみても、ちゃんとコピーされてない。。

原因
そもそも公開鍵というものを作成してなかった。なのでまずは公開鍵を作成します。手順は以下の通りです。
(注意:公開鍵の作成方法はOSによって異なります。こちらはMac環境での手順になります。)

  1. ターミナルで以下のコマンドを実行し、SSH鍵の保存先ディレクトリを作成する
% mkdir -m 700 ~/.ssh
  1. 以下のコマンドを入力してEnterキーを押す
% ssh-keygen -t rsa
  1. 保存場所とファイル名を確認し、Enterキーを押す
Enter file in which to save the key (/Users/hogehoge/.ssh/id_rsa):
  1. パスフレーズを入力する
Enter passphrase (empty for no passphrase): (パスフレーズを入力)
Enter same passphrase again: (もう一度入力)
  1. 以下のコマンドで公開鍵の内容を確認する
% pbcopy < ~/.ssh/id_rsa.pub
上記のコマンドでssh-rsa AAAAB3Nza...のような長い文が出てくればOKです。

接続を確かめます

% ssh -T git@github.com

Hi (account名)! You've successfully authenticated, but GitHub does not provide shell access.

上記のように返ってきたら接続完了です。

筆者の場合は、

The authenticity of host 'github.com (13.114.40.48)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 
Host key verification failed.

最後の行に「ホストキーの検証に失敗しました」と出ていますが、その前の行で間違えてEnterを押したのが原因でした。正しくはyesと打ってからEnterです。

yesと打って、パスワードも入れると下記のように出ます。

Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added 'github.com,52.192.72.89' (ECDSA) to the list of known hosts.
Enter passphrase for key '/Users/chiharatomoya/.ssh/id_rsa': 
Hi tomoya007! You've successfully authenticated, but GitHub does not provide shell access.

今度こそgit push -u origin masterを実行して、リモートリポジトリにプッシュします。
更に、デプロイもできるように準備していきます。Hello, world!だけ出るようにコントローラーに書き加えます(ここでは省略。なくてもいいです)

$ git commit -am "Add hello"
$ heroku create
$ git push && git push heroku master

何とか実行できました。
今回はここまで。

参考にした記事URL
https://qiita.com/shizuma/items/2b2f873a0034839e47ce

1
1
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
1
1