LoginSignup
52
52

More than 5 years have passed since last update.

Capistrano 3.x で GitHub(プライベートリポジトリ)からソースコードを取得する3種類の方法について

Last updated at Posted at 2014-09-10

バージョン3からバージョン2まであったローカルでソースコードを取得して配布する仕組み(deploy_via :copyなど)がなくなりました

ソースコードはアプリケーションサーバ(デプロイ先)で取得する必要があり、大きく以下の3種類の方法があります

  1. SSH Agent Forwarding
  2. デプロイ先にSSHを設定する
  3. HTTP Authentication

今回はその方法の手順について説明します

試したバージョン

  • ruby - 2.1.1p76
  • capistrano - 3.1.0
  • Vagrant - 1.6.3
  • CentOS - 6.5

準備

ひな形を用意しているのでそれを利用してください

$ git clone https://github.com/ryshinoz/capistrano_sample.git
$ cd capistrano_sample
$ vagrant up
$ vagrant ssh-config --host=capistrano_sample >> ~/.ssh/config
$ ssh capistrano_sample
Last login: Wed Sep 10 05:26:12 2014 from 10.0.2.2
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

一応エージェントから全ての鍵を削除します

$ ssh-add -D
All identities removed.

config/deploy.rbにリモートリポジトリURL(SSH)を設定します

set :repo_url, 'リモートリポジトリURL(SSH)'

deploy:checkを実行するとエラーになればOKです

$ cap development deploy
INFO[79c3aeec] Running /usr/bin/env mkdir -p /tmp/my_app_name/ on capistrano_sample
DEBUG[79c3aeec] Command: /usr/bin/env mkdir -p /tmp/my_app_name/
INFO[79c3aeec] Finished in 0.089 seconds with exit status 0 (successful).
...
...
DEBUG[7767f692]     Permission denied (publickey).
DEBUG[7767f692]     fatal: The remote end hung up unexpectedly
DEBUG[7767f692] Finished in 1.623 seconds with exit status 128 (failed).

SSH Agent Forwarding

デフォルトではSSH Agent Forwardingを利用してソースコードを取得します、デプロイするユーザがGitHubにSSHで接続できればいいので簡単な方法です

手順

  1. アプリケーションサーバにログインしてgit ls-remote リモートリポジトリURL(SSH)を実行してknown_hostsにホストを追加します ※ コマンド自体は失敗しますが問題ありません

    $ ssh capistrano_sample
    $ git ls-remote リモートリポジトリURL(SSH)
    The authenticity of host 'github.com (192.30.252.129)' can't be established.
    RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
    Permission denied (publickey).
    fatal: The remote end hung up unexpectedly
    
  2. アプリケーションサーバからログアウトします

  3. エージェントに鍵を追加します

    $ ssh-add ~/.ssh/id_rsa_github
    Enter passphrase for /Users/xxxxx/.ssh/id_rsa_github:
    Identity added: /Users/xxxxx/.ssh/id_rsa_github (/Users/xxxxx/.ssh/id_rsa_github)
    
  4. development環境に対してdeploy:checkが成功します

    $ cap staging deploy:check
    INFO[b50dbbe4] Running /usr/bin/env mkdir -p /tmp/my_app_name/ on capistrano_sample
    DEBUG[b50dbbe4] Command: /usr/bin/env mkdir -p /tmp/my_app_name/
    INFO[b50dbbe4] Finished in 0.084 seconds with exit status 0 (successful).
    ...
    ...
    INFO[a16e3f55] Finished in 0.012 seconds with exit status 0 (successful).
    

デプロイ先にSSHを設定する

SSH Agent Forwardingはすごく簡単なのですが、SSH Agent Forwardingが利用できない環境などで困ります

そのような場合はデプロイ先(アプリケーションサーバ)毎にSSHの設定をする方法があります

手順

  1. アプリケーションサーバでGenerating SSH KEYSに従い鍵作成、設定をする

  2. config/deploy/development.rbを修正してSSH Agent Forwardingを無効にする

    set :ssh_options, {
      forward_agent: false
    }
    
  3. 一応エージェントから鍵を削除する

    $ ssh-add -D
    All identities removed.
    
  4. development環境に対してdeploy:checkが成功します

    $ cap staging deploy:check
    INFO[b50dbbe4] Running /usr/bin/env mkdir -p /tmp/my_app_name/ on capistrano_sample
    DEBUG[b50dbbe4] Command: /usr/bin/env mkdir -p /tmp/my_app_name/
    INFO[b50dbbe4] Finished in 0.084 seconds with exit status 0 (successful).
    ...
    ...
    INFO[a16e3f55] Finished in 0.012 seconds with exit status 0 (successful).
    

HTTP Authentication

HTTPの認証を利用する方法です、以下の2種類方法があるのですが2段階認証しているとUsernameとPasswordは利用できないので利用シーンはないのではないかと思います

  • UsernameとPassword
  • UsernameとPersonal Access Token

手順

  1. GitHubでPersonal Access Tokenを取得する
  2. リモートリポジトリURL(HTTP)を設定する

    set :repo_url, https://Username:Personal Access Token@リポジトリURL(HTTP)
    
  3. development環境に対してdeploy:checkが成功します

    $ cap staging deploy:check
    INFO[b50dbbe4] Running /usr/bin/env mkdir -p /tmp/my_app_name/ on capistrano_sample
    DEBUG[b50dbbe4] Command: /usr/bin/env mkdir -p /tmp/my_app_name/
    INFO[b50dbbe4] Finished in 0.084 seconds with exit status 0 (successful).
    ...
    ...
    INFO[a16e3f55] Finished in 0.012 seconds with exit status 0 (successful).
    

Links

以上

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