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.
Create a Github Private Repository
Go to Github and create a private repository.
Generate User Token
Go to Github Settings -> Developer Settings -> Personal Access Tokens. Here you can choose token expiration date.
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
$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
$git commit -m "Project files"
$git push
Repository Changes
Here I have committed an index.php
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.
<?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
Create Webhook
Add your delopy.php url with your secret code.
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
$git commit -m "Project file changes"
$git push
Recent Deliveries
Now every Github push will trigger the webhoob deploy file.
Request
Github request with secret code.
Response from the webhook file
Response from the webhook.
This will happen for every branch?
ReplyDeleteWorks for master branch. You can set the default branch in Github.
DeleteYou can include the following executing in deploy.php file
git checkout branch_name
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.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete