GithubHelp home page GithubHelp logo

orphans / git-deploy-laravel Goto Github PK

View Code? Open in Web Editor NEW
34.0 6.0 14.0 54 KB

Helps automate the deployment of projects onto servers by utilising Git's web hooks.

License: MIT License

PHP 84.50% Blade 15.50%

git-deploy-laravel's Introduction

Deployment of Laravel projects using Git webhooks

git-deploy-laravel allows for automated deployment using webhook requests from your repository's server, and automatically pulls project code using the local Git binary.

This should work out-of-the-box with Laravel 5.x using with webhooks from GitHub and GitLab servers.

This is an internal tool to help with our common workflow pattern but please feel free to borrow, change and improve.

Installation

Step 1

Add the following to your composer.json file then update your composer as normal:

{
    "require" : {
        "orphans/git-deploy-laravel" : "dev-master"
    }
}

Or run:

composer require orphans/git-deploy-laravel

Step 2

Add the /git-deploy route to CSRF exceptions so your repo's host can send messages to your project.

In file in app/Http/Middleware/VerifyCsrfToken.php add:

protected $except = [
    'git-deploy',
];

Step 3 Optional

In case you need additional action after a successful commit, you can add you own Event Listener. For example you can write your own update script to run migrations etc.

1) Create a Listener to perform action when a git deployment is done. Open the App/Listeners directory (or create it if it doesn't exist). Now create a new file and call it GitDeployedListener.php. Paste in this code:

<?php

namespace App\Listeners;

use \Orphans\GitDeploy\Events\GitDeployed;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Queue\ShouldQueue;

class GitDeployedListener implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        // Here you can setup something
    }

    /**
     * Handle the event.
     *
     * @param  ReactionAdded  $event
     * @return void
     */
    public function handle(GitDeployed $gitDeployed)
    {
        // Do some magic with event data $gitDeployed contains the commits

    }
}

As you can see, it's a normal event listener. You might notice that the listener implements ShouldQueue โ€” it's useful because our app must respond fast. If you want some long-running stuff in your event listener, you need to configure a queue.

2) Now we add this listener to /App/Providers/EventServiceProvider.php like any other event listener:

// ...

protected $listen = [

        // ...

       \Orphans\GitDeploy\Events\GitDeployed::class => [
            \App\Listeners\GitDeployedListener::class
        ]
    ];

// ...

Usage

Add a webhook for http://your.website.url/git-deploy to your project in GitHub/GitLab and this package will take care of the rest. The webhook should fire on push-events.

Your website will automatically receive POST messages from the repo manager and perform a Git pull.

Configuration

In most cases the package will find the correct Git repository and Git executable but we advise publishing our config anyway because it will let you enable extra security options and email notifications.

To add custom configuration run:

php artisan vendor:publish --provider="Orphans\GitDeploy\GitDeployServiceProvider"

Then edit /config/gitdeploy.php to suit your needs.

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Email recipients
    |--------------------------------------------------------------------------
    |
    | The email address and name that notification emails will be sent to.
    | Leave the array empty to disable emails.
    |
    | [
    |     ['name' => 'Joe Bloggs', 'address' => '[email protected]'],
    |     ['name' => 'Jane Doe', 'address' => '[email protected]'],
    |     ...
    | ]
    |
    */

    'email_recipients' => [],

    /*
    |--------------------------------------------------------------------------
    | Email sender
    |--------------------------------------------------------------------------
    |
    | The email address and name that notification emails will be sent from.
    | This will default to the sender in config(mail.from) if left null.
    |
    */

    'email_sender' => ['address' => null, 'name' => null],

    /*
    |--------------------------------------------------------------------------
    | Repository path
    |--------------------------------------------------------------------------
    |
    | This the root path of the Git repository that will be pulled. If this
    | is left empty the script will try to determine the directory itself
    | but looking for the project's .env file it's nearby .git directory.
    |
    | No trailing slash
    |
    */

    'repo_path' => '',

    /*
    |--------------------------------------------------------------------------
    | Allowed sources
    |--------------------------------------------------------------------------
    |
    | A request will be ignored unless it comes from an IP listed in this
    | array. Leave the array empty to allow all sources.
    |
    | This is useful for a little extra security if you run your own Git
    | repo server.
    |
    | Relies on the REMOTE_ADDR of the connecting client matching a value
    | in the array below. So if using IPv6 on both the server and the
    | notifing git server, then make sure to add it to the array. If your git
    | server listens on IPv4 and IPv6 it would be safest to add both.
    |
    | e.g.
    | 
    | 'allowed_sources' => ['192.160.0.1', '::1'], 
    |
    */

    'allowed_sources' => [],

    /*
    |--------------------------------------------------------------------------
    | Remote name
    |--------------------------------------------------------------------------
    |
    | The name of the remote repository to pull the changes from
    |
    */
    
    'remote' => 'origin',

    /*
    |--------------------------------------------------------------------------
    | Git binary path
    |--------------------------------------------------------------------------
    |
    | The full path to the system git binary. e.g. /usr/bin/git
    |
    | Leave blank to let the system detect using the current PATH variable
    |
    */
    
    'git_path' => '',

    /*
    |--------------------------------------------------------------------------
    | Maintenance mode
    |--------------------------------------------------------------------------
    |
    | Allow the git hook to put the site into maintenance mode before doing
    | the pull from the remote server.
    |
    | After a successful pull the site will be switched back to normal
    | operations. This does leave a possibility of the site remaining in
    | maintenance mode should an error occur during the pull.
    |
    */

    'maintenance_mode' => true,

    /*
    |--------------------------------------------------------------------------
    | Fire Event
    |--------------------------------------------------------------------------
    |
    | Allow the git hook to fire a event "GitDeployed" so that everybody can listen to that event.
    | See readme how to create a nice listener on that.
    |
    */
    'fire_event' => true,

    /*
    |--------------------------------------------------------------------------
    | Secret signature
    |--------------------------------------------------------------------------
    |
    | Allow webhook requests to be signed with a secret signature.
    |
    | If 'secret' is set to true, Gitdeploy will deny requests where the
    | signature does not match. If set to false it will ignore any signature
    | headers it recieves.
    | 
    | For Gitlab servers, you probably want the settings below:
    | 
    |     'secret_type' => 'plain',
    |     'secret_header' => 'X-Gitlab-Token',
    |
    | For Github, use something like the below (untested):
    |
    |    'secret_type' => 'hmac',
    |    'secret_header' => 'X-Hub-Signature',
    */
   
    'secret' => false,

    /**
     * plain|hmac
     */
    'secret_type' => 'plain',

    /**
     * X-Gitlab-Token|X-Hub-Signature
     */
    'secret_header' => 'X-Gitlab-Token',

    /**
     * The key you specified in the pushing client
     */
    'secret_key' => '',

];

