PHP Classes

How to Setup a PHP GitHub Webhook to Deploy Projects Automatically Using the Package Webhook Deploy: Webhook handler to update projects from GitHub

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-05-05 (4 months ago) RSS 2.0 feedNot enough user ratingsTotal: 34 All time: 11,016 This week: 36Up
Version License PHP version Categories
webhookdeploy 1.0.0The PHP License7Files and Folders, Web services, Proj..., P...
Description 

Author

This package provides a Webhook handler to update projects from GitHub.

It provides a class that can take data sent by GitHub to a Webhook site address.

The class processes the data sent to the Webhook address and updates files in the site project folder from the Git repository that was updated on GitHub.

Innovation Award
PHP Programming Innovation award nominee
May 2024
Number 2
Git is a version control system that can keep track of the changes done in the files of a project.

Git became more popular after the GitHub site started hosting projects using Git.

GitHub offers a Webhook service that can send requests to given URLs when a project is updated in a Git repository hosted by GitHub.

This package implements a Webhook handler script that updates site files when GitHub accesses a Webhook URL.

Manuel Lemos
Picture of Matheusz Maydana
  Performance   Level  
Name: Matheusz Maydana <contact>
Classes: 3 packages by
Country: Brazil Brazil
Age: 29
All time rank: 4198343 in Brazil Brazil
Week rank: 163 Up12 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

declare(strict_types=1);

namespace
App\Public;

$pathVendor = __DIR__.'/../../vendor/autoload.php';
$pathLoader = __DIR__.'/../Utils/loader.php';
if(!
file_exists($pathLoader)){
    die(
'Loader não encontrado.');
}
require_once
$pathLoader;

if (!
file_exists($pathVendor)) {
    die(
'Execute o comando: composer install');
}

require_once
$pathVendor;

use
App\GitHub\GitHubHandler;
use
App\Utils\Env;
use
App\Utils\Logger;
use
Exception;

$env = new Env();
$logger = new Logger();

try {

   
$githubHandler = new GithubHandler(
       
env: $env,
       
logger: $logger,
       
headers: $_SERVER,
       
dataPost: file_get_contents('php://input')
    );

   
$githubHandler->execute();

    echo
$githubHandler->response;
    exit;

}catch (
Exception $erro){

   
$logger->log($erro->getMessage());

   
header('HTTP/1.1 500 Internal Server Error');
    echo
$erro->getMessage();
    exit;
}



Details

Configuração do Webhook para Deploy Automatizado

Este repositório contém instruções e arquivos necessários para configurar um webhook que permite o deploy automatizado de projetos hospedados no GitHub. Siga os passos abaixo para configurar o webhook em seu servidor.

Configurações Iniciais

Criação do arquivo .env

Na raiz do repositório, crie um arquivo chamado .env e insira o seguinte conteúdo:

SECRET_WEBHOOK_GITHUB=XXXXXXXXXXXX
PERSONAL_ACCESS_TOKEN_GITHUB=XXXXXXXXXXXXXXXXXX
USUARIO_GITHUB=XXXXX
DIRETORIO_DE_PROJETOS=/folder/my/projects

  • SECRET_WEBOOK_GITHUB: É a 'secret' que você configura no momento que cria o webhook no github. (Instruções)
  • PERSONAL_ACCESS_TOKEN_GITHUB: É o token de acesso pessoal que você pode gerar nas configurações da sua conta de desenvolvedor do GitHub. (Obter Token)
  • USUARIO_GITHUB: Seu nome de usuário do GitHub. Por exemplo, se o seu perfil é `https://github.com/minhaConta`, o valor seria `minhaConta`
  • DIRETORIO_DE_PROJETOS: Este é o diretório no servidor onde estão localizados os repositórios dos projetos. É a pasta principal que será usada para o deploy.

Configuração do Webhook

Configure o VirtualHost (Apache) ou a maneira como o domínio do projeto é apontado para a pasta ~/src/Public.


  Files folder image Files (8)  
File Role Description
Files folder imagesrc (3 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (8)  /  src  
File Role Description
Files folder imageGitHub (1 file)
Files folder imagePublic (2 files)
Files folder imageUtils (3 files)

  Files folder image Files (8)  /  src  /  GitHub  
File Role Description
  Plain text file GitHubHandler.php Class Class source

  Files folder image Files (8)  /  src  /  Public  
File Role Description
  Accessible without login Plain text file index.php Example Example script
  Accessible without login Plain text file robots.txt Doc. Documentation

  Files folder image Files (8)  /  src  /  Utils  
File Role Description
  Plain text file Env.php Class Class source
  Accessible without login Plain text file loader.php Aux. Auxiliary script
  Plain text file Logger.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:34
This week:0
All time:11,016
This week:36Up