GIT is the best tools for developers to work on their code. If your code is being maintained by multiple developers, it would add much fun 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 manual deployment.
GIT Deployment by Example with Laravel
I’m not gonna lie that my favourite PHP Framework is Laravel although I have never test any PHP framework before this except my own framework, which I have abandon it for several years after learning Laravel.
The first thing to do is to create the repository for the code. Depending on your setup, you may create repository before you code or after you have start 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 in 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 the 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 would see the file being download inside your machine.
I’m not teaching how to use Laravel, so we just skip to the part where we should push the code to the GitHub.
Adding the GIT Repository to Laravel
As you should 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 email@example.com: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 didn’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 server in browser using 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 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. In 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 clone 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 own 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 although it is just a small typo. Doing so will resulting in deployment error and you will never get your latest code push to your server. Now 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.