PHPackages                             8ctopus/git-hook - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [DevOps &amp; Deployment](/categories/devops)
4. /
5. 8ctopus/git-hook

ActiveLibrary[DevOps &amp; Deployment](/categories/devops)

8ctopus/git-hook
================

Automate deployment for git pushes to GitHub and Gitea

3.0.2(5mo ago)3143MITPHPPHP &gt;=8.0CI passing

Since Dec 13Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/8ctopus/git-hook)[ Packagist](https://packagist.org/packages/8ctopus/git-hook)[ Docs](https://github.com/8ctopus/git-hook)[ RSS](/packages/8ctopus-git-hook/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (9)Versions (24)Used By (0)

git hook
========

[](#git-hook)

[![packagist](https://camo.githubusercontent.com/849097c1d5b399f0434dd2a2c9fd855eba82926c8f95371d672bb5e8e9184993/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f6769742d686f6f6b2f76)](https://packagist.org/packages/8ctopus/git-hook)[![downloads](https://camo.githubusercontent.com/f4a34ab943a03a33ec4dc51d73962ce96bac23f51b7b2acaef06f6069828d125/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f6769742d686f6f6b2f646f776e6c6f616473)](https://packagist.org/packages/8ctopus/git-hook)[![min php version](https://camo.githubusercontent.com/64c4346b30020bfee6c6b097fe6daa422b58769dffe4047b816ae627fd6d08aa/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f6769742d686f6f6b2f726571756972652f706870)](https://packagist.org/packages/8ctopus/git-hook)[![license](https://camo.githubusercontent.com/0e67f084efc21d14d66a6b3ca888c19c420279720d1dee33fc0ee7ad32de77fe/68747470733a2f2f706f7365722e707567782e6f72672f3863746f7075732f6769742d686f6f6b2f6c6963656e7365)](https://packagist.org/packages/8ctopus/git-hook)[![tests](https://github.com/8ctopus/git-hook/actions/workflows/tests.yml/badge.svg)](https://github.com/8ctopus/git-hook/actions/workflows/tests.yml)[![code coverage badge](https://raw.githubusercontent.com/8ctopus/git-hook/image-data/coverage.svg)](https://raw.githubusercontent.com/8ctopus/git-hook/image-data/coverage.svg)[![lines of code](https://raw.githubusercontent.com/8ctopus/git-hook/image-data/lines.svg)](https://raw.githubusercontent.com/8ctopus/git-hook/image-data/lines.svg)

Automate deployment for git pushes to [GitHub](https://github.com) and [Gitea](https://gitea.io) using webhooks.

Automatically push your git repository changes to any website.

demo
----

[](#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

```
curl \
    --request POST \
    --header "content-type: application/json" \
    --header "X-HUB-SIGNATURE-256: 2d8e4a6f3114e41f09a65195b2d69b5844e7a1a9284cdb2671354568304dd7a6" \
    --data '{"ref":"refs/heads/master", "before":"fc7fc95de2d998e0b41e17cfc3442836bbf1c7c9", "after": "fc7fc95de2d998e0b41e17cfc3442836bbf1c7c9", "total_commits":1, "repository":{"name": "site"}}' \
    http://localhost/
```

install
-------

[](#install)

```
composer require 8ctopus/git-hook

```

GitHub usage
------------

[](#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`)

```
use Apix\Log\Logger\File;
use HttpSoft\ServerRequest\ServerRequestCreator;
use Oct8pus\GitHubHook;

// assuming script is in DOCUMENT_ROOT/public/api/myhook/index.php
$documentRoot = __DIR__ . '/../../..';

require_once $documentRoot . '/vendor/autoload.php';

$commands = [
    // repository name eg. https://github.com/8ctopus/site
    'site' => [
        // directory in which commands will be executed
        'path' => $documentRoot,
        'commands' => [
            // adjust to your flavor
            '/usr/bin/git pull',
            '/usr/local/bin/composer install --no-interaction --no-dev',
        ],
    ],
];

// the logger is optional but provides useful information (any PSR-3 logger will do)
// to use this logger, composer require 8ctopus/apix-log
$logger = new File(sys_get_temp_dir() . '/git-hook-' . date('Y-m-d-His') . '.log');

try {
    $logger->info('Git hook...');

    $request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST);

    (new GitHubHook($request, $commands, 'SAME_SECRET_KEY_AS_IN_GITHUB_ADMIN', $logger))
        ->run();

    $logger->notice('Git hook - OK');
} catch (Exception $exception) {
    if ($exception->getCode() !== 0) {
        // informs the webhook that the command failed
        http_response_code($exception->getCode());

        // REMOVE ME IN PRODUCTION
        echo $exception->getMessage();
    }
}
```

- In the GitHub project, go to `Settings` &gt; `Webhooks` &gt; `Add webhook`.
- Set `Payload URL` to `https://example.com/api/myhook/`
- Set `Content type` to `application/json`
- Set `Secret` using a strong password (same as in the script `SAME_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
-----------

[](#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`)

```
declare(strict_types=1);

use Apix\Log\Logger\File;
use HttpSoft\ServerRequest\ServerRequestCreator;
use Oct8pus\GiteaHook;

// assuming script is in DOCUMENT_ROOT/public/api/myhook/index.php
$documentRoot = __DIR__ . '/../../..';

require_once $documentRoot . '/vendor/autoload.php';

$commands = [
    'site' => [
        'path' => $documentRoot,
        'commands' => [
            // adjust to your flavor
            '/usr/bin/git pull',
            '/usr/local/bin/composer install --no-interaction --no-dev',
        ],
    ],
];

// the logger is optional but provides useful information (any PSR-3 logger will do)
// to use this logger, composer require 8ctopus/apix-log
$logger = new File(sys_get_temp_dir() . '/git-hook-' . date('Y-m-d-His') . '.log');

try {
    $logger->info('Git hook...');

    $request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST);

    (new GiteaHook($request, $commands, 'SAME_SECRET_KEY_AS_IN_GITEA_ADMIN', $logger))
        ->run();

    $logger->notice('Git hook - OK');
} catch (Exception $exception) {
    if ($exception->getCode() !== 0) {
        // informs the webhook that the command failed
        http_response_code($exception->getCode());

        // REMOVE ME IN PRODUCTION
        echo $exception->getMessage();
    }
}
```

- update your gitea configuration in order to allow to send webhooks to your domain

```
[webhook]
SKIP_TLS_VERIFY = false
ALLOWED_HOST_LIST = example.com
```

- In the Gitea project, go to `Settings`, select `Gitea` from `Add webhook`.
- Set `Target URL` to `https://example.com/api/myhook/`
- Set `HTTP Method` to `POST`
- Set `Post Content Type` to `application/json`
- Set `Secret` using a strong password (same as in the script `SAME_SECRET_KEY_AS_IN_GITEA_ADMIN`)
- Set `Trigger On` to `Push Events`
- Set `Branch filter` to `master` 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
-----------------------------------------

[](#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

```
git branch --set-upstream-to=origin/master master

```

- make sure user `www-data` is the owner of the git repository. If not, you will get the error message

```
[2023-05-05 16:23:21] ERROR fatal: detected dubious ownership in repository at '...'

```

*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.

```
sudo -H -u ubuntu -- /usr/bin/git pull;

```

*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

```
- git remote set-url origin https://user:password@example.com/gitea/site.git
```

callbacks
---------

[](#callbacks)

Callbacks can be implemented per command or for every command:

```
$commands = [
    'site' => [
        'path' => $path,
        'commands' => [
            'git status' => commandCallback(...),
            'composer install --no-interaction',
        ],
        'afterExec' => globalCallback(...),
    ],
];
```

Both callbacks have the same signature

```
function callback(?LoggerInterface $logger, string $command, string $stdout, string $stderr, string $status) : bool
```

Returning false aborts the deployment

debugging
---------

[](#debugging)

The deployment script can be easily debugged locally using [ngrok](https://ngrok.com/).

- run ngrok `ngrok http 80`
- update the `Payload URL` for Github or `Target URL` for Gitea to the ngrok address, similar to this one `https://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
----------

[](#clean-code)

```
composer fix(-risky)

```

phpstan
-------

[](#phpstan)

```
composer phpstan

```

phpmd
-----

[](#phpmd)

```
composer phpmd

```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance70

Regular maintenance activity

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~54 days

Recently: every ~231 days

Total

21

Last Release

171d ago

Major Versions

1.0.1 → 2.0.02023-05-05

2.4.2 → 3.0.02024-03-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/4dafd5f7ef8134a5c9b231686c5da3d6416db09139b45aac0b26952178dffb8a?d=identicon)[8ctopus](/maintainers/8ctopus)

---

Top Contributors

[![8ctopus](https://avatars.githubusercontent.com/u/13252042?v=4)](https://github.com/8ctopus "8ctopus (142 commits)")

---

Tags

deploymentgitgiteagithubphpwebhookgithubgitwebhookHOOKdeploymentgitea

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/8ctopus-git-hook/health.svg)

```
[![Health](https://phpackages.com/badges/8ctopus-git-hook/health.svg)](https://phpackages.com/packages/8ctopus-git-hook)
```

###  Alternatives

[shopify/shopify-api

Shopify API Library for PHP

4634.8M16](/packages/shopify-shopify-api)[fadion/maneuver

Easily deploy Laravel projects via FTP or SFTP, using Git for versioning.

1729.1k](/packages/fadion-maneuver)[fortrabbit/craft-copy

Tooling for Craft on fortrabbit

7617.2k1](/packages/fortrabbit-craft-copy)[webklex/laravel-git-hook

Laravel Git Hook

191.2k](/packages/webklex-laravel-git-hook)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
