GIT is the best tool for developers to work on their code. If your code is being maintained by multiple developers, it cana be a problem to the GIT repository where you can find conflicts when you merge from multiple upstreams. The main features of RunCloud is the GIT deployment where you can push your code to GitHub or BitBucket and automatically deploy them into your staging or production server. This would be easier for developers to deploy their web application to the server without having to do a manual deployment.
GIT Deployment by Example with Laravel
I’m not gonna lie and say that that my favourite PHP Framework is Laravel although I have never test any PHP frameworks before this except my own framework, which I have abandoned for several years after learning Laravel.
The first thing to do is to create a repository for the code. Depending on your setup, you may create a repository before you code or after you have started coding. For this tutorial, I will create the repository first before I start to code. The repository will be created in my GitHub account.
The repository will be available at https://github.com/coolcodemy/runcloud-laravel-test.
Installing Laravel should be done on development machine, which is your computer to start your coding. Now, pull the latest Laravel with command below:
composer create-project --prefer-dist laravel/laravel runcloud-laravel-test -vvv
I’m installing Laravel to the work folder inside my machine since I never use vagrant or anything similiar. The
-vvv part is for verbose download, so you can see the file being download inside your machine in detail.
I’m not teaching how to actually use Laravel, so we are skipping to the part where we push the code to the GitHub.
Adding the GIT Repository to Laravel
As you might already know, GitHub will give you the command to initialize your repository inside your machine. I will use the command below to add my Laravel to GitHub.
git init git add --all git commit -m "first commit" git remote add origin firstname.lastname@example.org:coolcodemy/runcloud-laravel-test.git git push -u origin master
This is the result after I have push my code to the remote repository
Creating The Web Application
Now I will create new Web Application using RunCloud. I will set this as Default Web Application since I don’t want to hook any domain name to this web application. Default Web Application in RunCloud is the website that will be displayed when you open your web application in the browser using the IP Address. I will name this web application as laravel-test.
Creating the Deployment Key
As you can see from the image above, I am using laravel-test as the user that would run the web application. Please pay attention to this user as you will run everything using this user later. In the RunCloud panel, head to Deployment Key section and generate the deployment key for the user laravel-test.
Using the Deployment Key
Copy the generated Deployment Key and head to repository settings in the GitHub. On the left side menu bar, you will find the Deploy Key and add the key to the GitHub respository.
Attaching Repository to RunCloud Web Application
Now is the time to hook your repository to your web application. Click the web application that you have just created and click on the GIT tab. Fill in your repository details inside the simple form as shown below.
Depending on your branch, I will use master branch here since it is the only branch that I have. Now click the Attach GIT repository button and wait until your server successfully clone your repository to your server.
After you have cloned your GIT repository to your server, you will get the WebHook URL. Add this WebHook URL to your repository settings inside the GitHub. WebHook URL will automatically instruct your server to pull the latest changes of your code if you have push it to the branch that you have choosen before.
Now login to your server as laravel-test, which is the user that running the web application. Please do not login as root as PHP will only run by the user that owns the Web Application. After you have login inside your server as laravel-test, head to the Laravel folder which located in
/home/laravel-test/webapps/laravel-test. Please use SSH Key instead of password to login to your server. Now run the Laravel installation comand.
composer install -vvv cp .example.env .env php artisan key:generate
Wait until your server to finish the installation and then you can browse your new Laravel installation using your server IP Address (if you set it as Default Web Application).
Editing code and Trigger the Automatic GIT Deployment
You should never edit your file inside your server as doing so will result in a deployment error and your latest code will not be pushed to your server. Instead, edit the file inside your developent machine and push it to the remote repository.
After you have push your code to the GIT repository, you will get the WebHook history inside the GIT tab inside your web application
Now if you refresh your web application, the changes you made to your code will be automatically pulled by your server.
The deployment script is the bash script that you can instruct your server to do certain things before or after the GIT pull happen. If you are using Laravel, you will know that the Laravel is heavily depends on the
artisan command to ease your development and deployment.
To change the deployment script, scroll down to the GIT Deployment Script in the GIT tab for the web application. Here you can add your own script to run. Please make sure to include the git pull somewhere inside your deployment script. You can write script with Replacer for short hand usage.
I will share my deployment script that I have been using inside my production server.
git merge composer dump-autoload -o composer install php artisan migrate --force php artisan clear-compiled php artisan view:clear php artisan config:clear php artisan optimize composer dump-autoload -o php artisan queue:restart
Do not forget to tick the Automatically run this script upon new commit push (Auto Deploy) and click the save button. After you push your code to the GIT Repository, your server will run the deployment script that you have wrote starting from top to bottom.
And this is how you launch Laravel with GIT Deployment the right way.
6 thoughts on “Laravel With GIT Deployment The Right Way”
How do you manage/commits/push different .env files for different branches?
.env should not be tracked by git. You need to deploy same web app with different .env to achieve that
Link for replacer is no longer correct.
I have a problem with Runcloud.
We are using our on-premise Gitlab server and want to integrate git deployment.
So, in Runcloud I pick Custom Git Server, enter the Host, Repository, Branch and have added the deployment key to the repo.
In preview “This command will be run” I see that the git command is just fine, I copied it to a Linux VM, tested – it run fine.
Still, Runcloud does not let me save the configuration – for Repository field it displays an error “The repository format is invalid”.
I suspect the reason is that we are using groups and subgroups in our Gitlab organizational structure, so our repository path looks like this:
Does Runcloud have so strict validator that it doesn’t accept two / in path? But the git command is displayed correctly, why doesn’t it let me save the configuration? How to fix that?
cp .env.example .env