Debian系统配置GitLab CI/CD完整流程
确保Debian系统为最新版本,并安装必要依赖包:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates postfix
安装Postfix时选择“Internet Site”,按提示配置邮件服务(用于GitLab通知)。
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
替换your-gitlab-domain.com为你的域名或IP地址:
sudo EXTERNAL_URL="http://your-gitlab-domain.com" apt install gitlab-ce
sudo gitlab-ctl reconfigure # 应用配置
sudo gitlab-ctl restart # 重启服务
访问http://your-gitlab-domain.com,完成管理员账号初始化(默认用户名root)。
GitLab Runner是执行CI/CD任务的代理,需在Debian上安装并注册到GitLab项目。
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner
获取GitLab项目的Runner注册令牌(路径:项目→Settings→CI/CD→Runners):
sudo gitlab-runner register
按提示输入:
http://your-gitlab-domain.com)debian-runner)linux,可选)Shell或Docker,根据需求选择)示例(Shell执行器):
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
> http://your-gitlab-domain.com
Please enter the gitlab-ci token for this runner:
> <粘贴注册令牌>
Please enter the gitlab-ci description for this runner:
> [runner-name]
Please enter the gitlab-ci tags for this runner (comma separated):
> linux
Registering runner... succeeded runner=<runner-ID>
Please enter the executor: shell, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
> shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
sudo systemctl enable gitlab-runner # 开机自启
sudo systemctl start gitlab-runner # 立即启动
验证Runner状态:
sudo gitlab-runner status
应显示Runner is running。
在项目根目录创建.gitlab-ci.yml文件,定义CI/CD流程(以构建→测试→部署为例):
stages:
- build # 构建阶段
- test # 测试阶段
- deploy # 部署阶段
# 构建作业
build_job:
stage: build
script:
- echo "Building project..."
- mkdir -p build
- cd build && touch status.txt # 模拟构建产物
artifacts: # 将构建产物传递给后续作业
paths:
- build/
# 测试作业(依赖build_job的产物)
test_job:
stage: test
script:
- echo "Running tests..."
- test -f build/status.txt && echo "Tests passed!" || echo "Tests failed!"
dependencies: # 依赖build_job的输出
- build_job
# 部署作业(手动触发,避免误操作)
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- scp -r build/* user@your-server:/path/to/deploy # 示例:SCP传输文件
when: manual # 手动触发
only:
- main # 仅main分支触发
若项目需要特定环境(如Node.js、Python),可使用Docker镜像:
image: node:18 # 使用Node.js 18镜像
stages:
- install
- test
- deploy
install_job:
stage: install
script:
- npm install # 安装依赖
test_job:
stage: test
script:
- npm test # 运行测试
deploy_job:
stage: deploy
script:
- echo "Deploying with Docker..."
only:
- master
若需启动Docker服务(如测试数据库),添加services:
services:
- postgres:15 # 启动PostgreSQL服务
variables:
POSTGRES_DB: test_db
POSTGRES_USER: test_user
POSTGRES_PASSWORD: test_pass
通过rules控制不同分支的CI行为:
stages:
- build
- deploy
build_job:
stage: build
script:
- echo "Building..."
rules:
- if: $CI_COMMIT_BRANCH == "main" # main分支触发
- if: $CI_COMMIT_BRANCH =~ /^feature\// # feature/*分支触发
deploy_staging_job:
stage: deploy
script:
- echo "Deploying to staging..."
rules:
- if: $CI_COMMIT_BRANCH == "develop" # develop分支部署到staging
deploy_prod_job:
stage: deploy
script:
- echo "Deploying to production..."
when: manual # 生产环境手动触发
rules:
- if: $CI_COMMIT_BRANCH == "main" # main分支手动部署
将.gitlab-ci.yml推送到GitLab仓库:
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin your-branch-name
build_job、test_job)。若部署作业设置为when: manual,可在Jobs页面点击作业右侧的Play按钮手动触发。
gitlab-runner status,若未运行,执行sudo systemctl start gitlab-runner。.gitlab-ci.yml文件位于项目根目录,且语法正确(可通过GitLab的CI Lint工具验证)。--privileged=true)。通过以上步骤,即可在Debian系统上完成GitLab CI/CD的配置,实现项目的自动化构建、测试和部署。根据项目需求,可进一步扩展配置(如添加缓存、集成第三方工具、配置多环境变量等)。