เชื่อมต่อ Git กับ GitLab บน Self-hosted VPS

GitLab Self-hosted เป็น Git repository management platform ที่สามารถติดตั้งบน VPS ของตัวเองได้ โดยสร้าง private Git server พร้อม CI/CD, issue tracking, และ project management ทั้งหมด บทความนี้จะแนะนำวิธีติดตั้ง GitLab CE บน Ubuntu VPS และเชื่อมต่อจาก local Git

ทำไมต้อง GitLab Self-hosted?

  • ควบคุมข้อมูลเต็มที่ – ไม่ต้องอาศัย GitHub หรือ GitLab.com
  • สร้าง private repositories ฟรี
  • CI/CD pipeline ในตัว
  • ไม่มีค่าใช้บริการ (Community Edition)
  • Customizable ตามความต้องการ

ความต้องการของระบบ (System Requirements)

  • Ubuntu 18.04 LTS ขึ้นไป
  • CPU: 2 cores ขั้นต่ำ
  • RAM: 4GB ขั้นต่ำ (แนะนำ 8GB)
  • Storage: 20GB ขั้นต่ำ
  • Sudo access

ติดตั้ง GitLab CE บน VPS (Ubuntu Step-by-Step)

# Update system
sudo apt-get update
sudo apt-get upgrade -y

# Install dependencies
sudo apt-get install -y curl openssh-server ca-certificates

# Add GitLab repository
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

# Install GitLab CE
sudo apt-get install -y gitlab-ce

# Configure and start GitLab
sudo gitlab-ctl reconfigure

# Check status
sudo gitlab-ctl status

แก้ไข Configuration (Optional)

sudo nano /etc/gitlab/gitlab.rb

# Edit external_url to your domain
external_url 'https://gitlab.yourdomain.com'

# Save and reconfigure
sudo gitlab-ctl reconfigure

เชื่อมต่อ Git Local กับ GitLab Self-hosted ผ่าน SSH

# Generate SSH key (if not exists)
ssh-keygen -t rsa -b 4096

# Copy public key
cat ~/.ssh/id_rsa.pub

# Add to GitLab (Settings > SSH Keys)
# Then clone repository
git clone [email protected]:username/project.git

การเชื่อมต่อ Multiple Remotes (Backup ไป GitHub)

# Add GitHub as backup remote
git remote add github https://github.com/username/project.git

# Push to both
git push origin main
git push github main

คำสั่ง GitLab Admin ที่มีประโยชน์

# Check GitLab status
sudo gitlab-ctl status

# Restart GitLab
sudo gitlab-ctl restart

# View logs
sudo gitlab-ctl tail

# Backup
sudo gitlab-backup create

# Restore
sudo gitlab-backup restore

การตั้งค่า CI/CD Pipeline

สร้าง .gitlab-ci.yml ในรูท repository เพื่อ define CI/CD pipeline ตัวอย่าง:

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building application"

test_job:
  stage: test
  script:
    - echo "Running tests"

deploy_job:
  stage: deploy
  script:
    - echo "Deploying application"

ความปลอดภัยและการตั้งค่า HTTPS (SSL Certificate)

# Generate SSL certificate with Let's Encrypt
sudo apt-get install -y certbot

sudo certbot certonly --standalone -d gitlab.yourdomain.com

# Update GitLab config
sudo nano /etc/gitlab/gitlab.rb

# Set certificate paths
ssl_certificate "/etc/letsencrypt/live/gitlab.yourdomain.com/fullchain.pem"
ssl_certificate_key "/etc/letsencrypt/live/gitlab.yourdomain.com/privkey.pem"

# Reconfigure
sudo gitlab-ctl reconfigure

แก้ไขปัญหาทั่วไป (Troubleshooting)

  • 502 Bad Gateway – ตรวจสอบหน่วยความจำ รอสักครู่ หรือ restart GitLab
  • SSH connection refused – ตรวจสอบ SSH key และ permissions
  • High memory usage – เพิ่ม swap หรือ upgrade RAM
  • Slow performance – ตรวจสอบ disk I/O และ optimize database

Monitoring และ Maintenance

  • เรียกใช้ backup อย่างน้อยรายสัปดาห์
  • Monitor disk space และ memory
  • Update GitLab regularly
  • Check logs สำหรับ errors
  • Setup email notifications

สรุป

GitLab Self-hosted ให้คุณควบคุมเต็มที่ Git repository ของตัวเอง สามารถสร้าง private Git server พร้อม CI/CD ในหลาย VPS ได้อย่างง่าย ปฏิบัติตามขั้นตอนข้างต้นคุณจะมี fully functional Git platform สำหรับทีม