Cách triển khai git lên host Git™ Version Control (ok)

Bước 1: Tạo remote trên github

Bước 2: Tạo file C:\Users\Administrator\Desktop\wordpress\wordpress.cpanel.yml

Cách 1: Push lên một phần thư mục hoặc một file lên thư mục chính

---
deployment:
  tasks:
    - export DEPLOYPATH=/home/lzrioghz/public_html/
    - /bin/cp -R wp-content $DEPLOYPATH
    Nó sẽ upload folder wp-content lên đường dẫn chính $DEPLOYPATH

Cách 2: Sử dụng để push toàn bộ source lên

---
deployment:
  tasks:
    - export DEPLOYPATH=/home/lzrioghz/public_html/
    - export PROJECTROOT=/home/lzrioghz/public_html
    - /bin/cp $PROJECTROOT/gitpush/* $DEPLOYPATH

Tham khảo: https://huongdan.azdigi.com/git-tren-cpanel-dong-bo-kho-chua-git-tren-cpanel-voi-remote-git/

[Git trên cPanel] Đồng bộ kho chứa Git trên cPanel với remote git

Ở một bài viết trước đó, AZDIGI đã có hướng dẫn làm việc với kho chứa git trên cPanel với một kho chứa dưới máy tính (local repository). Trong bài viết này, AZDIGI sẽ tiếp tục hướng dẫn thêm về cách đồng bộ dữ liệu từ kho chứa git trên cPanel với một kho chứa từ xa khác như Github, Gitlab,…

Trước khi sử dụng tính năng này, AZDIGI khuyến khích bạn nên biết sử dụng Git và terminal trước đó. Bạn có thể tham khảo serie Hướng dẫn Git cơ bản để nắm rõ kiến thức về Git.

Đầu tiên là truy cập vào cPanel => Git™ Version Control và ấn nút Create để tạo một kho chứa trên cPanel.

Sau đó bạn bật Clone a Repository và nhập thông tin như sau:

  • Clone a Repository: Clone kho chứa này từ một kho chứa khác.

  • Clone URL: Đường dẫn kho chứa cần clone.

  • Repository Path: Đường dẫn kho chứa trên host.

  • Repository Name: Tên kho chứa, viết liền không dấu, không khoảng trắng và không ký tự đặc biệt.

Ấn nút Create để bắt đầu tạo. Lúc này kho chứa của bạn sẽ bắt đầu tạo và clone dữ liệu về.

Pull dữ liệu – Cập nhật dữ liệu từ remote git về cPanel

Khi trên kho chứa đang kết nối tới có dữ liệu mới thì bạn có thể cập nhật nó về host cPanel (pull) bằng cách vào phần Manage của kho chứa đang làm việc.

Chọn Pull or Deploy, và chọn Update from Remote.

Nếu khi pull mà có một commit nào mới được cập nhật nó sẽ hiển thị tại phần HEAD Commit.

Push dữ liệu – Từ host cPanel lên remote git

Để sử dụng tính năng này một cách hiệu quả, AZDIGI xin đặt ra một giả thiết như sau:

Bạn có 2 website trên host cPanel như sau:

  • demo.domain.com – Địa chỉ website đang trong giai đoạn hoàn thiện và đặt tại thư mục là kho chứa git. Như trong ví dụ này là /home/azdigiweb/repositories/azrepo.

  • domain.com – Địa chỉ website chính, chạy tại thư mục /home/azdigiweb/public_html.

Và dưới máy tính của bạn có một kho chứa, bạn sẽ push những thay đổi đã hoàn thiện lên kho chứa git tại Github, sau đó là vào host tại cPanel tiến hành pull dữ liệu từ Github về thư mục đang chạy demo.domain.com để chạy thử. Sau khi chạy mà không thấy lỗi gì nữa sẽ tiến hành deploy lên thư mục website chính là domain.com.

Như vậy để làm được như vậy, chúng ta sẽ sử dụng phương pháp push deploy trên cPanel.

Trước tiên là trong thư mục kho chứa, bạn phải có một tập tin .cpanel.yml như sau:

---
deployment:
  tasks:
    - export DEPLOYPATH=/home/azdigiweb/public_html/
    - /bin/cp -rf images $DEPLOYPATH
    - /bin/cp -rf content $DEPLOYPATH

Trong đó, khai báo biến DEPLOYPATH là đường dẫn thư mục của website chính, hay còn gọi là production site. Và bên dưới nó là những lệnh mà nó sẽ thực thi khi chúng ta deploy, ví dụ ở trên là nó sẽ tự động copy hai thư mục imagescontent vào thư mục chính của website mỗi khi gửi lệnh push deploy. Bạn có thể đặt bao nhiêu lệnh ở đây tùy thích.

Sau đó push tập tin này lên kho chứa.

git add .
git commit -m "added .cpanel.yml"
git push -u origin master

Sau đó vào cPanel => Git™ Version Control => Manage => Pull or Deploy và ấn nút Update from Remote để cập nhật dữ liệu mới từ kho chứa (là tập tin .cpanel.yml mà ta vừa mới tạo).

Bây giờ bạn kiểm tra lại thư mục kho chứa trên cPanel và đảm bảo rằng tập tin .cpanel.yml xuất hiện ở đây.

Tính năng này sẽ tiến hành deploy từ kho chứa cPanel qua thư mục website chính nên bây giờ để thử nó, bạn vào mục Terminal trên giao diện cPanel để tiến hành thêm một số tập tin và thêm nó vào git để thử nhé.

Chúng ta chạy các lệnh sau để truy cập vào thư mục kho chứa trên cPanel và tạo thử một số tập tin:

cd /home/azdigiweb/repositories/azrepo
mkdir includes
touch includes/hello.txt
git add .
git commit -m "added hello.txt"
git push -u origin master

Nếu bạn chạy lệnh git commit trên cPanel bị lỗi thì chạy 2 lệnh sau để thiết lập tên và email làm việc trong Git.

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

Bây giờ bạn quay trở lại giao diện Git™ Version Control => Manage => Pull or Deploy và nút Deploy HEAD Commit sáng lên và có thể ấn vào để bắt đầu chạy tập tin .cpanel.yml và nó sẽ bắt đầu deploy từ commit hiện tại (HEAD Commit).

Sau khi chạy xong, bạn có thể thử kiểm tra thư mục website chính và sẽ thấy nó copy thư mục images và các tập tin bên trong đó theo như lệnh ta khai báo trong .cpanel.yml.

Như vậy là bạn đã hoàn tất việc sử dụng tính năng Git™ Version Control trên cPanel để deploy mã nguồn từ kho chứa máy tính lên website chính một cách an toàn nhất rồi đó.

Chúc các bạn thành công.

Hoặc t ham khảo: https://docs.cpanel.net/knowledge-base/web-services/guide-to-git-deployment/

Guide to Git™ - Deployment


Last modified: November 24, 2021

Overview

The Git™ Version Control feature allows you to deploy your cPanel-managed repositories. Generally, deployment sends finished code into production. You can use different configurations to automatically (push deployment) or manually (pull deployment) deploy changes.

  • For example, you could use deployment to make changes to your website locally. Then, automatically send them to a directory on your cPanel account.

  • For more information about how to deploy changes, read our Git™ Version Control documentation.

  • For more information about how to troubleshoot problems with this feature, read our Guide to Git - For System Administrators documentation.

  • For more information about Git commands, such as git push, git pull, or git commit, read our Guide to Git™ - Common Git Commands documentation.

Requirements

Before deployment, repositories must meet the following requirements:

  • A valid checked-in .cpanel.yml file in the top-level directory.

  • One or more local or remote branches.

If a repository does not meet these requirements, the system will not display deployment information. Also, it will disable deployment functionality.

The deployment YAML file

The .cpanel.yml file determines how and where the changed files deploy. You must check a .cpanel.yml file in to the top-level directory for each cPanel-managed repository that you deploy. The .cpanel.yml files must use the format in the examples below.

Important:

  • The files below are only examples. You must update them to suit your needs. These files will not allow you to deploy a repository successfully.

  • Don’t use a wildcard character, such as an asterisk, to deploy all files. This could deploy items like the .git directory and cause serious problems.

  • Don’t use characters that are invalid in YAML files. For more information, read the Escaped Characters section of yaml.org’s YAML Specification.

Deploy individual files

The following .cpanel.yml file deploys the index.html and style.css files to the example account’s public_html directory:

1
2
3
4
5
6
---
deployment:
  tasks:
    - export DEPLOYPATH=/home/user/public_html/
    - /bin/cp index.html $DEPLOYPATH
    - /bin/cp style.css $DEPLOYPATH
  • Line 1 is the beginning of a YAML file.

  • Lines 2 and 3 add the deployment and tasks keys, respectively.

  • Lines 4 through 6 specify an array of BASH commands to run during deployment. You can add as many commands to this array as you wish.

Note:

To add comments to this file, add a line that begins with the hash character (#).

Deploy an entire directory

The following .cpanel.yml file copies the images directory and all of its contents to the example account’s public_html directory:

1
2
3
4
5
---
deployment:
  tasks:
    - export DEPLOYPATH=/home/example/public_html/
    - /bin/cp -R images $DEPLOYPATH
  • Line 1 is the beginning of a YAML file.

  • Lines 2 and 3 add the deployment and tasks keys, respectively.

  • Lines 4 and 5 specify an array of BASH commands to run during deployment. You can add as many commands to this array as you wish.

Note:

To add comments to this file, add a line that begins with the hash character (#).

Automatic or push deployment

Important:

cPanel’s Git™ Version Control interface (cPanel >> Home >> Files >> Git Version Control) automatically adds a post-receive hook to all cPanel-managed repositories.

  • When you push changes directly to a cPanel-managed repository that includes a .cpanel.yml file, the hook deploys those changes automatically.

  • For more information, read Git’s githooks documentation.

With push deployment, a single git push command sends changes from your local computer to your cPanel-managed repository. The system then automatically runs the commands in your .cpanel.yml file. This configuration will send changes from the cPanel-managed repository to a production directory. (For example, to the directory that contains your website’s public files.)

Note:

You can use manual deployment to deploy your repository again without new changes.

Manual or pull deployment

With pull deployment, the git push command sends changes from your local computer to a remote repository.

  • When you click Update from Remote in the Pull or Deploy tab of the Manage section of cPanel’s Git™ Version Control interface (cPanel >> Home >> Files >> Git Version Control), the system retrieves changes from the remote repository and applies them to the cPanel-managed repository.

  • When you click Deploy HEAD Commit, the system runs the commands in your .cpanel.yml file to send changes from the cPanel-managed repository to a production directory. (For example, to the directory that contains your website’s public files.)

Last updated