How we Deploy Laravel: Branches, Staging Servers, Forge and Envoyer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is pointless carnival daily team and YouTube channel are evil business and today's video will be a lesson on how to deploy projects to live and to have a staging server to test I will show you how we do that usually with help of laravel Forge but it won't be directly related to forge it's it will be more about processes so how do you deal with branches how do you merge how do you what scripts do you need to deploy what commands to run how to test and all of that so in the first part of the video I will show you how to have a staging server and deploy to life when the client approves and then in the second part we will deal with zero downtime deployments with laravel and boyer and maybe we will take a look at the versioning of every release we'll tackle that part so let's start with the local example here I have laravel test local domain which is from our own github repository one of them our projects are for quick admin panel I've cloned it locally and first how developers work with the code they clone only developed branch so in every project are at least two branches maybe more I use source tree by the way for for branching and for all that so developers cloned down developed branch only that don't shouldn't even have the master branch locally ever well in very rare situations so they clone the development up clone pull down latest changes and then for example you want to change I don't know some text button so will refresh I've already played around so we need to to change this buy and sell near you change the title to something there so buying so near you I have already the project open for example change that buy and sell near you we refresh locally it works ok then we have to push somewhere so we push develop branch source tree already finds the change we take the file and we do commit and then change of title commit message is pretty important we commit and then we push changes immediately to origin develop so now our changes are in developed branch and then we have two servers two physical servers which are usually on different physical servers in our case it will be the same software part two servers with laravel forge one domain is def demo classifieds it's long domain but this is the demo the dev demo and this is the live demo which is actually public on our quick admin panel website this is not public so this is almost same domains but this one will be known only for for client and for ourselves or maybe evil even will be restricted to some IP address for testing so we we need to deploy to the staging now right if you refresh the changes are not there yet so we need to deploy an employable forge you have such a thing called deploy script so this is what happens on every deploy pretty much it it can vary from project to project but more or less it's the same so we're changing the folder we're doing full from develop branch this is important so it's not master it's developed then we're doing composer install with some flags that it wouldn't ask us for for something because it's automatic then we're doing migrate force if there's new migration and then we start PHP F p.m. that's it and if we click deploy now afford to actually deploy that and it's done so deploy is active again deploy now button which means that there's no error of course Forge could tell us that deploy is successful but anyway it's it's successful and if we go to our dev domain and we refresh the page we should see buy-and-sell near you so here's how changes come to staging server not visible to the public yet not visible well it is visible for the public but for the clients this no one knows the domain name now we can automate that with laravel Forge and we do automate that there's a button called enable quick deploy I won't show you that how it works because in some additional configuration but anyway if it enables quick quick deploy then whenever you push something to the branch this deploy script runs automatically which means that developer needs to just push to the develop branch and that's it and then go to staging refresh and changes should be there now let's say the client approved the change this one text and we need to deploy to production then we go to github well there are multiple ways to do that but basically we need to merge the develop changes into master and then pull from master to the live server so for that we refresh the page and there should probably be a warning no there's no warning but anyway pull request with the pull request and we do from develop to master there is one commit change of title with changes so one thing which is pretty necessary to do is to review all those changes and get up is also not showing them visually so you can see clearly what are the changes create pull request and then you can assign reviewers so if you are any team probably you won't be the same person to merge the change to approve the pull request if it's a bigger team so you can assign reviewers or tell the tell someone to review and then someone else goes here and do the merge pull request so basically merge emerges from from develop to master approving the change but this is not the deployment yet so the code is from the route it is in master already but then someone needs to go to the server and pull changes so now we go to laravel Forge but not into dev demo but we have a different server for non dev and here the PostScript is a bit more fast fancy but that's pretty much the same thing and then we click deploy now and it pulls from master as you can see the only difference is get origin/master its successful as well and we go to live now demo classifieds we refresh and we should see the same change buy and sell this is how it works more or less from staging server develop branch staging server pull request and to master you can do that pretty easily without largo forge so setup and do those commands manually so get pull and merge and all of that but this is how typically we do that now we get to the second part of the video and how do we do zero time zero downtime deployments so to speak so the problem here is when i click deploy now on live server there is as you can see some some downtime so five seconds or so where the web page is not really accessible and may may throw errors may throw inconsistencies if someone is performing some operation with database during that there is no guarantee it will succeed unpredictable things for for the user so first thing what we can do without even zero downtime is to inform user that it is deploying so inside of the script which should do we should do PHP artists on down and then when it's all finished PHP artisan up so while the while the script is deploying and if we couldn't deploy now and refresh the page meanwhile yep now will be service unavailable and you can customize that page to like something is deploying or something like that and then deploy successful now and we refresh the page and the page is up again so that's the least thing you can do is to artists them down whenever you deploy something and then to artists on up whenever whenever it's actually up but now let's let's take a look at laravel and wire and how do we use that for deployments so here I am in my laravel employer dashboard I won't show you how to set up and where it's at topic and I think I did the video already I will link it up in in the description but here's my server so this is a project and in the project I need to create a server with IP address and I need to add SSH key to the server and I did that by a laravel Forge so basically what it does is if you click deploy actually let's try it yeah we click deploy you choose the branch click deploy it is deploying but the thing is the site isn't down at that stage if we refresh it's still up while it's deploying how is that because it's deploying in a separate folder on the server and then changes the link now after the deployment is fully finished and see what's happened last duration of the deployment 16 seconds and if we go to deployments tab we see the list of deployments and if we go to see details what it did clone new release installed composer activated new release so basically it's showing everything that was actually done and on top of that you can you can add deployment hooks so called deployment hooks so whenever whenever you did something you can add more action so artisan migrate clearing the cache from any basically any terminal commands here so add hooks so that is also really useful but the thing is that the thing about Anwar is zero downtime deployment because it deploys as I said in a separate folder and then changes the link so the user doesn't actually feel the downtime at all but what is even more interesting is that you can roll back the deployment so you can redeploy here's the button redeploy any changes so for example let's actually try it in action so let's change another something locally I'm still under developed branch remember let's change some I know some more text join the millions thousands change millions to thousands now then we have sourcetree and recommit with message thousands then we do what kind of repeat what we did already so we'll do a pull request and github so go to github we'll click on the repository and then we do pull requests in you pull request from develop to master create pull request now we'll merge it in fury someone else should merge it approve it but we merge and now we can click on unwired deploy so the changes should be in master well of course it should be first on staging and someone should test that but for the for this demo its deploying as you can see and whenever it's finished we'll refresh the live life website and as you can see if we refresh live website it's thousands here so it was successfully deployed and our changes are there final thing I want to show you is the releases and web hooks what is it so to deploy now we need to go to employ our website and click deploy what if we could do that from github directly so we merge the changes into master branch and we somehow trigger the deployment not automatically but from github for that we can do web hook so in deployment hooks those deployment info which says that deployments me deployments may be triggered manually or by making requests to the following URL so we need to have this URL so copy and paste here and inside of github we go to our as administrator of that repository I go to web hooks actually so web hooks and I add a web hook with that URL delete the spaces and for the events I choose on what event that trigger should be fired by default it's on pushes but we need releases and what is a release I will show you in a minute and then its active at webhook hook was successfully created we sent a link to test it out pending means that it's already deploying as you can see so already the web book has been sent and it's already deploying so be careful with that when you said I setting up well a web hook for the first time and deploy is successful but what is a release now here we go to the code here we go to releases and there aren't any release here release it's kind of a tag so tagging as you can see here tagging specific points in history so you it's kind of on version so create new release version 1.0.1 first release with some changes what exactly was changed like some text changes and you create a release publisher release that released is published and if we go to n wire as you can see it was deployed immediately was deployed immediately probably without any changes so so nothing has been actually deployed but it was triggered so this is how you deal with deployments this is our way of doing that and not on every project it varies from project to project depending on the client infrastructure and can we use for general boil like this one but this is probably the most convenient way we came up with if you have any questions probably all of those things can be discussed in details but this is what I could shoot in like 15 minutes video hope it was useful subscribe to the channel and see you guys on other videos
Info
Channel: Laravel Daily
Views: 11,591
Rating: undefined out of 5
Keywords: laravel, php, deployment, laravel forge, laravel envoyer, devops, servers, github
Id: 8DVuVftFZcQ
Channel Id: undefined
Length: 14min 53sec (893 seconds)
Published: Thu Oct 31 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.