温馨提示×

如何使用GitLab进行Linux项目的部署管理

小樊
44
2025-08-05 13:38:07
栏目: 智能运维

使用GitLab进行Linux项目的部署管理涉及多个步骤,包括环境准备、安装依赖、配置GitLab、设置CI/CD流水线、备份与恢复策略以及安全加固等。以下是一个详细的指南:

环境准备

  • 主机名更改sudo hostnamectl set-hostname gitlab
  • 防火墙配置
    • 对于Ubuntu:
      sudo ufw allow 80/tcp
      sudo ufw allow 443/tcp
      sudo ufw enable
      
    • 对于CentOS:
      sudo firewall-cmd --permanent --add-service=https
      sudo firewall-cmd --permanent --add-service=http
      sudo systemctl reload firewalld
      
  • 关闭SELinux
    sudo sed -i 's/selinuxenforcing/selinuxdisabled/g' /etc/sysconfig/selinux
    sudo setenforce 0
    
  • 设置EPEL源、安装基本操作命令
    • 对于Ubuntu:
      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
      
    • 对于CentOS:
      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
    
  • 添加GitLab软件包仓库
    • 对于Ubuntu:
      curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
      
    • 对于CentOS:
      curl -fsSL https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
      
  • 安装GitLab
    sudo apt-get update
    sudo apt-get install -y gitlab-ce
    
  • 配置并启动GitLab
    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl restart
    
  • 配置GitLab
    • 打开浏览器,输入你的服务器IP地址或域名进行访问,初始账户为 root,密码为 5iveL!fe,首次登录后会提示修改密码。
    • 修改管理员密码:在个人账户页面中修改密码。
    • 配置SMTP服务:在员工设置中选择邮件通知,然后输入SMTP服务器相关信息。
    • 使用自签名SSL证书:在浏览器中访问信任的HTTPS网站时,需要使用SSL证书。GitLab在默认情况下使用自签名证书,用于建立HTTPS连接。

配置CI/CD流水线

  • 创建和配置 .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"
    
  • 配置自动化测试和部署:可以使用各种工具来实现自动化测试和部署,例如Maven、Gradle、npm、Docker等。

备份与恢复策略

  • 自动备份
    • 配置定时任务:
      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
      

安全加固建议

  • SSH访问控制:限制SSH登录用户的IP范围。使用SSH密钥认证,禁用密码登录。
  • 数据库加密:启用PostgreSQL的TLS加密。定期更换数据库密码。
  • Runner安全:使用Parallels执行器(虚拟机隔离)。避免使用 --privileged 标记运行容器。
  • 审计日志:启用GitLab的审计日志功能,记录所有操作行为。

以上步骤是在Linux系统上部署GitLab的基本流程,具体操作可能会因Linux发行版和具体需求有所不同。在部署前,请确保你的服务器满足GitLab的系统要求,并参考GitLab的官方文档进行详细配置。

0