温馨提示×

ubuntu gitlab如何实现自动化运维

小樊
31
2025-12-14 19:51:44
栏目: 智能运维

Ubuntu 上基于 GitLab 的自动化运维落地方案


一 架构与总体思路

  • GitLab CI/CD 为核心,配合 GitLab Runner 执行构建、测试、部署;在代码推送时自动触发流水线,完成从代码到环境的全流程自动化。
  • 在需要“推送到服务器即自动部署”的场景,补充 服务器端钩子(custom_hooks),实现裸仓库的自动更新(如重启服务、拉取最新代码)。
  • 对平台稳定性与可恢复性,使用 定时备份 + 远程复制 + 过期清理 + 邮件告警 的闭环方案,确保数据与配置的可靠性。

二 用 GitLab CI/CD 实现自动化构建与部署

  • 安装并注册 Runner(Ubuntu 示例)
    • 安装 Runner
      • curl -L --output gitlab-runner https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh
      • sudo bash script.deb.sh
      • sudo apt-get install -y gitlab-runner
    • 注册 Runner(在项目 Settings → CI/CD → Runners 获取 URLRegistration token
      • sudo gitlab-runner register --url --registration-token --executor shell --tag-list deploy,ubuntu --run-untagged true --locked false
  • 定义流水线 .gitlab-ci.yml(示例)
    • stages:
      • build
      • test
      • deploy
    • variables:
      • APP_DIR: /opt/myapp
    • build_job:
      • stage: build
      • script:
        • echo “Building…”
        • mvn package -DskipTests
      • artifacts:
        • paths:
          • target/*.jar
    • test_job:
      • stage: test
      • script:
        • echo “Running tests…”
        • mvn test
      • artifacts:
        • reports:
          • junit: target/surefire-reports/TEST-*.xml
    • deploy_job:
      • stage: deploy
      • tags:
        • deploy
        • ubuntu
      • script:
        • ssh -o StrictHostKeyChecking=no deploy@$DEPLOY_HOST “mkdir -p $APP_DIR/releases/$(date +%F_%H-%M-%S)”
        • scp target/*.jar deploy@$DEPLOY_HOST:$APP_DIR/releases/$(date +%F_%H-%M-%S)/app.jar
        • ssh deploy@$DEPLOY_HOST “ln -sfn $APP_DIR/releases/$(date +%F_%H-%M-%S) $APP_DIR/current && systemctl restart myapp || true”
      • environment: production
  • 要点
    • 使用 tags 精准调度到带标签的 Runner;通过 artifacts 传递构建产物;测试阶段输出 JUnit 报告 便于在 GitLab 查看质量趋势。

三 使用服务器端钩子实现推送到服务器即部署

  • 适用场景:希望“git push”后服务器自动拉取并重启服务,而不依赖外部 CI。
  • 配置步骤(Omnibus 安装路径示例)
    • 钩子目录结构
      • 全局钩子目录:/opt/gitlab/embedded/service/gitlab-shell/hooks
      • 项目裸仓库:/var/opt/gitlab/git-data/repositories//.git
      • 自定义钩子目录:/var/opt/gitlab/git-data/repositories//.git/custom_hooks(需与全局 hooks 目录为同一实体,通常通过软链实现)
    • 创建自定义钩子
      • sudo -u git mkdir -p /var/opt/gitlab/git-data/repositories//.git/custom_hooks
      • sudo -u git tee /var/opt/gitlab/git-data/repositories//.git/custom_hooks/post-receive <<‘EOF’ #!/usr/bin/env bash set -euo pipefail while read oldrev newrev ref; do if [[ “$ref” == “refs/heads/main” ]]; then echo “Auto-deploy main branch…” git --git-dir=/var/opt/gitlab/git-data/repositories//.git
        –work-tree=/var/www/myapp
        checkout -f main systemctl restart myapp || true fi done EOF
      • sudo chmod +x /var/opt/gitlab/git-data/repositories//.git/custom_hooks/post-receive
      • sudo chown -R git:git /var/opt/gitlab/git-data/repositories//.git/custom_hooks
  • 说明
    • 保持 GitLab 自带的全局钩子存在,custom_hooks 会被框架自动调用;修改 /etc/gitlab/gitlab.rb 后需执行 gitlab-ctl reconfigure 使配置生效。

四 自动化备份与恢复

  • 定时本地备份(crontab 示例)
    • 0 2 * * * root /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
  • 远程复制与过期清理(可拆分为两个定时任务)
    • 0 3 * * * root /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1 && scp /var/opt/gitlab/backups/*.tar :/backup/gitlab/
    • 0 4 * * * root find /backup/gitlab/ -name “*.tar” -mtime +30 -delete
  • 备份目录自定义(/etc/gitlab/gitlab.rb)
    • gitlab_rails[‘backup_path’] = “/data/gitlab/backups”
    • 修改后执行:sudo gitlab-ctl reconfigure
  • 恢复要点
    • 需保证 备份与恢复时的 GitLab 版本一致;恢复命令示例:sudo /opt/gitlab/bin/gitlab-rake gitlab:backup:restore BACKUP=timestamp_of_backup。

五 监控告警与质量度量

  • 平台内置能力
    • 利用 CI/CD Pipelines 查看构建成功率、耗时、部署频率;结合 错误跟踪日志 快速定位问题。
  • 第三方集成
    • 监控与可视化:集成 Prometheus + Grafana 收集与展示关键指标(如 Runner 负载、作业排队、HTTP 可用性等)。
    • 错误监控:接入 Sentry 捕获与聚合运行时异常,联动工单与告警。
  • 告警与报表
    • 配置 自动化告警规则定时报表,将构建失败、部署回滚、备份异常等事件及时通知到企业 IM/邮件。

0