リポジトリをGitHubに変更してCircle CIで自動デプロイしようとしたら大ハマリした
今更ながらCircle CIを使ってみる
前から興味があったCircle CIでの自動デプロイを、機会があったので試してみました。
リポジトリをBitbucketからGitHubに変えて、Circle CIに登録してデプロイできるようになるまで8時間。。。
いい経験にはなりましたが大変でした。
構成
サービス稼働中のRailsアプリに対して、デプロイ方法のみを変更します。
変更前
- リポジトリ:Bitbucket
- 手動で
rake test
- Capistranoで手動デプロイ
変更後
作業内容
circle.yml追加
# circle.yml machine: timezone: Asia/Tokyo ruby: version: 2.1.7 deployment: master: branch: master commands: - bundle exec cap production deploy - bundle exec cap production deploy:restart
リポジトリの向き先を変更
$ git remote set-url origin git@github.com:zakihaya/sample_repo.git
GitHubにpushする
$ git push origin master
Circle CIにプロジェクト追加
普通に登録する。
その後、サーバにSSHで接続するための秘密鍵を追加しておく。
Project settings → Permissions → SSH Permissions
リポジトリを修正するところは1箇所ではなかった!
これで適当な修正を加えてpushしてみると、こんな感じのSSHエラーが。。
(Backtrace restricted to imported tasks) cap aborted! SSHKit::Runner::ExecuteError: Exception while executing as deploy@example.com: git exit status: 128 git stdout: Permission denied (publickey).
この調査で3時間くらいハマる。
やってみた事は
どれをやってもうまくいかない。。。
困ったのでもう一度最初からエラーを見直してみる。
+ bundle exec cap production deploy DEBUG [1562897d] Running /usr/bin/env [ -d ~/.rbenv/versions/2.1.7 ] as deploy@example.com DEBUG [1562897d] Command: [ -d ~/.rbenv/versions/2.1.7 ] DEBUG [1562897d] Finished in 2.463 seconds with exit status 0 (successful). INFO [addbc921] Running /usr/bin/env mkdir -p /tmp/sample-repo/ as deploy@example.com DEBUG [addbc921] Command: ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.7 /usr/bin/env mkdir -p /tmp/sample-repo/ ) INFO [addbc921] Finished in 0.386 seconds with exit status 0 (successful). DEBUG Uploading /tmp/sample-repo/git-ssh.sh 0.0% INFO Uploading /tmp/sample-repo/git-ssh.sh 100.0% INFO [edd74d42] Running /usr/bin/env chmod +x /tmp/sample-repo/git-ssh.sh as deploy@example.com DEBUG [edd74d42] Command: ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.7 /usr/bin/env chmod +x /tmp/sample-repo/git-ssh.sh ) INFO [edd74d42] Finished in 0.384 seconds with exit status 0 (successful). INFO [0101ca3e] Running /usr/bin/env git ls-remote --heads git@bitbucket.org:zakihaya/sample-repo.git as deploy@example.com DEBUG [0101ca3e] Command: ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.7 GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/sample-repo/git-ssh.sh /usr/bin/env git ls-remote --heads git@bitbucket.org:zakihaya/sample-repo.git ) DEBUG [0101ca3e] Permission denied (publickey).
なんと、 bitbucket
が残っているではないか!
Circle CIがソースを取りに行っている先がBitbucketのままになっているっぽい。
確認したところ、 config/deploy.rb
で repo_url
がBitbucketのままになっていた。。。
がっかりしながら修正
# config/deploy.rb # set :repo_url, 'git@bitbucket.org:zakihaya/sample-repo.git' ← 削除 set :repo_url, 'git@github.com:zakihaya/sample_repo.git'
リポジトリを修正するところは2箇所ではなかった!
これでイケるだろうと思いきや、もう1つハードルが。
今度はこんなエラーが出る。
DEBUG [83c5bd28] Command: if test ! -d /var/rails/sample-repo/repo; then echo "Directory does not exist '/var/rails/sample-repo/repo'" 1>&2; false; fi DEBUG [83c5bd28] Finished in 0.358 seconds with exit status 0 (successful). INFO [72e69c47] Running /usr/bin/env git remote update as deploy@example.com DEBUG [72e69c47] Command: cd /var/rails/sample-repo/repo && ( RBENV_ROOT=~/.rbenv RBENV_VERSION=2.1.7 GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/sample-repo/git-ssh.sh /usr/bin/env git remote update ) DEBUG [72e69c47] Fetching origin DEBUG [72e69c47] Permission denied (publickey). DEBUG [72e69c47] fatal: The remote end hung up unexpectedly DEBUG [72e69c47] error: Could not fetch origin (Backtrace restricted to imported tasks) cap aborted! SSHKit::Runner::ExecuteError: Exception while executing as deploy@example.com: git exit status: 1 git stdout: Fetching origin Permission denied (publickey). fatal: The remote end hung up unexpectedly
今度はちゃんとエラーを見ると、サーバで git remote update
をする時にエラーになっているっぽい。
なるほど、サーバ側のリポジトリの設定にもBitbucketが残っていたのか。。
/var/rails/sample-repo
にデプロイしていたので、
サーバにログインして、リポジトリの向き先を変える。
$ cd /var/rails/remple-repo/repo $ git remote set-url origin git@github.com:zakihaya/sample_repo.git
これで全てうまくいきました。
まとめ
Capistranoを使っていて、CIする直前にリポジトリを変えた場合は気をつけましょう。