2026/4/16 16:25:08
网站建设
项目流程
网站呢建设,wordpress 微信plugin,建设公司官网介绍,mooc 网站建设情况本文详解如何自建GitLab代码仓库#xff0c;配置CI/CD自动化流水线#xff0c;打造完整的团队协作开发环境。前言
代码托管平台的选择#xff1a;
GitHub#xff1a;开源首选#xff0c;但私有仓库有限制Gitee#xff1a;国内快#xff0c;但有审查自建GitLab#xff1…本文详解如何自建GitLab代码仓库配置CI/CD自动化流水线打造完整的团队协作开发环境。前言代码托管平台的选择GitHub开源首选但私有仓库有限制Gitee国内快但有审查自建GitLab完全自主功能强大对于企业或有隐私需求的团队自建GitLab是最好的选择代码完全在自己手里无限私有仓库内置CI/CD功能比GitHub还全今天来搭建一套完整的GitLab CI/CD环境。一、GitLab部署1.1 服务器要求配置最低推荐CPU2核4核内存4GB8GB磁盘50GB100GB SSD注意GitLab比较吃资源4GB内存勉强能跑8GB以上才流畅。1.2 Docker Compose部署# docker-compose.ymlversion:3.8services:gitlab:image:gitlab/gitlab-ce:latestcontainer_name:gitlabrestart:alwayshostname:gitlab.example.comenvironment:GITLAB_OMNIBUS_CONFIG:|external_url http://gitlab.example.com gitlab_rails[gitlab_shell_ssh_port] 2222 # 邮件配置可选 gitlab_rails[smtp_enable] true gitlab_rails[smtp_address] smtp.example.com gitlab_rails[smtp_port] 465 gitlab_rails[smtp_user_name] gitlabexample.com gitlab_rails[smtp_password] password gitlab_rails[smtp_domain] example.com gitlab_rails[smtp_authentication] login gitlab_rails[smtp_enable_starttls_auto] true gitlab_rails[smtp_tls] true gitlab_rails[gitlab_email_from] gitlabexample.comports:-80:80-443:443-2222:22volumes:-./gitlab/config:/etc/gitlab-./gitlab/logs:/var/log/gitlab-./gitlab/data:/var/opt/gitlabshm_size:256m# 启动docker compose up -d# 等待启动完成第一次需要几分钟docker logs -f gitlab# 获取初始root密码dockerexec-it gitlabgrepPassword:/etc/gitlab/initial_root_password1.3 初始配置1. 访问 http://服务器IP 2. 用户名root 3. 密码上一步获取的初始密码 4. 登录后修改密码 建议配置 - Admin Area → Settings → Sign-up restrictions → 关闭注册 - Admin Area → Settings → Visibility → 设置默认私有1.4 创建用户和项目1. Admin Area → Users → New User 2. 创建项目组Groups → New Group 3. 创建项目Projects → New Project 4. 添加SSH KeyUser Settings → SSH Keys二、GitLab Runner配置2.1 Runner是什么Runner是执行CI/CD任务的执行器可以部署在GitLab服务器本机单独的服务器推荐开发者本地2.2 安装Runner# Docker方式安装docker run -d --name gitlab-runner --restart always\-v /var/run/docker.sock:/var/run/docker.sock\-v gitlab-runner-config:/etc/gitlab-runner\gitlab/gitlab-runner:latest2.3 注册Runner# 获取注册Token# GitLab → Admin Area → Runners → 复制Token# 注册Runnerdockerexec-it gitlab-runner gitlab-runner registerEnter the GitLab instance URL: http://gitlab.example.com Enter the registration token: [粘贴Token] Enter a description: my-runner Enter tags: docker,build Enter an executor: docker Enter the default Docker image: docker:latest2.4 Runner配置优化# 编辑配置dockerexec-it gitlab-runnernano/etc/gitlab-runner/config.toml[[runners]] name my-runner url http://gitlab.example.com token xxx executor docker [runners.docker] tls_verify false image docker:latest privileged true # 允许Docker in Docker disable_cache false volumes [/cache, /var/run/docker.sock:/var/run/docker.sock]三、CI/CD流水线配置3.1 基本概念# .gitlab-ci.yml 结构stages:# 阶段定义-build-test-deployvariables:# 全局变量APP_NAME:my-appjob_name:# 任务定义stage:build# 所属阶段script:# 执行脚本-echo Building...only:# 触发条件-main3.2 Java项目示例# .gitlab-ci.ymlstages:-build-test-package-deployvariables:MAVEN_OPTS:-Dmaven.repo.local.m2/repositorycache:paths:-.m2/repository/build:stage:buildimage:maven:3.8-jdk-11script:-mvn compileonly:-main-developtest:stage:testimage:maven:3.8-jdk-11script:-mvn testonly:-main-developpackage:stage:packageimage:maven:3.8-jdk-11script:-mvn package-DskipTestsartifacts:paths:-target/*.jarexpire_in:1 weekonly:-maindeploy:stage:deployimage:docker:latestservices:-docker:dindscript:-docker build-t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .-docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHAonly:-main3.3 Node.js项目示例# .gitlab-ci.ymlstages:-install-test-build-deploycache:paths:-node_modules/install:stage:installimage:node:18script:-npm ciartifacts:paths:-node_modules/expire_in:1 hourtest:stage:testimage:node:18script:-npm run testdependencies:-installbuild:stage:buildimage:node:18script:-npm run buildartifacts:paths:-dist/expire_in:1 weekdependencies:-installonly:-maindeploy:stage:deployimage:alpine:latestscript:-apk add rsync openssh-client-rsync-avz--delete dist/ userserver:/var/www/app/only:-mainwhen:manual# 手动触发3.4 Docker镜像构建# .gitlab-ci.ymlbuild-image:stage:packageimage:docker:latestservices:-docker:dindvariables:DOCKER_TLS_CERTDIR:before_script:-docker login-u $CI_REGISTRY_USER-p $CI_REGISTRY_PASSWORD $CI_REGISTRYscript:-docker build-t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .-docker build-t $CI_REGISTRY_IMAGE:latest .-docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-docker push $CI_REGISTRY_IMAGE:latestonly:-main四、分支策略与代码审查4.1 Git Flow分支模型main ──●───────────●───────────●──→ 生产环境 ↑ ↑ ↑ release ──●───────────●───────────●──→ 预发布 ↑ ↑ develop ──●───●───●───●───●───●───●──→ 开发主线 ↑ ↑ ↑ feature ──●───● ●───● ●───●───→ 功能分支4.2 分支保护Settings → Repository → Protected Branches main分支 - Allowed to merge: Maintainers - Allowed to push: No one - Require approval: 1人以上4.3 Merge Request模板!-- .gitlab/merge_request_templates/default.md -- ## 变更描述 !-- 描述这个MR做了什么 -- ## 变更类型 - [ ] 新功能 - [ ] Bug修复 - [ ] 重构 - [ ] 文档更新 ## 测试说明 !-- 如何测试这个变更 -- ## Checklist - [ ] 代码已自测 - [ ] 更新了相关文档 - [ ] 添加了必要的测试五、团队远程协作5.1 场景挑战团队分布 - 开发A公司内网 - 开发B家里远程 - 开发C出差酒店 - GitLab公司内网服务器 问题开发B、C无法访问公司内网的GitLab5.2 解决方案方案1GitLab暴露公网❌ 风险高容易被攻击 ❌ 需要公网IP和域名方案2VPN接入⚠️ 需要VPN服务器 ⚠️ 配置复杂方案3组网软件推荐使用组网软件如星空组网将团队成员和GitLab服务器组成虚拟局域网┌──────────────┐ │ GitLab │ │ 10.10.0.1 │←─┐ └──────────────┘ │ │ 虚拟局域网 ┌──────────────┐ │ │ 开发A │←─┤ │ 10.10.0.2 │ │ └──────────────┘ │ │ ┌──────────────┐ │ │ 开发B │←─┤ │ 10.10.0.3 │ │ └──────────────┘ │ │ ┌──────────────┐ │ │ 开发C │←─┘ │ 10.10.0.4 │ └──────────────┘配置步骤# 1. GitLab服务器安装组网客户端# 2. 所有开发者电脑安装组网客户端# 3. 登录同一账号# 4. 修改git remote地址为组网IP# 原地址gitremote set-url origin http://192.168.1.100/group/project.git# 改为组网IPgitremote set-url origin http://10.10.0.1/group/project.git效果任何地点都能访问GitLab不需要公网暴露加密传输安全配置简单一次设置5.3 SSH配置# ~/.ssh/configHost gitlab HostName10.10.0.1# 组网IPPort2222UsergitIdentityFile ~/.ssh/gitlab_key# 克隆使用gitclone gitlab:group/project.git六、备份与恢复6.1 备份配置# 手动备份dockerexec-t gitlab gitlab-backup create# 备份文件位置./gitlab/data/backups/# 定时备份crontab03* * * dockerexec-t gitlab gitlab-backup createCRON16.2 恢复# 停止相关服务dockerexec-it gitlab gitlab-ctl stop puma dockerexec-it gitlab gitlab-ctl stop sidekiq# 恢复备份BACKUP为备份时间戳dockerexec-it gitlab gitlab-backup restoreBACKUP11493107454_2018_04_25_10.6.4-ce# 重启docker restart gitlab6.3 异地备份# 备份到远程服务器通过组网rsync-avz ./gitlab/data/backups/ user10.10.0.5:/backup/gitlab/七、常见问题7.1 内存不足# 减少Sidekiq并发gitlab_rails[sidekiq_concurrency]5# 减少Puma workerspuma[worker_processes]2# 禁用Prometheus省内存prometheus_monitoring[enable]false7.2 克隆速度慢# 启用Git LFS存储大文件gitlfsinstallgitlfs track*.psdgitlfs track*.zip7.3 502错误# 通常是启动未完成或内存不足# 查看日志docker logs gitlab# 重启docker restart gitlab八、总结GitLab CI/CD搭建要点服务器配置至少8GB内存Docker部署最简单的部署方式Runner配置单独部署避免资源竞争CI/CD流水线按项目类型配置分支保护main分支必须通过MR远程协作组网软件打通网络定期备份数据无价我的团队配置- GitLab服务器公司内网8GB内存 - Runner单独一台4GB服务器 - 远程访问星空组网 - 备份每天自动备份rsync到另一台机器自建GitLab虽然需要维护但完全掌控数据的感觉真的很好。参考资料GitLab官方文档https://docs.gitlab.com/GitLab CI/CD文档https://docs.gitlab.com/ee/ci/GitLab Docker安装https://docs.gitlab.com/ee/install/docker.html建议先在测试服务器上搭建熟悉流程后再部署到生产环境。数据迁移GitLab有完善的备份恢复机制。