-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitautodeploy.php
61 lines (55 loc) · 1.91 KB
/
gitautodeploy.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
<?php
const API_KEY = "YOUR API KEY HERE";
const API_URL = "https://api.cloudways.com/api/v1";
const EMAIL = "YOUR EMAIL GOES HERE";
/* examples
const BranchName = "master";
const GitUrl = "[email protected]:user22/repo_name.git";
*/
//Use this function to contact CW API
function callCloudwaysAPI($method, $url, $accessToken, $post = [])
{
$baseURL = API_URL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_URL, $baseURL . $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Set Authorization Header
if ($accessToken) {
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $accessToken]);
}
//Set Post Parameters
$encoded = '';
if (count($post)) {
foreach ($post as $name => $value) {
$encoded .= urlencode($name) . '=' . urlencode($value) . '&';
}
$encoded = substr($encoded, 0, strlen($encoded) - 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_POST, 1);
}
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode != '200') {
die('An error occurred code: ' . $httpcode . ' output: ' . substr($output, 0, 10000));
}
curl_close($ch);
return json_decode($output);
}
//Fetch Access Token
$tokenResponse = callCloudwaysAPI('POST', '/oauth/access_token', null
, [
'email' => EMAIL,
'api_key' => API_KEY
]);
$accessToken = $tokenResponse->access_token;
$gitPullResponse = callCloudWaysAPI('POST', '/git/pull', $accessToken, [
'server_id' => $_GET['server_id'],
'app_id' => $_GET['app_id'],
'git_url' => $_GET['git_url'],
'branch_name' => $_GET['branch_name']
/* Uncomment it if you want to use deploy path, Also add the new parameter in your link
'deploy_path' => $_GET['deploy_path']
*/
]);
echo (json_encode($gitPullResponse));