温馨提示×

ubuntu gitlab自动化运维实践

小樊
39
2025-12-26 14:43:12
栏目: 智能运维

Ubuntu 上基于 GitLab 的自动化运维实践

一 架构与总体方案

  • GitLab CI/CD 为核心,配合 GitLab Runner 执行构建、测试、部署;Runner 建议与 GitLab 实例分离部署,使用 Shell、Docker、SSH 等执行器;按作用域选择 Shared/Group/Specific Runner;流水线通过项目根目录的 .gitlab-ci.yml 定义,支持多阶段与产物、报告、缓存等能力。该模式适合在 Ubuntu 20.04/22.04/24.04 上落地标准化交付链路。

二 安装与注册 GitLab Runner

  • 安装 Runner(Ubuntu/Debian 示例):
    • 添加仓库并安装:
      • curl -L --output gitlab-runner https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh
      • sudo bash
      • sudo apt-get install gitlab-runner
  • 注册 Runner:
    • 在项目的 Settings → CI/CD → Runners 获取 URLRegistration token
    • 执行注册并配置标签与执行器(示例为 shell):
      • sudo gitlab-runner register --url --registration-token --executor shell --description “ubuntu-runner” --tag-list “build,deploy” --run-untagged true --locked false
  • 常用运维命令:
    • 启动/停止/状态:sudo gitlab-runner start|stop|status
    • 注册后可在 UI 中查看、启用/禁用、标签匹配与并发设置。

三 流水线示例与关键实践

  • 示例 .gitlab-ci.yml(多阶段 + 产物 + 报告 + 部署脚本)
    • stages:
      • build
      • test
      • deploy
    • variables:
      • APP_NAME: myapp
      • ARTIFACTS_DIR: target
    • build_job:
      • stage: build
      • script:
        • echo “Building $APP_NAME…”
        • ./mvnw package -DskipTests
      • artifacts:
        • paths:
          • $ARTIFACTS_DIR/*.jar
        • expire_in: 1 week
    • test_job:
      • stage: test
      • script:
        • echo “Running tests…”
        • ./mvnw test
      • artifacts:
        • reports:
          • junit: target/surefire-reports/TEST-*.xml
    • deploy_job:
      • stage: deploy
      • tags:
        • deploy
      • script:
        • ./scripts/deploy.sh $ARTIFACTS_DIR/*.jar
      • only:
        • main
  • 关键实践要点
    • 合理使用 tags 将构建与部署解耦;为生产部署设置 only/exceptrules 控制触发分支与环境。
    • 使用 artifacts 传递构建产物,使用 reports 输出测试报告(JUnit、覆盖率等),在 UI 中直观查看。
    • 将脚本(如部署脚本)纳入版本控制,保证可审计与可回滚;必要时使用 needs 优化阶段并行度。

四 备份与恢复自动化

  • 定时本地备份(Omnibus 包)
    • 编辑 /etc/gitlab/gitlab.rb 自定义备份目录(可选):gitlab_rails[‘backup_path’] = “/data/gitlab/backups”
    • 使配置生效:sudo gitlab-ctl reconfigure
    • 执行备份(CRON=1 抑制进度条):sudo /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
  • 远程复制与清理(示例思路)
    • 在 /etc/crontab 中编排多步任务:例如每日 02:00 本地备份、03:00 将最新包 SCP 到备份服务器、04:00 在远端清理 30 天前 的备份。
    • 建议增加日志与 邮件通知,便于失败告警与审计(可封装为 Shell 脚本统一执行与汇报)。
  • 恢复要点
    • 恢复时通常需要停止相关服务,执行:sudo /opt/gitlab/bin/gitlab-rake gitlab:backup:restore BACKUP=;恢复完成后启动服务。
    • 备份与恢复两端的 GitLab 版本需保持一致,避免不兼容问题。

五 服务器端钩子与 Ansible 自动化

  • 服务器端自定义钩子(custom_hooks)
    • 适用场景:push 后触发服务器端动作(如自动部署到测试环境)。
    • 路径与权限(Omnibus 常见路径):/var/opt/gitlab/git-data/repositories//.git/custom_hooks/,钩子脚本需可执行且属主为 git:git
    • 放置如 post-receive 脚本,注意保留上游调用链,避免覆盖上游脚本导致自定义钩子不被调用;修改后建议重启相关服务或按官方流程重载。
  • 配置与扩缩容自动化(Ansible)
    • 使用 Ansible 批量部署 GitLab Runner、分发 SSH 密钥、分发与注册 Runner、推送 .gitlab-ci.yml 模板、管理 crontab 备份任务等,形成 IaC 与标准化交付。
    • 示例 Playbook 片段(安装 Runner 与注册):
        • name: Install GitLab Runner
        • apt: name=gitlab-runner state=present
        • name: Register Runner
        • command: > gitlab-runner register --non-interactive –url {{ gitlab_url }} --registration-token {{ runner_token }} –executor {{ executor }} --tag-list “{{ runner_tags }}” –description “{{ runner_desc }}” --run-untagged {{ run_untagged }} --locked {{ locked }}
    • 结合 Docker 作为 Runner 执行器,可在 CI 中快速构建与测试多语言环境,减少环境漂移。

0