Automated Deployment of PHP Application using Github Push.
Wall Script
Wall Script
Friday, October 15, 2021

Automated Deployment of PHP Application using Github Push.

Nowadays most of my side projects are managed with Github. It has more advantages and flexibility to manage file versions. I am following a different webhook system to automatically deploy my old PHP projects. Not sure about the standards, but the following solution is an alternative approach that may solve your deployment problem for every Github push.

Automated Deployment of PHP Application using Github Push.


Create a Github Private Repository
Go to Github and create a private repository.
Create a private Github repository

Generate User Token
Go to Github Settings -> Developer Settings -> Personal Access Tokens. Here you can choose token expiration date.
Generate User Token

Clone your project
You can checkout the project using developer secret token. Make sure don't share the developer token with anyone and don't commit this to Github.
$git clone https://your_developer_token@github.com/username/your-php-application.git
$cd your-php-application

Push Changes
You can create or add files here and push to the repository
$git add .
$git commit -m "Project files"
$git push

Repository Changes
Here I have committed an index.php
Github repository

Deploy Webhook File
Create a file your-php-application-deploy.php in root level /var/www/html for Apache. If you are using XAMPP create under opt/lampp/htdocs directory. Here the code use your own secret key for code execution directly.
Shell execution
  • Removes the existing project folder.
  • Clone the project using developer token
  • Delete .git folder for preventing git config file.
Multi domain setup using .htaccess
<?php
# webhook for project deployment
if ($_GET['token'] === 'secret_key') {
$cmd = shell_exec("rm -rf your-php-application
&& git clone https://[email protected]/username/your-php-application.git
&& rm -rf your-php-application/.git
);
echo $cmd;
echo 'Deployment sucess';
} else {
echo 'Error';
}

?>

Note: Here the secret code not the Github personal developer token

Permissions for Executing
For shell executing you have to apply www-data user permission .
Apache
sudo chown -R www-data:www-data /var/www/html

XAMPP
sudo chown -R www-data:www-data /opt/lampp/htdocs

Add Webhook
Go to Github project settings and select webhook. You will find the Add Webhook button
Github repository

Create Webhook
Add your delopy.php url with your secret code.
Github repository

Push New Changes
You can modify or add files here and push to the repository
$git add .
$git commit -m "Project file changes"
$git push

Recent Deliveries
Now every Github push will trigger the webhoob deploy file.
Github repository

Request
Github request with secret code.
Github repository

Response from the webhook file
Response from the webhook.
Github repository

web notification

4 comments:

  1. Replies
    1. Works for master branch. You can set the default branch in Github.

      You can include the following executing in deploy.php file

      git checkout branch_name

      Delete
  2. You shouldn't be using shell_exec unless you know where it's coming from. Allowing users to use arbitrary inputs is bad. Especially if they have console access which shell_exec does have.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete

mailxengine Youtueb channel
Make in India
X