I gave up， xGreetings humans (I am not a bot 😶) hope you are interfacing properly?
Follow me on a journey as I show you a simple way of continuous deployment on a Laravel project using git and some other things (just read on).
For our tutorial, we’re going to need a few things before we proceed:
- A Laravel project
- Some version of git (Github would be used for this tutorial, but the process is just about the same)
- Access to a server (You can test locally tho but it won’t be the same)
- A brain
- ok I’ll stop now…
Let us start from the beginning, a laravel project
Very beautiful framework, (no regrets about cheating on asp.net [this is not a confession 😶]).
The first thing we want to do on our project is use composer to install symfony/process to our project, we are going to need this package later.
You can do that by running this simple command at the root of your project
composer require symfony/process
This would load the required loadables to your composer.json file, in order to add the package to your project you just simply run this comman
That should add the package to your vendors folder and generate the required classes and so on.
Here is a link to the Symfony docs where you can read up more about that.
After this is done, we are going to need a shell script that is going to hold the sauce to our magic.
This is how our shell script looks like:
We can call this file deploy.sh
The truth is that this is just a template, you can modify this script to suit whatever needs you might have.
Now you have to make this script executable
sudo chmod +x deploy.sh
Depending on your production environment, this method is very risky so if you’re one of those “safety” freaks, just clap for me and move on…
But if you are one with the force (Linux) please proceed, it only get’s interesting from here.
Now we have our script ready, we would need to prepare for our git webhook.
On GitHub, on your repository page, select the Settings tab, then Webhooks in the left navigation. Or go directly to the URL:
https://github.com/<your account>/<your repository>/settings/hooks
Click Add Webook:
- Payload URL: http://example.com/deploy
- Secret: Some random long String (the longer the better 😏)
Now we would need to add this webhook to our project (this is where it gets fun)
Firstly we need to add our secret to the project or in Layman’s terms we need make or project understand that there is a secret that a url needs before we proceed.
In config/app.php, add this line:
'deploy_secret' => env('APP_DEPLOY_SECRET'),
In your .env file add your webhook secret:
Now we’re done with the manual part, let’s write some codes
We need to make a controller which would house our logic and process for making our deploy process run. Now let’s make our controller…
php artisan make:controller DeployController
I’m just going to call this controller DeployController for simplicity sake.
Then we would add all our code, don’t worry I’ll explain most of it. At the end our controller should look something like this:
Before I proceed, clap for me, it’s not easy to indent your code here on medium.com.
The code above does the following:
Makes sure the post request it coming from GitHub using the X-Hub-Signature unique to github. You can remove this particular verification if you’re feeling adventurous but I recommend you keep it.
You can always refer to the git version control system documentation you are using for their own X-Signature
Makes sure the post request is coming from your github repo by verifying your deploy secret (in a production environment there are other checks before and after this, so don’t bother much about how flimsy the security might look)
Uses the symfony process to run the deploy script at the root of the project path in a shell environment
That’s the basic gist about the code above, let’s proceed to adding a route to the webhook we added to github (or whatever proper sounding English that fits, English is hard)
Navigate to route/web.php in your project and add this line
The method for this route has to always be a post method because github sends only post requests to webhooks, so you can call this another check if you want.
Secondly after this, to prevent CSRF token validation errors, we add the route above to our excepted route in the Middleware/VerifyCsrfToken.php
Which when done should look like this:
After this, on your server change the unix group of your project folder to www-data. This is necessary to allow the shell script to run in peace (allow the www-data user to update the project folder) this can be simply done by:
sudo chgrp -R www-data .
Then after all this, you are done.
With this now you have successfully set up a simple autodeployment (coughs Continuous Deployment) process on your project using git (while Jenkins and Travis are having some alone time).
If you’ve made it this far, Congratulations!!! You made it through series of bad jokes and hopefully learnt something, please a round of applause for yourself (I mean that clap button 😐)