在Debian上配置GitLab CI/CD流程的完整步骤
1. 准备基础环境
确保Debian系统已更新至最新版本,并安装GitLab所需的依赖组件:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates postfix
安装Postfix时,选择“Internet Site”类型,填写系统域名(如example.com)以启用邮件通知。
2. 安装GitLab Community Edition(CE)
添加GitLab官方APT仓库并安装GitLab CE:
# 添加GitLab仓库
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
# 安装GitLab CE
sudo apt install -y gitlab-ce
# 配置GitLab外部访问地址(替换为你的域名/IP)
sudo vim /etc/gitlab/gitlab.rb
# 修改以下行(取消注释并替换值)
external_url 'http://your-gitlab-domain-or-ip'
# 重新配置并启动GitLab
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
等待几分钟,通过浏览器访问http://your-gitlab-domain-or-ip,使用初始管理员账号(root)登录。
3. 安装GitLab Runner(CI/CD任务执行器)
GitLab Runner是执行.gitlab-ci.yml中任务的守护进程,需单独安装:
# 添加GitLab Runner仓库
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
# 安装GitLab Runner
sudo apt install -y gitlab-runner
# 注册Runner(替换为你的GitLab实例URL和注册Token)
sudo gitlab-runner register
注册时需填写以下信息:
- GitLab instance URL:
http://your-gitlab-domain-or-ip - Registration Token: 在GitLab项目页面→
Settings→CI/CD→Runners中获取 - Executor选择: 推荐
docker(隔离环境)或shell(直接运行命令) - Description: 自定义Runner名称(如
debian-runner) - Tag List: 添加标签(如
ci),用于限制任务匹配
4. 编写.gitlab-ci.yml配置文件
在项目根目录创建.gitlab-ci.yml,定义CI/CD流程的核心逻辑(以Java项目为例):
# 定义流程阶段(按顺序执行)
stages:
- build
- test
- deploy
# 全局变量(可选)
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
# 缓存依赖(加速后续构建)
cache:
paths:
- .m2/repository/
# 构建阶段
build:
stage: build
image: maven:3.8.7-openjdk-11 # 使用Maven镜像
script:
- echo "Building project..."
- mvn clean package -DskipTests
artifacts:
paths:
- target/*.jar # 保存构建产物(供后续阶段使用)
expire_in: 1 hour # 产物有效期
# 测试阶段
test:
stage: test
image: maven:3.8.7-openjdk-11
script:
- echo "Running unit tests..."
- mvn test
artifacts:
when: always # 无论测试是否通过都保存结果
paths:
- target/surefire-reports/*.xml # 保存测试报告
# 部署阶段(仅master分支触发)
deploy:
stage: deploy
image: alpine:latest # 使用轻量级镜像
script:
- echo "Deploying to production..."
- apk add --no-cache openssh
- ssh -o StrictHostKeyChecking=no user@your-server "mkdir -p /opt/app && scp target/*.jar user@your-server:/opt/app/app.jar && ssh user@your-server 'systemctl restart app.service'"
only:
- master # 限制仅在master分支推送时触发
说明:
stages: 定义流程顺序(build→test→deploy);image: 指定任务运行的Docker镜像(需提前拉取);script: 任务执行的具体命令;artifacts: 传递产物(如构建结果、测试报告)给后续阶段;only: 限制任务触发的分支(如仅master分支部署)。
5. 触发CI/CD流水线
将.gitlab-ci.yml文件提交到GitLab仓库,流水线将自动触发:
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin master
触发后,可在GitLab项目页面→CI/CD→Pipelines查看流水线状态(运行中/成功/失败),点击任务可查看详细日志。
6. 高级配置(可选)
6.1 配置SSH访问(用于部署)
若部署阶段需要SSH连接远程服务器,需将私钥添加为GitLab CI/CD变量:
# 生成SSH密钥(若未生成)
ssh-keygen -t rsa -b 4096 -C "gitlab-ci@example.com"
# 复制公钥到远程服务器
cat ~/.ssh/id_rsa.pub | ssh user@your-server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# 将私钥添加为GitLab变量
# 在项目页面→`Settings`→`CI/CD`→`Variables`中添加:
# Key: SSH_PRIVATE_KEY
# Value: (粘贴私钥内容)
# Type: File(可选,用于直接写入文件)
更新.gitlab-ci.yml中的before_script,自动加载SSH密钥:
before_script:
- apk add --no-cache openssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan your-server >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
6.2 使用Docker镜像优化
通过image字段指定任务运行的镜像,避免重复安装工具(如Maven、Node.js):
build:
stage: build
image: node:18-alpine # 使用Node.js镜像
script:
- npm install
- npm run build
推荐使用官方镜像(如maven:3.8.7-openjdk-11、node:18-alpine),减少镜像大小和依赖问题。
6.3 条件执行任务
通过only/except或rules限制任务触发条件(如仅在特定分支、标签或MR时运行):
deploy_staging:
stage: deploy
script:
- echo "Deploying to staging..."
only:
- staging # 仅在staging分支触发
deploy_production:
stage: deploy
script:
- echo "Deploying to production..."
only:
- tags # 仅打标签时触发(如v1.0.0)
或使用rules实现更复杂的逻辑(如合并请求时运行测试):
test_on_mr:
stage: test
script:
- echo "Running tests for MR..."
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"' # 仅在MR时运行
when: always
以上就是关于“如何在Debian上配置GitLab的CI/CD流程”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm