Home WebServer PanelcPanel [Git trên cPanel] Sync Git repository on cPanel with computer

[Git trên cPanel] Sync Git repository on cPanel with computer

by Thạch Phạm
Published: Last Updated on

On the cPanel interface, a Git™ Version Control feature helps developers easily deploy source code from the computer directly to the host or synchronize with another repository through Git.

Before using this feature, AZDIGI recommends that you know how to use Git and Terminal before. You can refer to the series of Basic Git Tutorials to gain knowledge about Git.

In this case, we will both set up git synchronization from the repository directly on the cPanel host with the computer without having to go through an intermediate repository.

Some note:

  • The main domain name of the hosting package must be pointed to the host to be able to connect the repository via the domain name.
  • When pushing or pulling, fetching from the computer, it will ask for the password, you enter the host package password.

First, at the cPanel interface, you access the Git™ Version Control feature to start creating a repository on the host.

2020 05 21 B94yRFWL

Then you press the Create button on the right to start creating the repository.

2020 05 21 yktml5qZ

Next, turn off Clone a Repository and start entering the following information:

2020 05 21 8YQskSrl

  • Repository Path: Enter the repository path that you need to create, AZDIGI recommends not using the public_html directory as a repo because it will be easy to conflict repositories with each other through the directory.git .
  • Repository Name: The name of the repository, written immediately without accents, without special characters.

Click the Create button to finish.

At this point, you will go to the tutorial screen to set up this repository under your computer, there are two options to set up the repository to an existing project folder, or create a new folder. Here you can choose the option that is right for you, AZDIGI will take the example of creating a new repository on the computer.

2020 05 21 QQch8xhG

Starting at the computer, we access the directory to work with the repository on cPanel with the command cd and create a repository on your computer with git init , and then add the cPanel repository address to this directory.

AZDIGI Tutorial
git clone ssh://azdigiweb@azdigiweb.com:2210/home/azdigiweb/azrepo
cd azrepo

Now we will proceed to create a file called README.md and push it to the repository.

AZDIGI Tutorial
echo "# README" >> README.md
git add .
git commit -m "Init commit"
git push -u origin master
2020 05 21 2frkG2jn

Now you go to the repository folder on cPanel and you will see the README.md file that has just been pushed.

2020 05 21 MWFBz5Qo

Thus, the repository connection is completed between the Git repository on the computer and the cPanel host.

Automatic deployment with .cpanel.yml file

Above, I have guided you to create a repository on cPanel, but for example, that repository is syncing with a website in a development state, and the production website is located in another directory. Now we can proceed to use the .cpanel.yml file to deploy part or all of the repository through the production website directory.

Here I will take for example my production directory is /home/azdigiweb/public_html/.

You first create a file .cpanel.yml in the repository folder and declare the content as follows:

    - export DEPLOYPATH=/home/azdigiweb/public_html/
    - /bin/cp -rf wp-content $DEPLOYPATH
    - /bin/cp -rf images $DEPLOYPATH

It means we will declare a variable named $DEPLOYPATH is the value containing the path of the production website directory. And then every time we push from the computer to the repository task in the .cpanel.yml file will execute (like the example above, copy the wp-content and images folders into the production folder. You can add as many commands as you like.

Proceed to push it to the repository on cPanel.

AZDIGI Tutorial
git add .
git commit -m "Added .cpanel.yml"
git push -u origin master

Now AZDIGI will try to create a folder named images and give it a file inside.

AZDIGI Tutorial
mkdir images
touch images/hello.txt
git add .
git commit -m "upload 5 folders"
git push origin master

Now you can try again in File Manager, it will automatically execute the command to copy the images folder over to the production folder.

Wishing you success!

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign