Capistranoを使ってデプロイする時に「git ls-remote」でつまづいた

TECH
2016.01.19
Pocket

Capistranoを使ってデプロイする時に「git ls-remote」でつまづいた

Ruby on Railsを開発していて、バージョン管理にはGitHubを導入しています。デプロイも自動化したいと思った時に便利なのが”Capistrano”というGemです。
なお基本的な設定はこちらの記事を参考にさせていただきました。
参考:Rails 初心者が Capistrano3 で AWS EC2 にデプロイするためにがんばったこと。
しかしいくらやっても「git ls-remote」で止まってしまいます。そんな時に修正しなければいけないのは、CapistranoでのGitHubのリポジトリの指定です。

なおここでの各変数や名称は以下の通りとします。

Webアプリ(サーバー)のURL:web_app.com
GitHubのユーザー名:user_name
GitHubのパスワード:pass_word
GitHubのリポジトリ:web_app.com

「git ls-remote」で止まってしまう

Capistranoのログはこのようなものが出力されました。

DEBUG [b70aaf71] Running /usr/bin/env git ls-remote git@github.com:user_name/web_app.git as web_app@web_app.com
DEBUG [b70aaf71] Command: ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.0.0-p481 GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/web_app/git-ssh.sh /usr/bin/env git ls-remote git@github.com:user_name/web_app.git )
DEBUG [b70aaf71] Warning: Permanently added the RSA host key for IP address ‘192.30.252.129' to the list of known hosts.
DEBUG [b70aaf71] Permission denied (publickey).
DEBUG [b70aaf71] fatal: Could not read from remote repository.
DEBUG [b70aaf71] 
DEBUG [b70aaf71] Please make sure you have the correct access rights
DEBUG [b70aaf71] and the repository exists.
DEBUG [b70aaf71] Finished in 2.207 seconds with exit status 128 (failed).

GitHubのリポジトリの指定を変える

こちらの記事を見ると、どうやらGitHubのリポジトリがプライベートだとうまくいかないようです。
(果たしてこのケースにおいてプライベートかどうかが本当の原因なのかは確かめられていません…)

そこでSSHではなくHTTPを使ってGitHubのリポジトリにアクセスしてみます。

# config/deploy.rb
set :repo_url, 'https://user_name:pass_word@github.com/user_name/web_app.git'

ここでユーザー名とパスワードを指定することが重要です。
設定ファイル内にこれらの情報を書くのはよろしくないので、どこかに変数としてまとめておくのが良いでしょう。

【注意】ユーザー名とパスワードを指定する必要がある

なお以下のように設定すると再び止まってしまうので注意が必要です。

set :repo_url, 'https://github.com/user_name/web_app.git'

その時にはこのようなログが出力されます。

DEBUG [8aea6c76] Running /usr/bin/env git ls-remote https://github.com/user_name/web_app.git as web_app@web_app.com
DEBUG [8aea6c76] Command: ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.0.0-p481 GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/web_app/git-ssh.sh /usr/bin/env git ls-remote https://github.com/user_name/web_app.git )
DEBUG [8aea6c76] remote: Invalid username or password.
DEBUG [8aea6c76] fatal: Authentication failed for 'https://github.com/user_name/web_app.git/'
DEBUG [8aea6c76] Finished in 2.830 seconds with exit status 128 (failed).

まとめ

「git ls-remote」でCapistranoのデプロイが止まってしまったら『set :repo_url, ‘https://user_name:pass_word@github.com/user_name/web_app.git’』
を設定してください。

Pocket