回答編集履歴
1
実際に開発環境で試した方法で再回答
answer
CHANGED
@@ -1,102 +1,144 @@
|
|
1
|
+
開発環境で試したのでその方法を記載しておきます。
|
1
|
-
|
2
|
+
まず、エラーの意味についてですが、これはBitbucketのレポジトリにアクセスできないという意味です。
|
2
3
|
|
3
4
|
```
|
4
5
|
fatal: Could not switch to 'git@bitbucket.org/hogehoge': No such file or directory
|
5
6
|
```
|
6
7
|
|
7
|
-
|
8
|
+
下記のWarningは気にする必要はありません。なんらかのエラーが出た時に私の環境でも出ていました。
|
8
9
|
|
9
10
|
```
|
11
|
+
Error Output:
|
10
|
-
|
12
|
+
================
|
11
|
-
|
13
|
+
Warning: Permanently added '111.111.111.xxx' (RSA) to the list of known hosts.
|
12
|
-
bash -s" failed.
|
13
14
|
```
|
14
15
|
|
15
|
-
|
16
|
+
#### 下準備
|
16
|
-
[https://deployer.org/docs/advanced/deploy-and-git](https://deployer.org/docs/advanced/deploy-and-git)
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
下記が`~/.ssh/config`です。パーミッションも気をつける必要があります。SSHコマンドで接続できているようなので気にする必要はありません。
|
19
|
+
私の開発環境では`keychain`をインストールして試しました。実際のやり方は本回答下部の**SSHパスフレーズ対応**を参照してください。
|
20
20
|
|
21
|
-
```php
|
22
|
-
// v4.x
|
23
|
-
|
21
|
+
- **/home/user1**:`700`以下(750以下でも良いかもしれません)
|
22
|
+
- **~/.ssh**:`700`以下
|
24
|
-
|
23
|
+
- **~/.ssh/id_rsa**:`600`以下
|
25
|
-
->identityFile('/home/user1/.ssh/id_rsa.pub', '/home/user1/.ssh/id_rsa', 'パスフレーズ')
|
26
|
-
|
24
|
+
- **~/.ssh/id_rsa.pub**:`644`以下
|
27
|
-
```
|
28
25
|
|
29
|
-
#### Bitbucket SSH接続
|
30
|
-
|
31
|
-
BitbucketはSSH公開鍵を下記に登録しておかなければなりません。
|
32
|
-
https://bitbucket.org/account/user/**Bitbucketユーザー名**/ssh-keys/
|
33
|
-
|
34
|
-
Bitbucketアクセス用のSSHキーを新規作成するには下記のように`~/.ssh/bitbucket`以下に作成します。既にBitbucketにSSH公開鍵を登録してある場合は、秘密鍵と公開鍵を`~/.ssh/bitbucket`に設置します。デプロイ先サーバーと同じ鍵を使用している場合は、この工程をスキップしてください。
|
35
|
-
|
36
|
-
```
|
26
|
+
```ssh
|
37
|
-
ssh-keygen -t rsa -b 4096 -C 'Bitbucketユーザー名@bitbucket.org' -f ~/.ssh/bitbucket/id_rsa
|
38
|
-
#Enter passphrase (/home/user1/.ssh/bitbucket/id_rsa): ←ここにパスフレーズ入力
|
39
|
-
#Enter same passphrase again: ←ここに上記と同じパスフレーズ入力
|
40
|
-
```
|
41
|
-
|
42
|
-
そして、Bitbucketアクセス用のSSH設定を`~/.ssh/config`に下記のように追加・編集します。
|
43
|
-
(viやnanoなど好みのエディタで行います)
|
44
|
-
|
45
|
-
```bash
|
46
|
-
|
27
|
+
# ~/.ssh/config
|
47
|
-
|
48
|
-
#下記追加・編集
|
49
|
-
#----↓ここから↓----
|
50
28
|
Host bitbucket.org
|
29
|
+
HostName bitbucket.org
|
51
30
|
User git
|
31
|
+
identityFile ~/.ssh/bitbucket/id_rsa
|
52
32
|
Port 22
|
53
|
-
HostName bitbucket.org
|
54
|
-
identityFile ~/.ssh/bitbucket/id_rsa #Bitbucket用の秘密鍵パス
|
55
33
|
TCPKeepAlive yes
|
56
34
|
IdentitiesOnly yes
|
35
|
+
ForwardAgent yes
|
36
|
+
|
57
|
-
|
37
|
+
Host production
|
38
|
+
HostName 11.111.111.xxx
|
39
|
+
User linux
|
40
|
+
identityFile ~/.ssh/id_rsa
|
41
|
+
Port 22
|
42
|
+
TCPKeepAlive yes
|
43
|
+
IdentitiesOnly yes
|
44
|
+
ForwardAgent yes
|
58
45
|
```
|
59
46
|
|
60
|
-
|
47
|
+
下記のようなコマンドを実行して、パスフレーズなしでログインが成功すれば問題ありません。
|
61
48
|
|
62
49
|
```bash
|
63
|
-
|
50
|
+
ssh -T bitbucket.org
|
51
|
+
ssh production
|
64
52
|
```
|
65
53
|
|
66
|
-
|
54
|
+
また、Bitbucketへのアクセスを確認するために下記を実行して確認しました。パスフレーズなしで`git clone`できれば確認終了です。
|
67
55
|
|
68
56
|
```bash
|
57
|
+
mkdir /tmp/test
|
58
|
+
cd /tmp/test
|
69
|
-
|
59
|
+
git clone git@bitbucket.org:ユーザー名/レポジトリ.git
|
70
|
-
#Enter passphrase for key '/home/user1/.ssh/bitbucket/id_rsa': ←ここにパスフレーズ入力
|
71
|
-
#logged in as Bitbucketユーザー名.
|
72
|
-
#
|
73
|
-
#You can use git or hg to connect to Bitbucket. Shell access is disabled.
|
74
60
|
```
|
75
61
|
|
76
|
-
####
|
62
|
+
#### Deployer
|
77
63
|
|
78
|
-
|
64
|
+
下記が開発環境で試した`deployer-4.x`のインストール方法です。
|
79
65
|
|
80
66
|
```bash
|
81
|
-
ssh user1@111.111.111.xxx
|
82
|
-
|
67
|
+
curl -LO https://deployer.org/releases/v4.3.1/deployer.phar
|
68
|
+
sudo mv deployer.phar /usr/local/bin/dep
|
69
|
+
sudo chmod 755 /usr/local/bin/dep
|
70
|
+
composer require deployer/deployer --dev
|
71
|
+
dep init
|
72
|
+
#[common]を選択
|
83
73
|
```
|
84
74
|
|
85
|
-
最新バージョンを使っている場合は、`server()`は`host()`に変更されたので、下記のようにします。
|
86
|
-
|
75
|
+
下記は開発環境で試した`deployer-4.x`のスクリプトです。`deploy:lock`は何故か私の環境だと失敗してしまったのでコメントアウトしてあります。
|
87
76
|
|
88
77
|
```php
|
78
|
+
// deploy.php
|
79
|
+
<?php
|
80
|
+
namespace Deployer;
|
81
|
+
require 'recipe/common.php';
|
82
|
+
|
83
|
+
// Configuration
|
84
|
+
set('ssh_type', 'native');
|
85
|
+
set('ssh_multiplexing', false);
|
86
|
+
|
87
|
+
set('repository', 'git@bitbucket.org:ユーザー名/レポジトリ.git');
|
88
|
+
set('shared_files', []);
|
89
|
-
|
89
|
+
set('shared_dirs', []);
|
90
|
+
set('writable_dirs', []);
|
91
|
+
|
92
|
+
// Servers
|
90
|
-
|
93
|
+
server('production', '11.111.111.xxx', 22)
|
91
|
-
->user('
|
94
|
+
->user('linux')
|
92
|
-
->
|
95
|
+
->configFile('~/.ssh/config')
|
93
|
-
->forwardAgent(
|
96
|
+
->forwardAgent()
|
97
|
+
->set('branch', 'master')
|
94
98
|
->set('deploy_path', '/var/www/hogehoge/hogehoge');
|
99
|
+
|
100
|
+
// Tasks
|
101
|
+
desc('プロジェクト名');
|
102
|
+
task('deploy', [
|
103
|
+
'deploy:prepare',
|
104
|
+
// 'deploy:lock',
|
105
|
+
'deploy:release',
|
106
|
+
'deploy:update_code',
|
107
|
+
'deploy:shared',
|
108
|
+
'deploy:writable',
|
109
|
+
'deploy:vendors',
|
110
|
+
'deploy:clear_paths',
|
111
|
+
'deploy:symlink',
|
112
|
+
'deploy:unlock',
|
113
|
+
'cleanup',
|
114
|
+
'success'
|
115
|
+
]);
|
116
|
+
|
117
|
+
// [Optional] if deploy fails automatically unlock.
|
118
|
+
//after('deploy:failed', 'deploy:unlock');
|
95
119
|
```
|
96
120
|
|
121
|
+
下記は実際に実際にデプロイした時のコマンドです。
|
122
|
+
|
123
|
+
```bash
|
124
|
+
dep deploy
|
125
|
+
#OR
|
126
|
+
dep deploy production
|
127
|
+
|
128
|
+
#エラーが出る時は
|
129
|
+
dep deploy -vvv production
|
130
|
+
```
|
131
|
+
|
132
|
+
うまくいけば、デプロイ先サーバーの下記パスにソースが置かれます。
|
133
|
+
|
134
|
+
```
|
135
|
+
ls /var/www/hogehoge/hogehoge/current/ #下記のシンボリックリンク
|
136
|
+
ls /var/www/hogehoge/hogehoge/releases/1
|
137
|
+
```
|
138
|
+
|
97
139
|
#### SSHパスフレーズ対応
|
98
140
|
|
99
|
-
ローカルのSSHに公開キー設定をしてパスフレーズなしでSSH接続できるように`ssh-agent`を使用するか、`keychain`をインストールして対応します。
|
141
|
+
ローカルのSSHに公開キー設定をしてパスフレーズなしでSSH接続できるように`ssh-agent`を使用するか、`keychain`をインストールして対応します。
|
100
142
|
|
101
143
|
`ssh-agent`を使用する場合は(keychainは使わない)、サーバーを再起動した際に必ず下記コマンドを実行します。
|
102
144
|
|
@@ -150,12 +192,12 @@
|
|
150
192
|
```bash
|
151
193
|
cat << 'EOS' >> ~/.bashrc
|
152
194
|
|
153
|
-
if [ -
|
195
|
+
if [ -d "$HOME/.keychain" ]; then
|
154
196
|
#デプロイ先サーバー用SSH鍵
|
155
|
-
[ -f "
|
197
|
+
[ -f "$HOME/.ssh/id_rsa" ] && /usr/bin/keychain ~/.ssh/id_rsa
|
156
198
|
|
157
199
|
#Bitbucket用SSH鍵
|
158
|
-
[ -f "
|
200
|
+
[ -f "$HOME/.ssh/bitbucket/id_rsa" ] && /usr/bin/keychain ~/.ssh/bitbucket/id_rsa
|
159
201
|
|
160
202
|
source $HOME/.keychain/$HOSTNAME-sh
|
161
203
|
fi
|
@@ -166,6 +208,6 @@
|
|
166
208
|
これで、パスフレーズなしでSSH接続できるようになったので、実際にBitbucketとデプロイ先サーバーに接続確認をします。
|
167
209
|
|
168
210
|
```bash
|
169
|
-
ssh -T
|
211
|
+
ssh -T bitbucket.org
|
170
|
-
ssh
|
212
|
+
ssh production
|
171
213
|
```
|