Download the PHP package 8ctopus/git-hook without Composer
On this page you can find all versions of the php package 8ctopus/git-hook. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package git-hook
git hook
Automate deployment for git pushes to GitHub and Gitea using webhooks.
Automatically push your git repository changes to any website.
demo
- git clone the repository
- start a local php development server:
php -S localhost:80 demo.php
- run the curl request which simulates the webhook request
install
composer require 8ctopus/git-hook
GitHub usage
-
Create a new page such as this one in a public part of your website (in this example
https://example.com/api/myhook/index.php
) - In the GitHub project, go to
Settings
>Webhooks
>Add webhook
. - Set
Payload URL
tohttps://example.com/api/myhook/
- Set
Content type
toapplication/json
- Set
Secret
using a strong password (same as in the scriptSAME_SECRET_KEY_AS_IN_GITHUB_ADMIN
) - Set
Just the push event
- Check
Active
- Click
Add Webhook
- Once added, click on it and scroll to the bottom to check the first delivery. If the first delivery succeeded you are all set. If it failed, review the response error and click
Redeliver
once you think you fixed it.
Note: Also read the important notes below.
Gitea usage
-
Create a new page such as this one in a public part of your website (in this example
https://example.com/api/myhook/index.php
) -
update your gitea configuration in order to allow to send webhooks to your domain
- In the Gitea project, go to
Settings
, selectGitea
fromAdd webhook
. - Set
Target URL
tohttps://example.com/api/myhook/
- Set
HTTP Method
toPOST
- Set
Post Content Type
toapplication/json
- Set
Secret
using a strong password (same as in the scriptSAME_SECRET_KEY_AS_IN_GITEA_ADMIN
) - Set
Trigger On
toPush Events
- Set
Branch filter
tomaster
or any branch you want to trigger the script - Check
Active
- Click
Add Webhook
- Once added, click on it and scroll to the bottom and click
Test Delivery
- If the delivery succeeds you are all set. If it fails, go to the server and check the log.
important notes for both github and gitea
Note: for git pulls to work using user www-data
(the apache typically runs under that user), you probably will need to:
-
make sure the upstream is set, so git knows where to pull from
- make sure user
www-data
is the owner of the git repository. If not, you will get the error message
Note: If you are concerned about weaker security, you can consider giving user www-data
permissions to run git as another user such as ubuntu
. This way, your webserver files can be owned by ubuntu
and www-data
can only read them. I'm not security specialist, so be warned.
Note: for git pulls to work using user www-data
(the apache processes typically run under that user), you probably will need to:
- include the user and password (must be url encoded) inside the git remote url
callbacks
Callbacks can be implemented per command or for every command:
Both callbacks have the same signature
Returning false aborts the deployment
debugging
The deployment script can be easily debugged locally using ngrok.
- run ngrok
ngrok http 80
- update the
Payload URL
for Github orTarget URL
for Gitea to the ngrok address, similar to this onehttps://6daf-31-218-13-51.ngrok-free.app
- run the php local server
php -S localhost:80 demo.php
- start visual studio code debugging and set a breakpoint in
demo.php
- resend the webhook request
clean code
composer fix(-risky)
phpstan
composer phpstan
phpmd
composer phpmd