Cài đặt và sử dụng GitLab

1. Cài đặt và cấu hình GitLab

Tài liệu cài đặt GitLab có thể được tìm thấy tại trang này. Các yêu cầu về môi trường cài đặt có thể tham khảo tại đây.

1.1 Chuẩn bị hệ thống Ubuntu

Hệ thống cần ít nhất 4GB RAM.

1.1.1 Thiết lập kết nối từ xa

bash
user@ubuntu:~$ sudo su - root
[sudo] password for user:
root@ubuntu:~# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@ubuntu:~# vim /etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes

1.1.2 Cấu hình mạng và tên máy chủ

bash
root@ubuntu:~# cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [192.168.8.2/21]
      gateway4: 192.168.15.254
      nameservers:
        addresses: [192.168.15.254]
root@ubuntu:~# echo "gitlab.example.com" > /etc/hostname
root@ubuntu:~# reboot

1.1.3 Cài đặt GitLab

Thêm kho lưu trữ GitLab vào hệ thống:

bash
curl -fsSL https://packages.gitlab.com/gpg.key | gpg --dearmor -o /usr/share/keyrings/gitlab_gitlab-ce-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/gitlab_gitlab-ce-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu focal main" | tee /etc/apt/sources.list.d/gitlab-ce.list
apt-get update
apt-get install gitlab-ce

Hoặc tải gói deb trực tiếp và cài đặt:

bash
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/focal/main/g/gitlab-ce/gitlab-ce_13.12.9-ce.0_amd64.deb
dpkg -i gitlab-ce_13.12.9-ce.0_amd64.deb

1.2 Cấu hình cơ bản

Sửa file cấu hình `/etc/gitlab/gitlab.rb`:

bash
external_url 'http://172.16.1.31'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.example.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "user@example.com"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_domain'] = "example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "user@example.com"
user['git_user_email'] = "user@example.com"

Áp dụng cấu hình mới:

bash
gitlab-ctl reconfigure

1.3 Các lệnh thường dùng

  • `gitlab-rails`: Dùng để thực hiện các thao tác đặc biệt như thay đổi mật khẩu quản trị viên hoặc mở console cơ sở dữ liệu.
  • `gitlab-rake`: Thực hiện sao lưu và khôi phục dữ liệu.
  • `gitlab-ctl`: Quản lý dịch vụ GitLab (start, stop, restart).

2. Sử dụng GitLab

2.1 Các lệnh Git phổ biến

bash
git config --global user.name "name"
git config --global user.email "email@example.com"
git add .
git commit -m "message"
git push
git pull
git log
git reset --hard HEAD~2

2.2 Khái niệm cơ bản trong Git

  • Workspace: Thư mục chứa mã nguồn sau khi clone.
  • Staging Area: Khu vực tạm thời lưu trữ các thay đổi trước khi commit.
  • Local Repository: Lưu trữ các commit đã thực hiện trên máy local.
  • Remote Repository: Lưu trữ mã nguồn chung cho nhiều người hợp tác.

3. Sao lưu và khôi phục dữ liệu

3.1 Sao lưu dữ liệu

Sử dụng lệnh sau để tạo bản sao lưu:

bash
gitlab-rake gitlab:backup:create

Định cấu hình sao lưu trong `/etc/gitlab/gitlab.rb`:

bash
gitlab_rails['backup_path'] = "/mnt/backups"
gitlab_rails['backup_keep_time'] = 2592000

3.2 Khôi phục dữ liệu

Dừng dịch vụ GitLab:

bash
gitlab-ctl stop puma
gitlab-ctl stop sidekiq

Khôi phục từ bản backup:

bash
gitlab-rake gitlab:backup:restore BACKUP=timestamp

4. Các phương pháp triển khai mã nguồn

4.1 Triển khai xanh-đỏ

Phương pháp này đảm bảo rằng phiên bản cũ vẫn hoạt động trong khi phiên bản mới được kiểm tra trước khi chuyển sang.

4.2 Triển khai Canary

Triển khai Canary là một cách tiếp cận nơi chỉ một phần nhỏ người dùng sẽ nhận được phiên bản mới để kiểm tra tính ổn định.

4.3 Triển khai lăn

Mỗi máy chủ hoặc nhóm máy chủ sẽ lần lượt được cập nhật mà không làm gián đoạn dịch vụ.

4.4 A/B Testing

A/B Testing giúp so sánh hiệu suất của hai phiên bản ứng dụng khác nhau.

Thẻ: gitlab deployment version_control

Đăng vào ngày 30 tháng 6 lúc 08:40