forked from AfaanBilal/gitlab-autodeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-autodeploy.php
187 lines (153 loc) · 4.29 KB
/
gitlab-autodeploy.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
<?php
/*
* GitLab Deploy
* https://afaan.ml/gitlab-autodeploy
*
* Automatically deploy your web app repositories on "git push" or any other hook.
*
* Author: Afaan Bilal
* Author URL: https://google.com/+AfaanBilal
*
* - No Shell Access Required
* - Best for Shared Hosting Platforms
* - Public, internal and private repositories supported
*
* (c) 2016 Afaan Bilal
*
*/
// GitLab Repo ID
define('REPO_ID', 0);
// GitLab Private Token
define('PRIVATE_TOKEN', '[PRIVATE_TOKEN]');
// Deploy Directory (RELATIVE TO THIS FILE)
define('DEPLOY_DIR', '[DEPLOY_DIR]');
// LOG ? (FALSE or 'filename')
define('LOGFILE', 'gitlab-autodeploy.log');
// TimeZone (for LOG)
date_default_timezone_set("Asia/Kolkata");
// Logging
function writeLog($data)
{
if (LOGFILE == FALSE)
return;
if (!file_exists(LOGFILE))
{
$logFile = fopen(LOGFILE, "a+");
fwrite($logFile, "--------------------------------------------------------\n");
fwrite($logFile, "| PHP GitLab AUTO-DEPLOY |\n");
fwrite($logFile, "| https://afaan.ml/gitlab-autodeploy |\n");
fwrite($logFile, "| (c) Afaan Bilal ( https://google.com/+AfaanBilal ) |\n");
fwrite($logFile, "--------------------------------------------------------\n");
fwrite($logFile, "\n\n");
fclose($logFile);
}
$fh = fopen(LOGFILE, "a+");
fwrite($fh, "\nTimestamp: ".date("d-m-Y h:i:s a"));
fwrite($fh, "\n\n {$data}");
fwrite($fh, "\n\n");
fclose($fh);
}
// Zip Extraction
function ExtractZip($zipFile, $extractTo)
{
$zip = new ZipArchive;
if ($zip->open($zipFile) === TRUE)
{
$zip->extractTo($extractTo);
$zip->close();
return TRUE;
}
else
return FALSE;
}
// Remove directories, sub-directories and files (including dot files)
function recursiveRemoveDirectory($dir)
{
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file)
{
(is_dir("$dir/$file")) ? recursiveRemoveDirectory("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
// recursive copy
function copyr($source, $dest)
{
// Simple copy for a file
if (is_file($source))
{
//chmod($dest, 0777);
return copy($source, $dest);
}
// Make destination directory
if (!is_dir($dest))
mkdir($dest);
chmod($dest, 0777);
// Loop through the folder
$dir = dir($source);
while (FALSE !== $entry = $dir->read())
{
// Skip pointers
if ($entry == '.' || $entry == '..')
continue;
// Deep copy directories
if ($dest !== "$source/$entry")
copyr("$source/$entry", "$dest/$entry");
}
// Clean up
$dir->close();
return TRUE;
}
function recursiveMoveDirectory($src, $dest)
{
if (copyr($src, $dest))
{
recursiveRemoveDirectory($src);
return TRUE;
}
return FALSE;
}
echo "Deploying...<br>";
$ProjectData = json_decode( file_get_contents("https://gitlab.com/api/v3/projects/".REPO_ID."?private_token=".PRIVATE_TOKEN), true );
if ( in_array("message", array_keys($ProjectData)) )
{
writeLog("Error: ".$ProjectData['message']);
exit;
}
$RepositoryWebURL = explode('/', $ProjectData['web_url']);
$RepositoryName = $RepositoryWebURL[ count( $RepositoryWebURL ) - 1 ];
$zipRepo = "https://gitlab.com/api/v3/projects/".REPO_ID."/repository/archive.zip?private_token=".PRIVATE_TOKEN;
$zipLocal = "{$RepositoryName}.zip";
if (!file_exists( DEPLOY_DIR ))
{
mkdir( DEPLOY_DIR , 0777 );
}
else
{
chmod( DEPLOY_DIR, 0777 );
recursiveRemoveDirectory( DEPLOY_DIR );
mkdir( DEPLOY_DIR , 0777 );
}
// Download the latest zip
copy($zipRepo, $zipLocal);
$tempDir = uniqid("temp");
$logStr = "";
// Deploy
if ( ExtractZip($zipLocal, $tempDir) )
{
if ( recursiveMoveDirectory( glob("{$tempDir}/{$RepositoryName}*")[0], DEPLOY_DIR ) )
$logStr = "Deployed {$RepositoryName} to ".DEPLOY_DIR;
else
$logStr = "Error: Failed on MOVE";
}
else
$logStr = "Error: Failed on ExtractZip";
writeLog($logStr);
echo $logStr;
// Prevent cPanel SoftException
chmod(DEPLOY_DIR, 0755);
// Clean up
unlink ($zipLocal);
chmod($tempDir, 0777);
recursiveRemoveDirectory($tempDir);
?>