-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_script.php
55 lines (42 loc) · 1.58 KB
/
deploy_script.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
<?php
// payload is a "post" variable sent by webhook's bitBucket
// If "payload" isn't setted, then it redirect to other page.. It prevent direct access.
if (!isset($_POST['payload'])) {
header("Location: https://www.google.com/");
exit;
} else {
// web repository
$web_dir = '~/public_html/booking/teste/';
// git directory
// in bash use command "which git" to discover
$git_bin_path = '/usr/bin/git';
// Log file name
$log_name = 'deploy_script-booking-9f164f640.log';
// Name of the branches of production and development
$branchProduction = 'master';
$branchDevelopment = 'develop';
$update = false;
// Parse data from Bitbucket hook payload
$payload = json_decode($_POST['payload']);
if (empty($payload->commits)){
// When merging and pushing to bitbucket, the commits array will be empty.
// In this case there is no way to know what branch was pushed to, so we will do an update.
$update = true;
} else {
foreach ($payload->commits as $commit) {
$branch = $commit->branch;
if ($branch === $branchDevelopment || isset($commit->branches) && in_array($branchDevelopment, $commit->branches)) {
$update = true;
break;
}
}
}
if ($update) {
// Access the repository and then do a pull to repository in bitBucket
shell_exec("cd ".$web_dir." && ".$git_bin_path." pull origin ".$branchDevelopment);
// Log the deployment
$commit_hash = shell_exec('cd '.$web_dir.' && '.$git_bin_path.' rev-parse --short HEAD');
file_put_contents($log_name, date('m/d/Y h:i:s a')." Deployed branch: ".$branch." Commit: ".$commit_hash." \n", FILE_APPEND);
}
}
?>