Gitee 持续集成与交付(CI/CD)篇 🚀
文章目录
嘿,宝子们!今天咱们来聊聊 Gitee 的持续集成与交付(CI/CD)!这可是现代软件开发的核心技能之一,掌握了它,你的开发效率绝对能起飞!✈️
🎯 什么是 CI/CD?
持续集成(Continuous Integration,CI):开发者频繁地将代码集成到主干分支,每次集成都通过自动化构建来验证,从而尽早发现集成错误。
持续交付(Continuous Delivery,CD):在持续集成的基础上,将集成后的代码自动部署到类生产环境,确保代码随时可以安全地发布到生产环境。
持续部署(Continuous Deployment):更进一步,将通过测试的代码自动部署到生产环境。
🌟 Gitee Go 介绍
Gitee Go 是 Gitee 推出的持续集成服务,为开发者提供了完整的 CI/CD 解决方案。它具有以下特点:
✨ 核心特性
- 云原生架构:基于容器技术,支持多种运行环境
- 丰富的模板:提供多种语言和框架的构建模板
- 灵活配置:支持 YAML 配置文件,自定义构建流程
- 集成度高:与 Gitee 代码仓库无缝集成
- 成本优化:按需使用,降低运维成本
🎨 支持的技术栈
- 前端:Vue.js、React、Angular、小程序等
- 后端:Java、Python、Node.js、Go、PHP 等
- 移动端:Android、iOS、Flutter、React Native 等
- 其他:Docker、Kubernetes、静态网站等
🚀 提交项目进行 CI/CD
第一步:创建 .gitee-ci.yml 文件
在项目根目录创建 .gitee-ci.yml
文件,这是 Gitee Go 的配置文件:
# Gitee Go CI/CD 配置文件
image: node:16 # 指定运行环境
stages:
- build
- test
- deploy
variables:
NODE_ENV: production
# 构建阶段
build_job:
stage: build
script:
- npm install
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 hour
# 测试阶段
test_job:
stage: test
script:
- npm run test
- npm run lint
coverage: '/Lines\s*:\s*(\d+\.?\d*)%/'
# 部署阶段
deploy_job:
stage: deploy
script:
- echo "部署到生产环境"
- scp -r dist/* user@server:/var/www/html/
only:
- master
第二步:配置项目设置
进入项目设置
- 打开 Gitee 项目页面
- 点击「服务」→「Gitee Go」
- 启用 CI/CD 服务
配置触发条件
# 触发条件配置 only: - master # 仅在 master 分支触发 - develop # 开发分支 except: - feature/* # 排除功能分支 # 或者使用规则 rules: - if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
设置环境变量
- 在项目设置中添加敏感信息
- 如数据库密码、API 密钥等
- 在 CI/CD 中通过
$VARIABLE_NAME
使用
第三步:提交代码触发构建
# 提交代码
git add .
git commit -m "feat: 添加 CI/CD 配置"
git push origin master
提交后,Gitee Go 会自动检测到 .gitee-ci.yml
文件并开始执行构建流程!🎉
📦 制品库配置
制品库用于存储构建产物,如 JAR 包、Docker 镜像、npm 包等。
Maven 制品库配置
创建制品库
- 进入 Gitee 企业版
- 创建 Maven 类型制品库
- 获取仓库地址和认证信息
配置 pom.xml
<distributionManagement> <repository> <id>gitee-releases</id> <name>Gitee Release Repository</name> <url>https://gitee.com/api/packages/your-org/maven</url> </repository> <snapshotRepository> <id>gitee-snapshots</id> <name>Gitee Snapshot Repository</name> <url>https://gitee.com/api/packages/your-org/maven</url> </snapshotRepository> </distributionManagement>
CI/CD 中发布制品
publish_job: stage: deploy script: - mvn clean compile package - mvn deploy -s settings.xml artifacts: paths: - target/*.jar
Docker 制品库配置
docker_build:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
services:
- docker:dind
npm 制品库配置
npm_publish:
stage: deploy
script:
- npm config set registry https://gitee.com/api/packages/your-org/npm/
- npm publish
only:
- tags
⚙️ 流水线配置示例与实践
Java Spring Boot 项目示例
# Java Spring Boot CI/CD 配置
image: maven:3.8.1-openjdk-11
stages:
- build
- test
- package
- deploy
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version"
cache:
paths:
- .m2/repository/
# 构建阶段
build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS compile
artifacts:
paths:
- target/
expire_in: 1 hour
# 单元测试
unit_test:
stage: test
script:
- mvn $MAVEN_CLI_OPTS test
artifacts:
reports:
junit:
- target/surefire-reports/TEST-*.xml
paths:
- target/site/jacoco/
coverage: '/Total.*?([0-9]{1,3})%/'
# 代码质量检查
code_quality:
stage: test
script:
- mvn sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_TOKEN
only:
- master
- develop
# 打包
package:
stage: package
script:
- mvn $MAVEN_CLI_OPTS package -DskipTests
artifacts:
paths:
- target/*.jar
expire_in: 1 week
only:
- master
- tags
# Docker 镜像构建
docker_build:
stage: package
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
- docker push $CI_REGISTRY_IMAGE:latest
services:
- docker:dind
only:
- master
# 部署到测试环境
deploy_test:
stage: deploy
script:
- echo "部署到测试环境"
- ssh test-server "docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
- ssh test-server "docker stop myapp || true"
- ssh test-server "docker run -d --name myapp -p 8080:8080 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
environment:
name: test
url: http://test.example.com
only:
- develop
# 部署到生产环境
deploy_prod:
stage: deploy
script:
- echo "部署到生产环境"
- ssh prod-server "docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
- ssh prod-server "docker stop myapp || true"
- ssh prod-server "docker run -d --name myapp -p 8080:8080 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
environment:
name: production
url: http://prod.example.com
when: manual # 手动触发
only:
- master
Vue.js 前端项目示例
# Vue.js 前端 CI/CD 配置
image: node:16
stages:
- install
- lint
- test
- build
- deploy
variables:
NODE_ENV: production
cache:
paths:
- node_modules/
- .npm/
# 安装依赖
install_dependencies:
stage: install
script:
- npm ci --cache .npm --prefer-offline
artifacts:
paths:
- node_modules/
expire_in: 1 hour
# 代码检查
lint_code:
stage: lint
script:
- npm run lint
- npm run format:check
dependencies:
- install_dependencies
# 单元测试
unit_test:
stage: test
script:
- npm run test:unit -- --coverage
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml
paths:
- coverage/
dependencies:
- install_dependencies
# E2E 测试
e2e_test:
stage: test
script:
- npm run test:e2e
artifacts:
paths:
- tests/e2e/screenshots/
- tests/e2e/videos/
when: on_failure
dependencies:
- install_dependencies
# 构建
build_app:
stage: build
script:
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 week
dependencies:
- install_dependencies
only:
- master
- develop
# 部署到 CDN
deploy_cdn:
stage: deploy
script:
- echo "部署到 CDN"
- aws s3 sync dist/ s3://my-bucket/ --delete
- aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_ID --paths "/*"
dependencies:
- build_app
environment:
name: production
url: https://myapp.example.com
only:
- master
# 部署到测试环境
deploy_staging:
stage: deploy
script:
- echo "部署到测试环境"
- rsync -avz --delete dist/ user@staging-server:/var/www/html/
dependencies:
- build_app
environment:
name: staging
url: https://staging.myapp.example.com
only:
- develop
Python Django 项目示例
# Python Django CI/CD 配置
image: python:3.9
stages:
- test
- build
- deploy
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
DJANGO_SETTINGS_MODULE: "myproject.settings.test"
cache:
paths:
- .cache/pip/
- venv/
before_script:
- python -V
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
- pip install -r requirements.txt
# 代码质量检查
flake8:
stage: test
script:
- flake8 .
# 安全检查
safety:
stage: test
script:
- safety check
# 单元测试
django_test:
stage: test
script:
- python manage.py test
- coverage run --source='.' manage.py test
- coverage report
- coverage xml
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
services:
- postgres:13
variables:
POSTGRES_DB: test_db
POSTGRES_USER: test_user
POSTGRES_PASSWORD: test_pass
DATABASE_URL: "postgresql://test_user:test_pass@postgres:5432/test_db"
# Docker 构建
docker_build:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
services:
- docker:dind
only:
- master
# 部署
deploy_production:
stage: deploy
script:
- echo "部署到生产环境"
- ssh production-server "docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
- ssh production-server "docker-compose down"
- ssh production-server "docker-compose up -d"
environment:
name: production
url: https://myapp.example.com
when: manual
only:
- master
🔧 高级配置技巧
1. 多环境部署策略
# 多环境部署配置
.deploy_template: &deploy_template
script:
- echo "部署到 $ENVIRONMENT 环境"
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
- docker-compose -f docker-compose.$ENVIRONMENT.yml down
- docker-compose -f docker-compose.$ENVIRONMENT.yml up -d
deploy_dev:
<<: *deploy_template
stage: deploy
variables:
ENVIRONMENT: dev
environment:
name: development
url: https://dev.myapp.com
only:
- develop
deploy_staging:
<<: *deploy_template
stage: deploy
variables:
ENVIRONMENT: staging
environment:
name: staging
url: https://staging.myapp.com
only:
- master
deploy_prod:
<<: *deploy_template
stage: deploy
variables:
ENVIRONMENT: prod
environment:
name: production
url: https://myapp.com
when: manual
only:
- tags
2. 条件执行和规则
# 条件执行示例
build_feature:
stage: build
script:
- echo "构建功能分支"
rules:
- if: '$CI_COMMIT_BRANCH =~ /^feature\/.*$/'
when: on_success
- when: never
build_hotfix:
stage: build
script:
- echo "构建热修复分支"
rules:
- if: '$CI_COMMIT_BRANCH =~ /^hotfix\/.*$/'
when: on_success
- when: never
# 基于文件变更的条件执行
test_frontend:
stage: test
script:
- npm run test
rules:
- changes:
- "frontend/**/*"
- "package.json"
when: on_success
- when: never
test_backend:
stage: test
script:
- mvn test
rules:
- changes:
- "backend/**/*"
- "pom.xml"
when: on_success
- when: never
3. 并行执行和依赖管理
# 并行执行示例
stages:
- build
- test
- integration
- deploy
# 并行构建
build_frontend:
stage: build
script:
- npm run build
artifacts:
paths:
- frontend/dist/
build_backend:
stage: build
script:
- mvn package
artifacts:
paths:
- backend/target/
# 并行测试
test_unit:
stage: test
script:
- npm run test:unit
parallel: 3 # 并行运行 3 个实例
test_integration:
stage: test
script:
- npm run test:integration
dependencies:
- build_frontend
- build_backend
# 集成测试
integration_test:
stage: integration
script:
- docker-compose up -d
- npm run test:e2e
- docker-compose down
dependencies:
- test_unit
- test_integration
4. 缓存优化策略
# 缓存优化配置
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
NPM_CONFIG_CACHE: "$CI_PROJECT_DIR/.cache/npm"
MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.cache/maven"
# 全局缓存
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- .cache/
- node_modules/
- .m2/repository/
policy: pull-push
# 作业级缓存
build_job:
stage: build
cache:
key: "build-$CI_COMMIT_REF_SLUG"
paths:
- dist/
- target/
policy: push
script:
- npm install
- npm run build
test_job:
stage: test
cache:
key: "build-$CI_COMMIT_REF_SLUG"
paths:
- dist/
- target/
policy: pull
script:
- npm run test
🛠️ 常见问题与解决方案
1. 构建超时问题
# 设置超时时间
build_job:
stage: build
timeout: 2h # 设置 2 小时超时
script:
- echo "长时间构建任务"
- sleep 3600 # 模拟长时间任务
2. 内存不足问题
# 优化内存使用
build_job:
stage: build
variables:
NODE_OPTIONS: "--max-old-space-size=4096" # 增加 Node.js 内存限制
MAVEN_OPTS: "-Xmx2g -Xms1g" # 设置 Maven 内存
script:
- npm run build
3. 网络连接问题
# 网络重试配置
before_script:
- apt-get update -qq
- apt-get install -y -qq curl
# 设置重试机制
- |
for i in {1..3}; do
npm install && break
echo "重试第 $i 次..."
sleep 5
done
4. 权限问题
# 权限配置
before_script:
- chmod +x scripts/deploy.sh
- chown -R $(whoami) /var/www/html
deploy_job:
stage: deploy
script:
- ./scripts/deploy.sh
📊 监控与通知
1. 构建状态通知
# 通知配置
after_script:
- |
if [ "$CI_JOB_STATUS" == "success" ]; then
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"✅ 构建成功: '$CI_PROJECT_NAME' - '$CI_COMMIT_REF_NAME'"}' \
$SLACK_WEBHOOK_URL
else
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"❌ 构建失败: '$CI_PROJECT_NAME' - '$CI_COMMIT_REF_NAME'"}' \
$SLACK_WEBHOOK_URL
fi
2. 性能监控
# 性能监控
performance_test:
stage: test
script:
- npm run build
- lighthouse --chrome-flags="--headless" --output=json --output-path=./lighthouse.json https://myapp.com
artifacts:
reports:
performance: lighthouse.json
🎯 最佳实践总结
1. 配置文件组织
- 使用模板和继承减少重复代码
- 将敏感信息存储在环境变量中
- 合理使用缓存提高构建速度
- 设置合适的超时时间
2. 流水线设计
- 快速失败原则:将快速的检查放在前面
- 并行执行:独立的任务可以并行运行
- 分层构建:合理划分构建阶段
- 增量构建:只构建变更的部分
3. 安全考虑
- 不在日志中输出敏感信息
- 使用最小权限原则
- 定期更新依赖和镜像
- 启用代码扫描和安全检查
4. 监控和维护
- 设置构建通知
- 监控构建时间和成功率
- 定期清理旧的制品
- 优化构建性能
🚀 进阶学习方向
- GitOps 实践:使用 Git 作为部署的单一真实来源
- 多云部署:在不同云平台间进行部署
- 蓝绿部署:零停机时间的部署策略
- 金丝雀发布:渐进式的功能发布
- 基础设施即代码:使用代码管理基础设施
好啦,宝子们!Gitee 的 CI/CD 就介绍到这里啦!🎉 掌握了这些技能,你就能实现真正的自动化开发流程,让代码从提交到部署一气呵成!
记住,CI/CD 不仅仅是工具,更是一种开发文化和理念。持续学习,持续改进,你的开发效率一定会越来越高的!💪
下一篇我们将深入探讨Gitee 在 DevOps 中的应用篇,敬请期待哦!✨