-
Notifications
You must be signed in to change notification settings - Fork 1
/
github-deploy.php
111 lines (74 loc) · 3.37 KB
/
github-deploy.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
<?php
/*
GitHub Auto-Deploy to ServerPilot by Matt Stone and Craig Bowler
https://github.com/MeMattStone/github-auto-deploy
Based on the tutorial at https://serverpilot.io/docs/how-to-automatically-deploy-a-git-repo-from-bitbucket
*/
/* ServerPilot app details */
$sp_user_name = 'serverpilot';
$sp_app_name = 'example';
/* Branch you want to deploy from */
$branch_to_deploy = 'master';
/* Deploy to public directory only? (by default we deploy to the app directory) */
$deploy_to_public = false;
/* Run composer install after deploy to update packages? */
$run_composer = true;
/* Run custom commands after deploy? */
$run_custom_commands = false;
/* Add your custom shell commands to run here and change run_custom_commands to true */
$custom_commands = '';
/* By default we deploy to the app directory which is great for applications like Laravel */
$app_root_dir = '/srv/users/' . $sp_user_name . '/apps/' . $sp_app_name;
/* Should we deploy to the public directory only? */
if ($deploy_to_public) {
/* Add the public directory to the deployment path */
$app_root_dir .= '/public';
}
/* The hidden directory that holds a copy of your repository where we actually deploy from */
$hidden_repo_dir = $app_root_dir . '/.repo';
/* Path to binary for git. This can be set to just 'git' in most cases */
$git_bin_path = 'git';
/* Get app PHP version to ensure correct version of composer is run */
$versionArray = explode('.', phpversion());
$php_version = $versionArray[0] . '.' . $versionArray[1];
/* Path to binary for composer. This is matched to your app's PHP runtime version */
$composer_bin_path = 'composer' . $php_version . '-sp';
/* Set default update value to false */
$update = false;
/* Get the data from the webhook */
$payload_data = file_get_contents('php://input');
/* Parse data from GitHub hook payload */
$payload = json_decode($payload_data);
/* Check that the ref contains data. On a GitHub push event it should always contain the HEAD path */
if (empty($payload->ref)){
/* In this case there is no way to know what branch was pushed to, so we will do an update. */
$update = false;
} else {
/* Set the branch this commit is for */
$branch = str_replace("refs/heads/", "", $payload->ref);
/* Check if this branch matches our defined branch to deploy */
if ($branch === $branch_to_deploy) {
/* Because this is a match we will do an update (and break out of the foreach loop) */
$update = true;
}
}
/* Check if there is a valid update */
if ($update) {
/* Do a git checkout to the web root */
exec('cd ' . $hidden_repo_dir . ' && ' . $git_bin_path . ' fetch');
exec('cd ' . $hidden_repo_dir . ' && GIT_WORK_TREE=' . $app_root_dir . ' ' . $git_bin_path . ' checkout -f ' . $branch_to_deploy);
/* Should we run composer install? */
if ($run_composer) {
/* Run composer install */
shell_exec($composer_bin_path . ' install -d ' . $app_root_dir);
}
/* Should we run custom commands? */
if ($run_custom_commands) {
/* Run custom commands on shell */
shell_exec($custom_commands);
}
/* Retrieve the commit hash */
$commit_hash = shell_exec('cd ' . $hidden_repo_dir . ' && ' . $git_bin_path . ' rev-parse --short ' . $branch_to_deploy);
/* Log the result of the commit */
file_put_contents($hidden_repo_dir . '/deploy.log', date('Y-m-d h:i:s a') . " Deployed Branch: " . $branch . " Commit: " . $commit_hash, FILE_APPEND);
}