質問編集履歴

2

変更

2019/04/16 08:48

投稿

cheche0830
cheche0830

スコア187

test CHANGED
File without changes
test CHANGED
@@ -14,21 +14,113 @@
14
14
 
15
15
 
16
16
 
17
+ ### デプロイファイル
18
+
19
+ ```ここに言語を入力
20
+
21
+ <?php
22
+
23
+ // リポジトリ名
24
+
25
+ $repo_name = 'hoge
26
+
27
+ ';
28
+
29
+ // Gitレポジトリの位置の指定
30
+
31
+ $git_dir = '/var/www/html/hoge/' . $repo_name . '.git';
32
+
33
+ // 展開先ディレクトリの指定
34
+
35
+ $work_tree = '/var/www/html/hoge/';
36
+
37
+ // logファイルの指定
38
+
39
+ $log_file = '/var/www/html/hoge/deploy.log';
40
+
41
+ // Gitコマンドパス
42
+
43
+ $git_command = '/usr/local/bin/git';
44
+
45
+ // リリースするブランチの指定
46
+
47
+ $deploy_ref = 'refs/heads/master';
48
+
49
+
50
+
51
+ $request_body = file_get_contents('php://input');
52
+
53
+ $request_body = urldecode($request_body);
54
+
55
+ $request_body = str_replace("payload=", "", $request_body);
56
+
57
+ $payload = json_decode($request_body);
58
+
59
+
60
+
61
+ $checkout = false;
62
+
63
+ if ($payload){
64
+
65
+ $ref = $payload->content->ref;
66
+
67
+ if ($ref == $deploy_ref) {
68
+
69
+ $checkout = true;
70
+
71
+ }
72
+
73
+ }
74
+
75
+
76
+
77
+ if ($checkout) {
78
+
79
+ exec($git_command . ' --git-dir=' . $git_dir . ' fetch');
80
+
81
+ exec($git_command . ' --git-dir=' . $git_dir . ' --work-tree=' . $work_tree . ' checkout -f');
82
+
83
+ $commit_hash = shell_exec($git_command . ' --git-dir=' . $git_dir . ' rev-parse --verify HEAD');
84
+
85
+ file_put_contents($log_file, date('r') . " Ref: " . $ref . " Commit: " . $commit_hash . "\n", FILE_APPEND);
86
+
87
+ }
88
+
89
+ ?>
90
+
91
+ ```
92
+
93
+
94
+
17
95
  ### うまくいっていない部分(php)
18
96
 
19
97
  ```ここに言語を入力
20
98
 
21
- exec('sudo git --git-dir=' . "/var/www/html/hoge.gif" . ' fetch');
99
+ if ($checkout) {
22
100
 
101
+ exec($git_command . ' --git-dir=' . $git_dir . ' fetch');
102
+
23
- exec('sudo git --git-dir=' . "/var/www/html/hoge.gif" . ' --work-tree=' . "/var/www/html/" . ' checkout -f');
103
+ exec($git_command . ' --git-dir=' . $git_dir . ' --work-tree=' . $work_tree . ' checkout -f');
104
+
105
+ $commit_hash = shell_exec($git_command . ' --git-dir=' . $git_dir . ' rev-parse --verify HEAD');
106
+
107
+ file_put_contents($log_file, date('r') . " Ref: " . $ref . " Commit: " . $commit_hash . "\n", FILE_APPEND);
108
+
109
+ }
110
+
111
+ //deploy.logはちゃんと生成されていますが、Commit: が空欄になります。
24
112
 
25
113
  ```
26
114
 
115
+ teraterm上でsshアクセスし下記を順番に行うとちゃんと出力されます。
116
+
27
- teraterm上で
117
+ ```ここに言語入力
28
118
 
29
119
  $ sudo git --git-dir=/var/www/html/hoge.gif fetch
30
120
 
31
- $ sudo git --git-dir=/var/www/html/hoge.git --work-tree=/var/www/html/ checkout -f
121
+ $ sudo git --git-dir=/var/www/html/hoge.git --work-tree=/var/www/html/hoge/ checkout -f
122
+
123
+ ```
32
124
 
33
125
 
34
126
 

1

参考

2019/04/16 08:48

投稿

cheche0830
cheche0830

スコア187

test CHANGED
File without changes
test CHANGED
@@ -37,3 +37,9 @@
37
37
  なぜかwebhookを通すとちゃんと動いてくれません。
38
38
 
39
39
  どこがおかしいでしょうか?
40
+
41
+
42
+
43
+ https://gist.github.com/hissy/6a114d0d9a85f3338aed
44
+
45
+ を参考にさせていただきました。