Skip to content

Instantly share code, notes, and snippets.

@ccloli
Created April 15, 2017 16:02
Show Gist options
  • Save ccloli/11f025d26e633c92f8bd15ae3f4704de to your computer and use it in GitHub Desktop.
Save ccloli/11f025d26e633c92f8bd15ae3f4704de to your computer and use it in GitHub Desktop.
PHP Git Deployment Script, deploy your repo with GitHub zipball
<?php
//error_reporting(E_ERROR);
set_time_limit(600);
$owner = '';
// repo owner
$repo = '';
// repo namespace
$token = '';
// access token, only required when it's a private repo
// to create a token, go to https://github.com/settings/tokens
$tmp = './';
// tmp dir, end with '/'
$dir = './';
// output dir, end with '/'
// DO NOT MODIFIED THE CONTENT BELOW, ONLY IF YOU HAVE A GOOD REASON
header('Content-Type: text/plain; charset=utf-8');
echo "Git Deployment Script\n";
echo "Deploy your repo with GitHub zipball\n";
echo "@version 0.2\n";
echo "@author ccloli\n\n";
flush();
ob_flush();
echo "Downloading repo package...";
flush();
ob_flush();
// use legacy HTTP GET parameters is okay
$url = 'https://api.github.com/repos/' . urlencode($owner) . '/' . urlencode($repo) . '/zipball';
if ($token) {
$url .= '?access_token=' . urlencode($token);
}
// we need to set a User Agent, or GitHub will give 403 Forbidden
$opts = array(
'http' => array(
'user_agent' => 'Git Deployment Script'
)
);
$fp = fopen($url, 'rb', false, stream_context_create($opts));
$httpCode = substr($http_response_header[0], 9, 3);
switch (substr($httpCode, 0, 1)) {
case '4':
echo "FAILED!\nSeems that the repo doesn't exist or the access token isn't correct.";
echo "Response Headers:\n";
var_dump($http_response_header);
exit;
break;
case '5':
echo "FAILED!\nSeems that GitHub has some problem, follow GitHub Status and try again later.";
echo "Response Headers:\n";
var_dump($http_response_header);
exit;
break;
}
foreach ($http_response_header as $value) {
if (strpos($value, 'Content-Disposition') !== FALSE && strpos($value, 'filename=') !== FALSE) {
$fileName = substr($value, strpos($value, 'filename=') + 9);
break;
}
}
if (!isset($fileName)) {
$fileName = $repo . '.zip';
}
file_put_contents($tmp . $fileName, $fp);
echo "OK!\n";
flush();
ob_flush();
echo 'Decompressing the archive...';
flush();
ob_flush();
$zip = new ZipArchive;
if ($zip -> open($fileName) !== TRUE) {
echo "FAILED!\nCannot open the zip file.";
exit;
}
$zip -> extractTo($tmp);
$zip -> close();
echo "OK!\n";
flush();
ob_flush();
echo 'Replacing with new files...';
flush();
ob_flush();
// the dir name will be the zip file name
$filesDir = $tmp . explode('.', $fileName, 2)[0];
$newFiles = scandir($filesDir);
foreach ($newFiles as $file) {
if ($file != '.' && $file != '..') {
// unlink and loop rmdir really makes me dizzy
//unlink($dir . $file);
if (is_dir($dir . $file)) {
if (PHP_OS === 'Windows' || PHP_OS === 'WINNT') {
exec('rd /s /q "' . $dir . $file . '"');
}
else {
exec('rm -rf "' . $dir . $file . '"');
}
}
else {
unlink($dir . $file);
}
rename($filesDir . '/' . $file, $dir . $file);
}
}
echo "OK!\n";
flush();
ob_flush();
echo 'Removing temp files...';
flush();
ob_flush();
unlink($tmp . $fileName);
rmdir($filesDir);
echo "OK!\nDeploy finished!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment