-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.php
330 lines (276 loc) · 11 KB
/
core.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* @module core
* @version 2019.01.25, 21:45
*
* Routines for work with bitbucket/github server, repositories and projects.
*
* Based on 'Automated git deployment' script by Jonathan Nicoal:
* http://jonathannicol.com/blog/2013/11/19/automated-git-deployments-from-bitbucket/
*
* See README.md and config.sample.php
*
* ---
* Igor Lilliputten
* mailto: igor at lilliputten dot ru
* http://lilliputten.ru/
*
* Ivan Pushkin
* mailto: iv dot pushk at gmail dot com
*/
/*{{{ *** Global variables */
define('DEFAULT_FOLDER_MODE', 0755);
if ( !defined('NL') ) {
define('NL',"\n");
}
$PAYLOAD = array ();
$BRANCHES = array ();
$REPO = ''; // Repository name (owner/name)
$REPO_FOLDER_NAME = ''; // Repository folder path (under `$REPOSITORIES_PATH` -- see config)
$REPO_TYPE = ''; // Repository type: bitbucket|github
$REPO_URL_PREFIX = ''; // Repo url prefix from `$REPO_URL_PREFIXES` for `$REPO_TYPE`
$REPO_URL_PREFIXES = array(
'bitbucket' => 'git@bitbucket.org',
'github' => 'git@github.com',
);
/*}}}*/
function initConfig ()/*{{{ Initializing repo configs */
{
global $CONFIG, $PROJECTS;
$tmpProjects = array();
// Bitbucket uses lower case repo names!
$hadUppercaseKeys = false;
foreach ( $PROJECTS as $repoName => $config ) {
$tmpProjects[strtolower($repoName)] = $config;
$hadUppercaseKeys = true;
}
// Rewrite projects list if has changes
if ( $hadUppercaseKeys ) {
$PROJECTS = $tmpProjects;
}
// Set default folder mode if absent
if ( empty($CONFIG['folderMode']) ) {
$CONFIG['folderMode'] = DEFAULT_FOLDER_MODE;
}
// NOTE: Log may be flushed in `_LOG_INIT` after `initConfig`
// Do not use logging here!
}/*}}}*/
function initLog ()/*{{{ Initializing log variables */
{
_LOG_INIT();
}/*}}}*/
function initPayload ()/*{{{ Get posted data */
{
global $CONFIG, $PAYLOAD, $REPO_TYPE, $REPO_URL_PREFIXES, $REPO_URL_PREFIX;
// EXAMPLE:
// HTTP_X_EVENT_KEY|HTTP_X_GITHUB_EVENT=repo:push
// HTTP_X_HOOK_UUID|HTTP_X_GITHUB_DELIVERY=5233528b-6d0d-4f41-a155-3b1c0dc2c566
// HTTP_USER_AGENT=Bitbucket-Webhooks/2.0
$event = isset($_SERVER['HTTP_X_EVENT_KEY']) ? $_SERVER['HTTP_X_EVENT_KEY'] : $_SERVER['HTTP_X_GITHUB_EVENT'];
$hook = isset($_SERVER['HTTP_X_HOOK_UUID']) ? $_SERVER['HTTP_X_HOOK_UUID'] : $_SERVER['HTTP_X_GITHUB_DELIVERY'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$addr = $_SERVER['REMOTE_ADDR'];
if (!empty($event) && !empty($hook) && !empty($agent) && !empty($addr)) {
_LOG('*** ' . $event . ' #' . $hook . ' (' . $agent . '@' . $addr . ')');
} else {
_LOG('*** Cannot detect event, hook id, remote agent or address!');
}
if ( isset($_POST['payload']) ) { // old method
$PAYLOAD = $_POST['payload'];
} else { // new method
$PAYLOAD = json_decode(file_get_contents('php://input'));
}
if ( empty($PAYLOAD) ) {
_ERROR("No payload data for checkout!");
exit;
}
if ( $CONFIG['logPayload'] ) {
_LOG_VAR('PAYLOAD',$PAYLOAD);
}
// Check for correct payload data received...
if (isset($PAYLOAD->repository->name)) {
// Bitbucket mode (changes list)...
if (isset($PAYLOAD->push->changes)) {
_LOG("Detected bitbucket mode (changes list)");
$REPO_TYPE = 'bitbucket';
}
// Github mode (one branch)...
else if (isset($PAYLOAD->ref)) {
_LOG("Detected github mode (one branch)");
$REPO_TYPE = 'github';
}
// Error???
}
if (empty($REPO_TYPE)) {
_ERROR("Invalid payload data was received -- Cannot detect repository mode (bitbucket, github)!");
exit;
}
// Determine url prefix by repository type
$REPO_URL_PREFIX = $REPO_URL_PREFIXES[$REPO_TYPE];
if (empty($REPO_URL_PREFIX)) {
_ERROR("Invalid payload data was received -- Cannot detect repository url prefix!");
exit;
}
_LOG('Repository url prefix: ' . $REPO_URL_PREFIX);
}/*}}}*/
function fetchParams ()/*{{{ Get parameters from bitbucket payload now only (REPO) */
{
global $CONFIG, $REPO, $REPO_FOLDER_NAME, $REPO_TYPE, $PAYLOAD, $PROJECTS, $BRANCHES;
// Get repository name:
$REPO = strtolower($PAYLOAD->repository->full_name);
_LOG_VAR('Repository', $REPO);
if ( empty($PROJECTS[$REPO]) ) {
_ERROR("Not found repository config for '$REPO'!");
exit;
}
// Fetch branches...
// Bitbucket mode (changes list)...
if ($REPO_TYPE === 'bitbucket') {
_LOG("Bitbucket mode (changes list)");
foreach ( $PAYLOAD->push->changes as $change ) {
// TODO: Fetch branch name for github from `$PAYLOAD->ref`
if (is_object($change->new) && $change->new->type == "branch") {
$branchName = $change->new->name;
_LOG("Found bitbucket branch: " . $branchName);
if (isset($PROJECTS[$REPO][$branchName])) {
// Create branch name for checkout
array_push($BRANCHES, $branchName);
_LOG("Found changes in branch: ".$branchName);
}
}
}
}
// Github mode (one branch)...
else if ($REPO_TYPE === 'github') {
_LOG("Github mode (one branch)");
$branchName = preg_replace('/refs\/heads\//', '', $PAYLOAD->ref);
_LOG("Found github branch: " . $branchName);
if (isset($PROJECTS[$REPO][$branchName])) {
// Create branch name for checkout
array_push($BRANCHES, $branchName);
_LOG("Found changes in branch: ".$branchName);
}
}
if ( empty($BRANCHES) ) {
_ERROR("Nothing to update (no branches found)! Please check correct branch names in your config PROJECTS list.");
// TODO: exit?
}
// Construct repository folder name
// NOTE: ATTENTION 2018.10.23, 20:45 -- Sometimes
// `$PAYLOAD->repository->name` has repository name in free form (with
// spaces etc). Now using two-level folders structure in `repositoriesPath`
// -- repositories stored with specified usernames.
// $REPO_FOLDER_NAME = strtolower($PAYLOAD->repository->name); // OLD buggy (?) code.
$REPO_FOLDER_NAME = $REPO_TYPE . '-' . preg_replace('/\//', '-', $REPO) . '.git';
_LOG_VAR('Repository folder name', $REPO_FOLDER_NAME);
}/*}}}*/
function checkPaths ()/*{{{ Check repository and project paths; create them if neccessary */
{
global $REPO, $CONFIG, $PROJECTS, $BRANCHES;
// Check for repositories folder path; create if absent
$repoRoot = $CONFIG['repositoriesPath'];
if ( !is_dir($repoRoot) ) {
$mode = $CONFIG['folderMode'];
if ( mkdir($repoRoot,$mode,true) ) {
chmod($repoRoot,$mode); // NOTE: Ensuring folder mode!
_LOG("Creating root repositories folder '".$repoRoot." (".decoct($mode).") for '$REPO'");
}
else {
_ERROR("Error creating root repositories folder '".$repoRoot." for '$REPO'! Exiting.");
exit;
}
}
// Create folder if absent for each pushed branch
foreach ( $BRANCHES as $branchName ) {
$deployPath = $PROJECTS[$REPO][$branchName]['deployPath'];
if ( !is_dir($deployPath) ) {
$mode = $CONFIG['folderMode'];
if ( mkdir($deployPath,$mode,true) ) {
chmod($deployPath,$mode); // NOTE: Ensuring folder mode!
_LOG("Creating project folder '".$deployPath.
" (".decoct($mode).") for '$REPO' branch '$branchName'");
}
else {
_ERROR("Error creating project folder '".$deployPath.
"' for '$REPO' branch '$branchName'! Exiting.");
exit;
}
}
}
}/*}}}*/
function placeVerboseInfo ()/*{{{ Place verbose log information -- if specified in config */
{
global $CONFIG; // , $REPO, $BRANCHES;
if ( $CONFIG['verbose'] ) {
// _LOG_VAR('REPO',$REPO);
// _LOG_VAR('BRANCHES',$BRANCHES);
}
}/*}}}*/
function fetchRepository ()/*{{{ Fetch or clone repository */
{
global $REPO, $REPO_FOLDER_NAME, $REPO_URL_PREFIX, $CONFIG;
// Compose current repository path
$repoRoot = $CONFIG['repositoriesPath'];
$repoPath = $repoRoot.DIRECTORY_SEPARATOR.$REPO_FOLDER_NAME;
// If repository or repository folder are absent then clone full repository
if ( !is_dir($repoPath) || !is_file($repoPath.DIRECTORY_SEPARATOR.'HEAD') ) {
_LOG("Repository folder absent for '$REPO', cloning...");
$repoUrl = $REPO_URL_PREFIX.':'.$REPO.'.git';
$cmd = 'cd "'.$repoRoot.'" && '.$CONFIG['gitCommand']
.' clone --mirror '.$repoUrl.' "'.$REPO_FOLDER_NAME.'" 2>&1';
_LOG_VAR('cmd',$cmd);
// system($cmd, $status);
exec($cmd, $output, $status);
if ( $status !== 0 ) {
_ERROR('Cannot clone repository '.$repoUrl.': '.NL.implode(NL,$output));
exit;
}
}
// Else fetch changes
else {
_LOG("Repository folder exists for '$REPO', fetching...");
$cmd = 'cd "'.$repoPath.'" && '.$CONFIG['gitCommand'].' fetch 2>&1';
_LOG_VAR('cmd',$cmd);
// system($cmd, $status);
exec($cmd, $output, $status);
if ( $status !== 0 ) {
_ERROR("Cannot fetch repository '$REPO' in '$repoPath': ".NL.implode(NL,$output));
exit;
}
}
}/*}}}*/
function checkoutProject ()/*{{{ Checkout project into target folder */
{
global $REPO, $REPO_FOLDER_NAME, $CONFIG, $PROJECTS, $BRANCHES;
// Compose current repository path
$repoPath = $CONFIG['repositoriesPath'].DIRECTORY_SEPARATOR.$REPO_FOLDER_NAME;
// Checkout project files
foreach ( $BRANCHES as $branchName ) {
$deployPath = $PROJECTS[$REPO][$branchName]['deployPath'];
$cmd = 'cd "'.$repoPath.'" && GIT_WORK_TREE="'.$deployPath.'" '.$CONFIG['gitCommand'].' checkout -f '.$branchName.' 2>&1';
_LOG_VAR('cmd',$cmd);
// system($cmd, $status);
exec($cmd, $output, $status);
if ( $status !== 0 ) {
_ERROR("Cannot checkout branch '$branchName' in repo '$REPO': ".NL.implode(NL,$output));
exit;
}
$postHookCmd = $PROJECTS[$REPO][$branchName]['postHookCmd'];
if ( !empty($postHookCmd) ) {
$cmd = 'cd "'.$deployPath.'" && '.$postHookCmd.' 2>&1';
_LOG_VAR('cmd',$cmd);
// system($cmd, $status);
exec($cmd, $output, $status);
if ( $status !== 0 ) {
_ERROR("Error in post hook command for branch '$branchName' in repo '$REPO': ".NL.implode(NL,$output));
exit;
}
}
// Log the deployment
// TODO: Catch output & errors (` 2>&1`)???
$cmd = 'cd "'.$repoPath.'" && '.$CONFIG['gitCommand'].' rev-parse --short '.$branchName;
_LOG_VAR('cmd',$cmd);
$hash = rtrim(shell_exec($cmd));
_LOG("Branch '$branchName' was deployed in '".$deployPath."', commit #$hash");
}
}/*}}}*/