how to auto deploy code via git

how to auto deploy code via git

The objective of this script is to deploy git code to ftp directory. We need to create a script file on ftp.
http://abc.com/git_deployment.php?secret_key=XYZ
git_deployment.php is the file name in which the deployment code would be placed.
?secret_key=XYZ is the secret key which needs to be place in deployment code file.
Now open you ftp and create file with name as of url mentioned above. “git_deployment.php“. Open the file and place the code below. You need to change the variables in this file $repo_url, $branch_name, $username, $password, $secret_key.
You can download the code via git repo also or use this code.

<?
date_default_timezone_set('Asia/Karachi');
$repo_url       = ''; // Git repo full url.
$branch_name    = ''; // The branch which you need to deploy.
$username       = ''; // gitlab or any other service username.
$password       = ''; // gitlab or any other service password.
$secret_key     = ''; // this key would be passed via get. It stops the direct exection of file.
// ========= DO NOT EDIT BELOW THIS LINE ============
ob_start();
$current_path = getcwd().'/';
if(preg_match('/[^a-z_\-0-9]/i', $password))
{
  die("Only alphanumeric password is accepted.");
}
if ( $secret_key == '' )
{
    die('Please mention the secret key in file.');
}
if ( ! isset($_GET['secret_key']) || $_GET['secret_key'] != $secret_key )
{
    die('You don\'t got the key? Get the hell otta here. i dont wanna see your nasty face again.');
}
$check_get_installed = shell_exec("cd ".$current_path."; git status 2>&1");
preg_match('/git: not found/i', $check_get_installed, $matches, PREG_OFFSET_CAPTURE, 3);
if ( count($matches) > 0 )
{
    die('Please install git on your server. For ubuntu servers type: "apt-get install git"');
}
$repo_url = str_replace('https://', '', $repo_url);
$repo_url = str_replace('http://', '', $repo_url);
$output1 = shell_exec("cd ".$current_path."; git add . 2>&1");
print $output1.'<br><br>';
// print "cd ".$current_path."; git -c user.email='your@email.com' -c user.name='Your Name' commit -m 'Direct FTP changes commit on ".date('r').".' 2>&1 <br><br>";
$output2 = shell_exec("cd ".$current_path."; git -c user.email='ftp@user.com' -c user.name='FTP User' commit -m 'Direct FTP changes commit on ".date('r')."' 2>&1");
print $output2.'<br><br>';
$output3 = shell_exec("cd ".$current_path."; git push -u origin ".$branch_name." 2>&1");
 print $output3.'<br><br>';
$output = shell_exec("cd ".$current_path."; git checkout ".$branch_name."; git pull 2>&1");
print $output.'<br><br>';
preg_match('/Authentication failed/i', $output, $matches, PREG_OFFSET_CAPTURE, 3);
if ( count($matches) > 0 )
{
    die('Authentication failed. Please check your username/password.');
}
preg_match('/Not a git repository/i', $output, $matches, PREG_OFFSET_CAPTURE, 3);
if ( count($matches) > 0 )
{
    print 'Git repo does not exists. Cloning repo to this directory.<br>';
    //  git clean -xdf;
    $command = "cd ".$current_path."; git init; git remote add origin https://".$username.":".$password."@".$repo_url."; git checkout ".$branch_name."; git pull; git branch --set-upstream-to=origin/".$branch_name." ".$branch_name."; git checkout ".$branch_name." 2>&1";
//  print $command;
    print shell_exec( $command );
}
$log_content = ob_get_clean();
print $log_content;
$filename = getcwd().'/git_deployment.log';
$log_info = "
#################################
date: ".date('r')."
#################################
    ";
$log_content = $log_info . $log_content;
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a'))
{
    echo "Cannot open file ($filename)";
    exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $log_content) === FALSE)
{
    echo "<br><br>Cannot write to file ($filename)";
    exit;
}
die();
?>

You can directly hit the url to update the code on ftp http://abc.com/git_deployment.php?secret_key=XYZ or you can create a hook in your git service provider.  I assume that you have setup the git repo on gitlab.com. Now we will create a hook in gitlab web application. We will put the url under web hook section of that repo.
You can choose when you want to trigger the hook. Eg on push Event, Merge Request Event, etc
Please add the line in your code .htaccess so that no one can access the .git folder which has all the files and configurations.

RedirectMatch 404 /\.git

 
 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *