-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitbucket-deploy.php
305 lines (224 loc) · 10.9 KB
/
bitbucket-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
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
<?php
/* Basic settings */
$projectName = "Project X"; // Just for email notifications
$email = "[...]@into-digital.fi";
$repoUri = "[email protected]:intodigital/[...].git";
$remoteBranch = "master"; // master, staging or production
$bbjson = @file_get_contents('bitbucket-config.json');
if ($bbjson) {
$bbconf = @json_decode($bbjson);
if ($bbconf) {
$projectName = $bbconf->projectName;
$email = $bbconf->projectEmail;
$repoUri = $bbconf->projectUri;
$remoteBranch = $bbconf->projectBranch;
}
}
/* Advanced settings */
//ini_set("display_errors", 1);
$gitPath = "git"; // Git binary path if not global. "/usr/local/cpanel/3rdparty/bin/git" etc.
$localProjectPath = dirname(__FILE__); // Change if project is not in same directory as this deploy script
// Don't fuck with it. Unless you really want to.
// -------------------------------------------------------------------
if ($repoUri == '[email protected]:intodigital/[...].git' || $email == '[...]@into-digital.fi') {
logput_stuff('Check settings before running');
if ($repoUri == '[email protected]:intodigital/[...].git') {
logput_stuff('Get SSH/[email protected] $repoUri from bitbucket');
}
if ($email == '[...]@into-digital.fi') {
logput_stuff('Set your email address to $email');
}
exit;
}
if (strpos($repoUri, 'git@') !== 0) {
logput_stuff('Use SSL ([email protected]...) as $repoUri. HTTPS requires password and thus fails.');
mail_stuff($email, $projectName . ': Use SSL URI for deploy', 'Use SSL ([email protected].../) as $repoUri. HTTPS requires password and thus fails.');
exit;
}
// Script start
output_stuff("Start!");
log_stuff("\n-----");
$runningFromWebhook = false;
$payload = file_get_contents('php://input');
if (!empty($payload)) {
$runningFromWebhook = true;
log_stuff("Running webhook");
} else {
logput_stuff("Running deploy manually...");
}
// Make sure we're in the right directory.
shell_exec("cd " . $localProjectPath);
// Check if git is initialized at all and remote repository is set & clone if not.
if (!is_dir("./.git")) {
// exec git init
logput_stuff("Git not initialized, running git init");
$output = shell_exec($gitPath . ' init');
if (!empty($output)) {
log_stuff($output);
}
if (!is_dir("./.git")) {
logput_stuff("Couldn't initialize repository, stopping...");
mail_stuff($email, $projectName . ': Couldn\'t initialize git repository', 'Does the server have git installed?');
exit();
} else {
logput_stuff("Git initialized!");
}
}
// Doesn't actually check if "origin" is set, but instead if there is any remote at all, so this may cause issues if there are multiple remotes.
$gitRemotes = trim(shell_exec($gitPath . " config remote.origin.url"));
if (!empty($gitRemotes) && $gitRemotes != $repoUri) {
logput_stuff('$repoUri does not match .git/config:remote.origin.url. Local .git/ was probably uploaded to server, try deleting it.');
mail_stuff($email, $projectName . ': Remote url mismatch', "bitbucket-config.json:\n'$repoUri'\n.git/config\n'$gitRemotes'\n\nLocal .git/ was probably uploaded to server, try deleting it.");
exit();
} else if (empty($gitRemotes)) {
// Set remote.
logput_stuff("Remote origin not set, running git remote add origin " . $repoUri);
$output = shell_exec($gitPath . " remote add origin " . $repoUri);
if (!empty($output)) {
log_stuff($output);
}
// Remote add origin doesn't give any message so check again if any remotes exist.
$gitRemotes = trim(shell_exec($gitPath . " config remote.origin.url"));
if (empty($gitRemotes)) {
logput_stuff("Couldn't add remote, stopping...");
mail_stuff($email, $projectName . ': Couldn\'t add remote', 'I have no idea, ask someone.');
exit();
} else {
logput_stuff("Origin set!");
}
// Fetch commits
$output = shell_exec($gitPath . " fetch origin {$remoteBranch}");
// Destroy everything!
$output = shell_exec($gitPath . " reset --hard FETCH_HEAD");
// Link local master to track desired remote branch.
$output = shell_exec($gitPath . " branch --set-upstream-to=origin/{$remoteBranch} master");
}
// presuming git is properly configured and can pull stuff
// -------------------------------------------------------------------
// First update remote info and check if remote branch is ahead
logput_stuff("Check if update is needed.");
$output = shell_exec($gitPath . " checkout master"); // ignores tags
$output = shell_exec($gitPath . " remote update");
$localHeadHash = shell_exec($gitPath . " rev-parse HEAD");
$remoteHeadHash = shell_exec($gitPath . " rev-parse remotes/origin/" . $remoteBranch);
if ($localHeadHash == $remoteHeadHash) {
logput_stuff("Local is up to date with remote.");
$localHeadHash = shell_exec($gitPath . " rev-parse --short HEAD");
logput_stuff("Local HEAD is now at " . $localHeadHash);
}
// tags and past commits can be used to revert back
if (!$runningFromWebhook && !empty($_GET['tag'])) {
logput_stuff("Reverting to tag " . $_GET["tag"]);
$output = shell_exec($gitPath . " checkout tags/" . $_GET['tag']);
output_stuff($output);
output_stuff("Done!");
$localHeadHash = shell_exec($gitPath . " rev-parse --short HEAD");
logput_stuff("Local HEAD is now at " . $localHeadHash);
exit();
} else if (!$runningFromWebhook && !empty($_GET["commit"])) {
logput_stuff("Reverting to commit " . $_GET["commit"]);
// Check if commit is valid and is included in right branch, only local.
$localBranchListContainingCommit = shell_exec($gitPath . " branch -a --contains " . $_GET["commit"]);
if (strpos($localBranchListContainingCommit, "remotes/origin/" . $remoteBranch) !== false) {
// If commit is ok, revert back to it.
// Note that version fast forwards back to HEAD when remote is updated and webhook is called.
$output= shell_exec($gitPath . " reset --hard " . $_GET["commit"]);
output_stuff($output);
logput_stuff("Done!");
$localHeadHash = shell_exec($gitPath . " rev-parse --short HEAD");
logput_stuff("Local HEAD is now at " . $localHeadHash);
} else {
logput_stuff("Commit " . $_GET["commit"] . " is not valid.");
}
exit();
}
// Discards any changes to _tracked_ files since our last deploy, if something is changed outside version control
// Works only if script is run manually with reset-hard GET-parameter. I.e. won't trigger with normal git push / merge.
if (!$runningFromWebhook && isset($_GET["reset-hard"])) {
logput_stuff("NOTICE! Local changes discarded with git reset --hard HEAD! Resetting repository...");
$output = shell_exec($gitPath . " reset --hard HEAD");
output_stuff($output);
}
// Check and stop if there are any local changes that would be overwritten with pull
$diff = shell_exec($gitPath . " diff --name-only");
if (!empty($diff)) {
logput_stuff("Pull canceled because there are local changes in _TRACKED_ files.");
logput_stuff("Changed files:");
foreach(explode("\n", $diff) as $filename) {
if ($filename != "") {
logput_stuff("- " . $filename, false);
}
}
// Since nothing has changed on server, try to inform last commiter via email
$message = "Recently made changes on project \"" . $projectName . "\" branch \"" . $remoteBranch . "\" were not deployed on server \"" . $_SERVER[HTTP_HOST] . "\" because there are local changes in git tracked files. This is most likely due to some asshat updating files directly to server instead of using version control. \r\n\r\nThese git tracked files have changed outside version control: \r\n";
foreach(explode("\n", $diff) as $filename) {
if ($filename != "") {
$message .= "- " . $filename . "\r\n";
}
}
$message .= "\r\nYou should merge changes in listed files to version control and then run deploy script manually with get parameter \"reset-hard\". You can find the deployment script url in repository settings -> webhooks. \r\n\r\n";
$message .= "NOTE! Reset-all deletes all changes done directly on server excluding files that are not tracked. So please take backup or make sure all those changes are moved to version control first because otherwise those changes are gone for good.";
// get email address of last commiter and send email
$authorName = shell_exec($gitPath . " --no-pager show -s --format='%an'"); // git --no-pager show -s --format='%an <%ae>'
$authorEmail = shell_exec($gitPath . " --no-pager show -s --format='%ae'");
if (!$authorEmail) {
$authorEmail = $email;
}
if ($authorEmail) {
mail_stuff($authorEmail, "Project " . $projectName . " not updated on server!", $message);
} else {
logput_stuff("Author " . $authorName . " does not have email set. Cannot send notifications.");
}
output_stuff("Stopping");
$localHeadHash = shell_exec($gitPath . " rev-parse --short HEAD");
logput_stuff("Local HEAD is now at " . $localHeadHash);
exit();
}
// Do pull if everything else is ok.
logput_stuff("Starting pull...");
exec($gitPath . " pull origin " . $remoteBranch, $output, $return_var);
if ($return_var > 0) {
logput_stuff('Pull failed! Test connection on server with "ssh -T [email protected]". Ask for help.');
mail_stuff($email, $projectName . ': Pull failed!', ' Connect to server with SSH/Putty and test connection with "ssh -T [email protected]". You may need to accept connections to bitbucket, or may be missing ACCESS KEY in bitbucket. Check readme: https://bitbucket.org/intodigital/bitbucket-deploy-script');
exit;
} else if (!empty($output)) {
log_stuff($output);
}
$localHeadHash = shell_exec($gitPath . " rev-parse --short HEAD");
logput_stuff("Local HEAD is now at " . $localHeadHash);
// I guess here could be some post deploy scripts or stuff.
// For example if some database update is required.
//output_stuff("The end. Check bitbucket-deploy.log for details.");
// some functions for stuff
function log_stuff ($stuff, $logDate = true) {
if (!file_exists("bitbucket-deploy.log")) {
$fp = fopen(dirname(__FILE__)."/bitbucket-deploy.log", "a+");
fclose($fp);
}
if ($logDate) {
error_log(date("Y-m-d H:i:s") . ": " . $stuff . "\n", 3, dirname(__FILE__) . "/bitbucket-deploy.log");
} else {
error_log($stuff . "\n", 3, dirname(__FILE__) . "/bitbucket-deploy.log");
}
}
function output_stuff ($stuff) {
if (php_sapi_name() == "cli") {
// In cli-mode
echo $stuff."\n";
} else {
// Not in cli-mode
echo $stuff."<br>";
}
}
function logput_stuff ($stuff, $logDate = true) {
log_stuff($stuff, $logDate);
output_stuff($stuff);
}
function mail_stuff ($email, $subject, $stuff) {
if (!mail($email, $subject, $stuff)) {
logput_stuff("Couldn't send email to last committer (" . $email . ")");
} else {
logput_stuff("Email sent to last committer (" . $email . ")");
}
}
?>