Future Plans

  • Email report on code conflicts that prevent a pull
  • Support for performing composer install after deployment
  • Support for restarting laravel queues after deployment with artisan queue:restart
  • Support for running custom artisan commands after successful pulls

git-deploy-laravel's People

Contributors

dpslwk avatar edjeavons avatar muffinman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

git-deploy-laravel's Issues

Doesn't deploy on server despite hook works seemingly

Hy There,

1stly thank you for making this laravel deployment for GitLab. This could make our job faster and more precise. However I have a problem, could you help me?
I use GitLab and installed your sollution as you've explained. Wasn't hard, loved the easiness of it. On gitlab I can test if it can reach and execute the hook. It sais OK. But whenever I push something from my local computer to the repository, it doesn't get pulled to the server.
What do you need to have a clue? I use Laravel 5.1 Framework and GitLab 8.4

I've made a custom configuration according to your manual and set only a server repository.

Oh and BTW: \config\gitdeploy.php ? You should reverse the prior. :) On linux it is the other way around!

500 - Web hook data does not look valid

Hello,

I seem to be getting a 500 response on my webhook. The message says "Web hook data does not look valid". I'm only sending push events, not sure what's wrong

Template & Security

Hi all,

while looking for a nice webhook, I came across your package. And without having it installed I ask myself how you manage the security e.g. with tokens so that not everybody could call your script?
Further I miss the maintenance mode or the script to have updates without downtime. Maybe I missed something? Some templates, examples, for your config would really improve the package.

Thank you.

logger not updated

Error: Call to undefined method Monolog\Logger::addError() in file /home/ubuntu/my-app/vendor/orphans/git-deploy-laravel/src/Http/GitDeployController.php on line 48

GitDeploy is not working with Laravel 5.5

Hello,

Gitdeploy was working perfectly but after migrating our project from Laravel 5.4 to Laravel 5.5 our webhook stopped working as you can see in the screenshot

capture d ecran de 2017-10-26 20-10-26

Routes not found

vendor/orphans/git-deploy-laravel/src/http/routes.php): failed to open stream: No such file or directory

"pull" command is never fired but laravel log says it is

So I've gone through some initial setup and fixed the secret_header and secret_key and that seems to be working. I'm getting a 200 OK response with "true" as post body when a push event is triggered from GitLab.

The storage/logs/laravel.log says this so it should be doing a "git pull":

[2018-06-23 16:04:26] local.INFO: Gitdeploy: putting site into maintenance mode
[2018-06-23 16:04:26] local.INFO: Gitdeploy: Pulling latest code on to server
[2018-06-23 16:04:26] local.INFO: Gitdeploy: taking site out of maintenance mode
[2018-06-23 16:04:26] local.DEBUG: Gitdeploy: Event GitDeployed fired

I've even tried doing /usr/bin/git --git-dir='/var/www/project-name/.git' --work-tree='/var/www/project-name' pull origin master as that is the command the package is running and that works.

The problem is that the "pull" command is never fired. If I do a "git pull" it pulls the latest changes but the package does nothing. I've tried checking permissions and modifying them but to no effect.

What am I doing wrong here? Is there something I can do to log more data?

Empty postdata when deploying from github

In file vendor/orphans/git-deploy-laravel/src/http/GitDeployController.php line 46 should be
$postdata = json_decode($request->get('payload'), TRUE);
if you are using github
Tested via laravel 5.7

500 error - Undefined index: ref

I'm using Github and have things mostly working. The trigger actually pulls the code but it dies sometime after that with this error message:

Undefined index: ref

The middle line below from GitDeployController.php is where it chokes:
// Get branch this webhook is for $pushed_branch = explode('/', $postdata['ref']); $pushed_branch = trim($pushed_branch[2]);
Weirdly enough, if I check the payload on my github webhook it looks like this:
{ "ref": "refs/heads/master", "before": "2ed33d476e7514eb079fa36bbd1280f3061a7c78", "after": "f2115760f1239c74c533c59d4af29f24c9ee85a9", "created": false, "deleted": false, "forced": false, "base_ref": null, ...
So the ref is definately there. Any ideas? I'm not getting email notifications att his point so would like to solve this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.