Automatically Deploy Git Repo Into cPanel — Part 2 Clone and Deploy
--
If you were manually uploading your files to public_html until now, you know how boring and time killing it can be. Follow this easy article and you are over that! Don’t worry if you are a beginner or not!
This article id divided into 2 part and an option part 3. In part-1 we added cPanel’s ssh key to git. In part-2 we will clone our git repo into cPanel and deploy it into public_html. In part-3(optional) we will setup development and production environment for our project.
Checkout Part-1 here
Step 1: Add the cPanel deploy script.
Open your project and add a file named .cpanel.yml into the root directory. Add the following lines inside your project.
Let’s explain, it’s very simple.
- The DEPLOYPATH is the path where the project will be deployed. You can see, it is set to public_html directory. You can change it to any other sub-directory if you want(i.e. /public_html/portfolio/).
- DO NOT forget to change “YOUR_CPANEL_USERNAME” with your own cPanel username.
- You can see “-R” is added before css. That’s the way you add a folder and all it’s contents to deploy.
- To deploy files(i.e index.html) you have to add it one by one.
For more info you can check the following link:
So now, change this file as your project needed. Commit the changes and push to your Git repo.
Step 2: Clone the git repo
2.1 Go to your Github repo and copy the https clone url.
2.2 Go to your cPanel and click on “Git Version Control” under FILES.
2.3 In the next screen, click on “Create” button from the right side
2.4 Paste your Github Clone URL. By Default your Repository Path will be /home/YOUR_CPANEL_USERNAME/repositories directory. You can change it if you want(i.e. /home/YOUR_CPANEL_USERNAME/public_html). But, I do not recommend that. Next add/change the Repository Name. Finally press the “Create” button.
2.5 If everything is fine you should be redirected to this page and you should see your repo in the list. If you face any issue then you might have issue with SSH key. Follow PART-1 to fix it. Now, click “Manage” button.
2.6 Here you can select the branch you want to deploy. Then click “Update”. After that go to “Pull or Deploy” tab.
2.7 The FINAL step. Here you will see the last commit you made in the branch which you want to deploy. If you don’t see that, click “Update from Remote”. If everything is GOOD to go, click “Deploy HEAD commit”. If it’s success, you will see “Last Deployment Information” as below.
Now if you go to your File Manager public_html folder you should see the new files there.
If the deployment is failed please check the following:
FIX 1: Check your .cpanel.yml file carefully and check if everything is okay
FIX 2: If you are using subdirectory in public_html, go to File Manager -> public_html and create the subdirectory if they are not already there. Now try to deploy again.
FIX 3: Click “Remove” and delete the repo from the repository list and clone it again. After that try fix 2 and then deploy again.
Thats all for this article! Optionally In Part-3 we will set up development and production environment for our project.