使用GitLab進行Linux項目的部署管理涉及多個步驟,包括環境準備、安裝依賴、配置GitLab、設置CI/CD流水線、備份與恢復策略以及安全加固等。以下是一個詳細的指南:
sudo hostnamectl set-hostname gitlabsudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld
sudo sed -i 's/selinuxenforcing/selinuxdisabled/g' /etc/sysconfig/selinux
sudo setenforce 0
sudo apt-get update
sudo apt-get install -y wget
wget -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sudo apt-get install -y net-tools vim lrzsz tree screen lsof tcpdump ntpdate
sudo yum install -y wget
wget -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sudo yum install -y net-tools vim lrzsz tree screen lsof tcpdump ntpdate
sudo cp /usr/share/zoneinfo/asia/shanghai /etc/localtime
echo "*/5 * * * * ntpdate time1.aliyun.com & /dev/null && hwclock -w" /var/spool/cron/roots
sudo systemctl restart crond
sudo apt-get install -y curl policycoreutils-python openssh-server
curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo apt-get update
sudo apt-get install -y gitlab-ce
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
root,密碼為 5iveL!fe,首次登錄后會提示修改密碼。.gitlab-ci.yml 文件:stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the project..."
- npm install
- npm run build
test_job:
stage: test
script:
- echo "Running tests..."
- npm test
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- ssh user@server "cd /var/www/html && git pull"
sudo crontab -e
# 添加以下行(每天凌晨 2 點備份)
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
/var/opt/gitlab/backups,文件名格式為 TIMESTAMP_gitlab_backup.tar。sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
sudo gitlab-rake gitlab:backup:restore BACKUP=1696156800_2025_08_03_15.0.0
sudo gitlab-ctl start
--privileged 標記運行容器。以上步驟是在Linux系統上部署GitLab的基本流程,具體操作可能會因Linux發行版和具體需求有所不同。在部署前,請確保你的服務器滿足GitLab的系統要求,并參考GitLab的官方文檔進行詳細配置。