使用GitLab进行Linux项目的部署管理涉及多个步骤,包括环境准备、安装依赖、配置GitLab、设置CI/CD流水线、备份与恢复策略以及安全加固等。以下是一个详细的指南:
sudo hostnamectl set-hostname gitlab
sudo 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的官方文档进行详细配置。