回答編集履歴

1

実際に開発環境で試した方法で再回答

2017/12/21 07:15

投稿

Tomak
Tomak

スコア1652

test CHANGED
@@ -1,4 +1,6 @@
1
+ 開発環境で試したのでその方法を記載しておきます。
2
+
1
- 質問の下記エラーメッセージあるように、Bitbucketのレポジトリ接続失敗して接続できていないようです。`~/.ssh/known_hosts`が変更されなのは、`-o UserKnownHostsFile=/dev/null`とあるよにSSH Hostsを切り捨ているためです。
3
+ まず、エラーの意味ついてですがこれはBitbucketのレポジトリにアクセスできないいう意味です。
2
4
 
3
5
 
4
6
 
@@ -10,181 +12,263 @@
10
12
 
11
13
 
12
14
 
13
- 自信はありませんが、デプロイ先バーにもSSH接続失敗していると思われ
14
-
15
-
16
-
17
- ```
18
-
19
- [RuntimeException]
20
-
21
- The command "ssh -A -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p '22' -i '/home/user1/.ssh/id_rsa' 'user1@111.111.111.xxx'
22
-
23
- bash -s" failed.
24
-
25
- ```
26
-
27
-
28
-
29
- 最新バージョンではSSHパスフレーズはSSHエージェントフォーワードが推奨されるようになり、パスフレーズやパスワード認証は使えなくなったようです。
30
-
31
- [https://deployer.org/docs/advanced/deploy-and-git](https://deployer.org/docs/advanced/deploy-and-git)
32
-
33
-
34
-
35
- バージョン4系場合下記のようにしてSSH接続することができましたが、最新バージョンではできませんレポジトリのSSH接続の方は不明です
36
-
37
- [https://github.com/deployphp/deployer/compare/v4.3.0...master#diff-828ad3929c2c6e3ba47c513ec3625631R104](https://github.com/deployphp/deployer/compare/v4.3.0...master#diff-828ad3929c2c6e3ba47c513ec3625631R104)
15
+ 下記のWarning気にする必要はありません。なんらかエラが出た時私の環境でていました
16
+
17
+
18
+
19
+ ```
20
+
21
+ Error Output:
22
+
23
+ ================
24
+
25
+ Warning: Permanently added '111.111.111.xxx' (RSA) to the list of known hosts.
26
+
27
+ ```
28
+
29
+
30
+
31
+ #### 下準備
32
+
33
+
34
+
35
+ 下記が`~/.ssh/config`です。パーミッションも気をつける必要があります。SSHコマンドで接続できているようなので気にする必要はありません。
36
+
37
+ 開発環境で`keychain`をインストールして試しました。実際やり方は本回答下部の**SSHパスフレーズ対応**を参照してください
38
+
39
+
40
+
41
+ - **/home/user1**:`700`以下(750以下でも良いかもしれません)
42
+
43
+ - **~/.ssh**:`700`以下
44
+
45
+ - **~/.ssh/id_rsa**:`600`以下
46
+
47
+ - **~/.ssh/id_rsa.pub**:`644`以下
48
+
49
+
50
+
51
+ ```ssh
52
+
53
+ # ~/.ssh/config
54
+
55
+ Host bitbucket.org
56
+
57
+ HostName bitbucket.org
58
+
59
+ User git
60
+
61
+ identityFile ~/.ssh/bitbucket/id_rsa
62
+
63
+ Port 22
64
+
65
+ TCPKeepAlive yes
66
+
67
+ IdentitiesOnly yes
68
+
69
+ ForwardAgent yes
70
+
71
+
72
+
73
+ Host production
74
+
75
+ HostName 11.111.111.xxx
76
+
77
+ User linux
78
+
79
+ identityFile ~/.ssh/id_rsa
80
+
81
+ Port 22
82
+
83
+ TCPKeepAlive yes
84
+
85
+ IdentitiesOnly yes
86
+
87
+ ForwardAgent yes
88
+
89
+ ```
90
+
91
+
92
+
93
+ 下記のようなコマンドを実行して、パスフレーズなしでログインが成功すれば問題ありません。
94
+
95
+
96
+
97
+ ```bash
98
+
99
+ ssh -T bitbucket.org
100
+
101
+ ssh production
102
+
103
+ ```
104
+
105
+
106
+
107
+ また、Bitbucketへのアクセスを確認するために下記を実行して確認しました。パスフレーズなしで`git clone`できれば確認終了です。
108
+
109
+
110
+
111
+ ```bash
112
+
113
+ mkdir /tmp/test
114
+
115
+ cd /tmp/test
116
+
117
+ git clone git@bitbucket.org:ユーザー名/レポジトリ.git
118
+
119
+ ```
120
+
121
+
122
+
123
+ #### Deployer
124
+
125
+
126
+
127
+ 下記が開発環境で試した`deployer-4.x`のインストール方法です。
128
+
129
+
130
+
131
+ ```bash
132
+
133
+ curl -LO https://deployer.org/releases/v4.3.1/deployer.phar
134
+
135
+ sudo mv deployer.phar /usr/local/bin/dep
136
+
137
+ sudo chmod 755 /usr/local/bin/dep
138
+
139
+ composer require deployer/deployer --dev
140
+
141
+ dep init
142
+
143
+ #[common]を選択
144
+
145
+ ```
146
+
147
+
148
+
149
+ 下記は開発環境で試した`deployer-4.x`のスクリプトです。`deploy:lock`は何故か私の環境だと失敗してしまったのでコメントアウトしてあります。
38
150
 
39
151
 
40
152
 
41
153
  ```php
42
154
 
155
+ // deploy.php
156
+
157
+ <?php
158
+
159
+ namespace Deployer;
160
+
161
+ require 'recipe/common.php';
162
+
163
+
164
+
165
+ // Configuration
166
+
167
+ set('ssh_type', 'native');
168
+
169
+ set('ssh_multiplexing', false);
170
+
171
+
172
+
173
+ set('repository', 'git@bitbucket.org:ユーザー名/レポジトリ.git');
174
+
175
+ set('shared_files', []);
176
+
177
+ set('shared_dirs', []);
178
+
179
+ set('writable_dirs', []);
180
+
181
+
182
+
43
- // v4.x
183
+ // Servers
44
-
184
+
45
- server('production', '111.111.111.xxx')
185
+ server('production', '11.111.111.xxx', 22)
46
-
186
+
47
- ->user('user1')
187
+ ->user('linux')
48
-
188
+
49
- ->identityFile('/home/user1/.ssh/id_rsa.pub', '/home/user1/.ssh/id_rsa', 'パスフレーズ')
189
+ ->configFile('~/.ssh/config')
190
+
191
+ ->forwardAgent()
192
+
193
+ ->set('branch', 'master')
50
194
 
51
195
  ->set('deploy_path', '/var/www/hogehoge/hogehoge');
52
196
 
53
- ```
54
-
55
-
56
-
57
- #### Bitbucket SSH接続
58
-
59
-
60
-
61
- BitbucketはSSH公開鍵を下記に登録しておかなければなりません。
62
-
63
- https://bitbucket.org/account/user/**Bitbucketユーザー名**/ssh-keys/
64
-
65
-
66
-
67
- Bitbucketアクセス用のSSHキーを新規作成するには下記のように`~/.ssh/bitbucket`以下に作成します。既にBitbucketにSSH公開鍵を登録してある場合は、秘密鍵と公開鍵を`~/.ssh/bitbucket`に設置します。デプロイ先サーバーと同じ鍵を使用している場合は、この工程をスキップしてください。
68
-
69
-
70
-
71
- ```bash
72
-
73
- ssh-keygen -t rsa -b 4096 -C 'Bitbucketユーザー名@bitbucket.org' -f ~/.ssh/bitbucket/id_rsa
74
-
75
- #Enter passphrase (/home/user1/.ssh/bitbucket/id_rsa): ←ここにパスフレーズ入力
76
-
77
- #Enter same passphrase again: ←ここに上記と同じパスフレーズ入力
78
-
79
- ```
80
-
81
-
82
-
83
- そして、Bitbucketアクセス用のSSH設定を`~/.ssh/config`に下記のように追加・編集します。
84
-
85
- (viやnanoなど好みのエディタで行います)
86
-
87
-
88
-
89
- ```bash
90
-
91
- vi ~/.ssh/config
92
-
93
-
94
-
95
- #下記追加・編集
96
-
97
- #----↓ここから↓----
98
-
99
- Host bitbucket.org
100
-
101
- User git
102
-
103
- Port 22
104
-
105
- HostName bitbucket.org
106
-
107
- identityFile ~/.ssh/bitbucket/id_rsa #Bitbucket用の秘密鍵パス
108
-
109
- TCPKeepAlive yes
110
-
111
- IdentitiesOnly yes
112
-
113
- #----↑ここまで↑----
114
-
115
- ```
116
-
117
-
118
-
119
- ※新規作成場合はパーミッションを644以下に変更しておきます。
120
-
121
-
122
-
123
- ```bash
124
-
125
- chmod 600 ~/.ssh/config
126
-
127
- ```
128
-
129
-
130
-
131
- 最後に、BitbucketにSSH接続確認してみます。下記コメントのように表示されると成功です。
132
-
133
-
134
-
135
- ```bash
136
-
137
- ssh -T git@bitbucket.org
138
-
139
- #Enter passphrase for key '/home/user1/.ssh/bitbucket/id_rsa': ←ここにパスフレーズ入力
140
-
141
- #logged in as Bitbucketユーザー名.
142
-
143
- #
144
-
145
- #You can use git or hg to connect to Bitbucket. Shell access is disabled.
146
-
147
- ```
148
-
149
-
150
-
151
- #### デプロイ先サーバーSSH接続
152
-
153
-
154
-
155
- デプロイ先のサーバーへSSH接続の確認を行います。既に確認が取れている場合はこの工程をスキップしてください。
156
-
157
-
158
-
159
- ```bash
160
-
161
- ssh user1@111.111.111.xxx
162
-
163
- #Enter passphrase for key '/home/user1/id_rsa': ←ここにパスフレーズ入力
164
-
165
- ```
166
-
167
-
168
-
169
- 最新バージョンを使っている場合は、`server()`は`host()`に変更されたので、下記のようにします。
170
-
171
- [https://deployer.org/docs/hosts](https://deployer.org/docs/hosts)
172
-
173
-
174
-
175
- ```php
176
-
177
- host('production')
178
-
179
- ->hostname('111.111.111.xxx')
180
-
181
- ->user('user1')
182
-
183
- ->identityFile('/home/user1/id_rsa') //デプロイ先サーバー秘密鍵
184
-
185
- ->forwardAgent(true)
186
-
187
- ->set('deploy_path', '/var/www/hogehoge/hogehoge');
197
+
198
+
199
+ // Tasks
200
+
201
+ desc('プロジェクト名');
202
+
203
+ task('deploy', [
204
+
205
+ 'deploy:prepare',
206
+
207
+ // 'deploy:lock',
208
+
209
+ 'deploy:release',
210
+
211
+ 'deploy:update_code',
212
+
213
+ 'deploy:shared',
214
+
215
+ 'deploy:writable',
216
+
217
+ 'deploy:vendors',
218
+
219
+ 'deploy:clear_paths',
220
+
221
+ 'deploy:symlink',
222
+
223
+ 'deploy:unlock',
224
+
225
+ 'cleanup',
226
+
227
+ 'success'
228
+
229
+ ]);
230
+
231
+
232
+
233
+ // [Optional] if deploy fails automatically unlock.
234
+
235
+ //after('deploy:failed', 'deploy:unlock');
236
+
237
+ ```
238
+
239
+
240
+
241
+ 下記は実際に実際にデプロイした時のコマンドです。
242
+
243
+
244
+
245
+ ```bash
246
+
247
+ dep deploy
248
+
249
+ #OR
250
+
251
+ dep deploy production
252
+
253
+
254
+
255
+ #エラーが出る時は
256
+
257
+ dep deploy -vvv production
258
+
259
+ ```
260
+
261
+
262
+
263
+ うまくいけば、デプロイ先サーバー下記スにソスが置かれます。
264
+
265
+
266
+
267
+ ```
268
+
269
+ ls /var/www/hogehoge/hogehoge/current/ #下記のシンボリックリンク
270
+
271
+ ls /var/www/hogehoge/hogehoge/releases/1
188
272
 
189
273
  ```
190
274
 
@@ -194,7 +278,7 @@
194
278
 
195
279
 
196
280
 
197
- ローカルのSSHに公開キー設定をしてパスフレーズなしでSSH接続できるように`ssh-agent`を使用するか、`keychain`をインストールして対応します。パスフレーズは聞かれないはずですので。。すみません試してないです。
281
+ ローカルのSSHに公開キー設定をしてパスフレーズなしでSSH接続できるように`ssh-agent`を使用するか、`keychain`をインストールして対応します。
198
282
 
199
283
 
200
284
 
@@ -302,17 +386,17 @@
302
386
 
303
387
 
304
388
 
305
- if [ -f "$HOME/.keychain/$HOSTNAME-sh" ]; then
389
+ if [ -d "$HOME/.keychain" ]; then
306
390
 
307
391
  #デプロイ先サーバー用SSH鍵
308
392
 
309
- [ -f "~/.ssh/id_rsa" ] && /usr/bin/keychain ~/.ssh/id_rsa
393
+ [ -f "$HOME/.ssh/id_rsa" ] && /usr/bin/keychain ~/.ssh/id_rsa
310
394
 
311
395
 
312
396
 
313
397
  #Bitbucket用SSH鍵
314
398
 
315
- [ -f "~/.ssh/bitbucket/id_rsa" ] && /usr/bin/keychain ~/.ssh/bitbucket/id_rsa
399
+ [ -f "$HOME/.ssh/bitbucket/id_rsa" ] && /usr/bin/keychain ~/.ssh/bitbucket/id_rsa
316
400
 
317
401
 
318
402
 
@@ -334,8 +418,8 @@
334
418
 
335
419
  ```bash
336
420
 
337
- ssh -T git@bitbucket.org
421
+ ssh -T bitbucket.org
338
-
422
+
339
- ssh user1@111.111.111.xxx
423
+ ssh production
340
-
424
+
341
- ```
425
+ ```