mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 06:33:37 +08:00
283 lines
10 KiB
YAML
283 lines
10 KiB
YAML
# 完整的 CI/CD 流程示例
|
||
# 这个示例展示了如何在完整的 CI/CD 流程中使用 trigger-version action
|
||
|
||
name: Complete CI/CD Pipeline
|
||
|
||
on:
|
||
push:
|
||
branches: ['main', 'develop', 'feature/*', 'v*']
|
||
tags: ['v*']
|
||
pull_request:
|
||
branches: ['main', 'develop']
|
||
|
||
env:
|
||
REGISTRY: ghcr.io
|
||
IMAGE_NAME: ${{ github.repository }}
|
||
|
||
jobs:
|
||
# 第一步:分析触发信息和构建策略
|
||
analyze:
|
||
runs-on: ubuntu-latest
|
||
outputs:
|
||
is-version-trigger: ${{ steps.version-info.outputs.is-version-trigger }}
|
||
trigger-version: ${{ steps.version-info.outputs.trigger-version }}
|
||
trigger-source: ${{ steps.version-info.outputs.trigger-source }}
|
||
ref-name: ${{ steps.version-info.outputs.ref-name }}
|
||
should-build: ${{ steps.build-strategy.outputs.should-build }}
|
||
should-test: ${{ steps.build-strategy.outputs.should-test }}
|
||
should-deploy-dev: ${{ steps.build-strategy.outputs.should-deploy-dev }}
|
||
should-deploy-staging: ${{ steps.build-strategy.outputs.should-deploy-staging }}
|
||
should-deploy-prod: ${{ steps.build-strategy.outputs.should-deploy-prod }}
|
||
image-tag: ${{ steps.build-strategy.outputs.image-tag }}
|
||
|
||
steps:
|
||
- name: Checkout code
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 获取触发版本信息
|
||
id: version-info
|
||
uses: ./trigger-version
|
||
|
||
- name: 确定构建和部署策略
|
||
id: build-strategy
|
||
run: |
|
||
IS_VERSION="${{ steps.version-info.outputs.is-version-trigger }}"
|
||
TRIGGER_SOURCE="${{ steps.version-info.outputs.trigger-source }}"
|
||
REF_NAME="${{ steps.version-info.outputs.ref-name }}"
|
||
TRIGGER_VERSION="${{ steps.version-info.outputs.trigger-version }}"
|
||
EVENT_NAME="${{ github.event_name }}"
|
||
|
||
echo "========== 构建策略分析 =========="
|
||
echo "事件类型: $EVENT_NAME"
|
||
echo "引用名称: $REF_NAME"
|
||
echo "是否版本触发: $IS_VERSION"
|
||
echo "触发源: $TRIGGER_SOURCE"
|
||
echo "版本号: $TRIGGER_VERSION"
|
||
|
||
# 确定是否构建
|
||
if [[ "$EVENT_NAME" == "pull_request" ]]; then
|
||
echo "should-build=true" >> $GITHUB_OUTPUT
|
||
echo "should-test=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-dev=false" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-staging=false" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-prod=false" >> $GITHUB_OUTPUT
|
||
echo "image-tag=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
|
||
echo "📝 PR 构建策略:仅构建和测试"
|
||
elif [[ "$IS_VERSION" == "true" && "$TRIGGER_SOURCE" == "tag" ]]; then
|
||
# 版本标签触发 - 完整流程
|
||
echo "should-build=true" >> $GITHUB_OUTPUT
|
||
echo "should-test=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-dev=false" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-staging=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-prod=true" >> $GITHUB_OUTPUT
|
||
echo "image-tag=v$TRIGGER_VERSION" >> $GITHUB_OUTPUT
|
||
echo "🚀 版本发布策略:完整 CI/CD 流程"
|
||
elif [[ "$REF_NAME" == "main" ]]; then
|
||
# 主分支推送
|
||
echo "should-build=true" >> $GITHUB_OUTPUT
|
||
echo "should-test=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-dev=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-staging=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-prod=false" >> $GITHUB_OUTPUT
|
||
echo "image-tag=main-${{ github.sha }}" >> $GITHUB_OUTPUT
|
||
echo "🌟 主分支策略:构建、测试、部署到开发和测试环境"
|
||
elif [[ "$REF_NAME" == "develop" ]]; then
|
||
# 开发分支推送
|
||
echo "should-build=true" >> $GITHUB_OUTPUT
|
||
echo "should-test=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-dev=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-staging=false" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-prod=false" >> $GITHUB_OUTPUT
|
||
echo "image-tag=develop-${{ github.sha }}" >> $GITHUB_OUTPUT
|
||
echo "🔧 开发分支策略:构建、测试、部署到开发环境"
|
||
elif [[ "$IS_VERSION" == "true" && "$TRIGGER_SOURCE" == "branch" ]]; then
|
||
# 版本分支推送
|
||
echo "should-build=true" >> $GITHUB_OUTPUT
|
||
echo "should-test=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-dev=false" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-staging=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-prod=false" >> $GITHUB_OUTPUT
|
||
echo "image-tag=branch-$TRIGGER_VERSION" >> $GITHUB_OUTPUT
|
||
echo "🔀 版本分支策略:构建、测试、部署到测试环境"
|
||
else
|
||
# 功能分支推送
|
||
echo "should-build=true" >> $GITHUB_OUTPUT
|
||
echo "should-test=true" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-dev=false" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-staging=false" >> $GITHUB_OUTPUT
|
||
echo "should-deploy-prod=false" >> $GITHUB_OUTPUT
|
||
echo "image-tag=feature-${{ github.sha }}" >> $GITHUB_OUTPUT
|
||
echo "🌿 功能分支策略:仅构建和测试"
|
||
fi
|
||
|
||
echo "================================="
|
||
|
||
# 第二步:代码质量检查
|
||
lint-and-test:
|
||
needs: analyze
|
||
runs-on: ubuntu-latest
|
||
if: needs.analyze.outputs.should-test == 'true'
|
||
|
||
steps:
|
||
- name: Checkout code
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Setup Node.js
|
||
uses: actions/setup-node@v4
|
||
with:
|
||
node-version: '18'
|
||
cache: 'npm'
|
||
|
||
- name: Install dependencies
|
||
run: npm ci
|
||
|
||
- name: 代码风格检查
|
||
run: |
|
||
echo "🔍 运行 ESLint..."
|
||
# npm run lint
|
||
|
||
- name: 类型检查
|
||
run: |
|
||
echo "📝 运行 TypeScript 检查..."
|
||
# npm run type-check
|
||
|
||
- name: 单元测试
|
||
run: |
|
||
echo "🧪 运行单元测试..."
|
||
# npm run test:unit
|
||
|
||
- name: 集成测试
|
||
run: |
|
||
echo "🔗 运行集成测试..."
|
||
# npm run test:integration
|
||
|
||
# 第三步:构建应用
|
||
build:
|
||
needs: [analyze, lint-and-test]
|
||
runs-on: ubuntu-latest
|
||
if: needs.analyze.outputs.should-build == 'true'
|
||
|
||
steps:
|
||
- name: Checkout code
|
||
uses: actions/checkout@v4
|
||
|
||
- name: Setup Node.js
|
||
uses: actions/setup-node@v4
|
||
with:
|
||
node-version: '18'
|
||
cache: 'npm'
|
||
|
||
- name: Install dependencies
|
||
run: npm ci
|
||
|
||
- name: 构建应用
|
||
run: |
|
||
echo "🔨 构建应用..."
|
||
echo "版本信息: ${{ needs.analyze.outputs.trigger-version || 'dev' }}"
|
||
# npm run build
|
||
|
||
- name: 构建 Docker 镜像
|
||
run: |
|
||
echo "🐳 构建 Docker 镜像..."
|
||
IMAGE_TAG="${{ needs.analyze.outputs.image-tag }}"
|
||
echo "镜像标签: $REGISTRY/$IMAGE_NAME:$IMAGE_TAG"
|
||
# docker build -t $REGISTRY/$IMAGE_NAME:$IMAGE_TAG .
|
||
|
||
- name: 推送 Docker 镜像
|
||
run: |
|
||
echo "📤 推送 Docker 镜像..."
|
||
# docker push $REGISTRY/$IMAGE_NAME:${{ needs.analyze.outputs.image-tag }}
|
||
|
||
# 第四步:部署到开发环境
|
||
deploy-dev:
|
||
needs: [analyze, build]
|
||
runs-on: ubuntu-latest
|
||
if: needs.analyze.outputs.should-deploy-dev == 'true'
|
||
environment: development
|
||
|
||
steps:
|
||
- name: 部署到开发环境
|
||
run: |
|
||
echo "🚀 部署到开发环境"
|
||
echo "镜像: $REGISTRY/$IMAGE_NAME:${{ needs.analyze.outputs.image-tag }}"
|
||
echo "分支: ${{ needs.analyze.outputs.ref-name }}"
|
||
# 部署逻辑
|
||
|
||
# 第五步:部署到测试环境
|
||
deploy-staging:
|
||
needs: [analyze, build]
|
||
runs-on: ubuntu-latest
|
||
if: needs.analyze.outputs.should-deploy-staging == 'true'
|
||
environment: staging
|
||
|
||
steps:
|
||
- name: 部署到测试环境
|
||
run: |
|
||
echo "🧪 部署到测试环境"
|
||
echo "镜像: $REGISTRY/$IMAGE_NAME:${{ needs.analyze.outputs.image-tag }}"
|
||
if [[ "${{ needs.analyze.outputs.is-version-trigger }}" == "true" ]]; then
|
||
echo "版本: ${{ needs.analyze.outputs.trigger-version }}"
|
||
fi
|
||
# 部署逻辑
|
||
|
||
- name: 运行冒烟测试
|
||
run: |
|
||
echo "🔥 运行冒烟测试..."
|
||
# 测试逻辑
|
||
|
||
# 第六步:部署到生产环境
|
||
deploy-production:
|
||
needs: [analyze, build, deploy-staging]
|
||
runs-on: ubuntu-latest
|
||
if: needs.analyze.outputs.should-deploy-prod == 'true'
|
||
environment: production
|
||
|
||
steps:
|
||
- name: 部署到生产环境
|
||
run: |
|
||
echo "🚀 部署版本 ${{ needs.analyze.outputs.trigger-version }} 到生产环境"
|
||
echo "镜像: $REGISTRY/$IMAGE_NAME:${{ needs.analyze.outputs.image-tag }}"
|
||
echo "触发源: ${{ needs.analyze.outputs.trigger-source }}"
|
||
# 生产部署逻辑
|
||
|
||
- name: 健康检查
|
||
run: |
|
||
echo "💚 执行生产环境健康检查..."
|
||
# 健康检查逻辑
|
||
|
||
- name: 创建 GitHub Release
|
||
if: needs.analyze.outputs.trigger-source == 'tag'
|
||
run: |
|
||
echo "📦 创建 GitHub Release"
|
||
echo "版本: v${{ needs.analyze.outputs.trigger-version }}"
|
||
# gh release create v${{ needs.analyze.outputs.trigger-version }} --generate-notes
|
||
|
||
# 第七步:通知和后续处理
|
||
notify:
|
||
needs: [analyze, deploy-dev, deploy-staging, deploy-production]
|
||
runs-on: ubuntu-latest
|
||
if: always() && needs.analyze.outputs.should-build == 'true'
|
||
|
||
steps:
|
||
- name: 发送部署通知
|
||
run: |
|
||
echo "📢 发送部署通知"
|
||
|
||
if [[ "${{ needs.analyze.outputs.is-version-trigger }}" == "true" ]]; then
|
||
if [[ "${{ needs.deploy-production.result }}" == "success" ]]; then
|
||
echo "🎉 版本 ${{ needs.analyze.outputs.trigger-version }} 已成功部署到生产环境"
|
||
else
|
||
echo "❌ 版本 ${{ needs.analyze.outputs.trigger-version }} 生产部署失败"
|
||
fi
|
||
else
|
||
echo "ℹ️ 分支 ${{ needs.analyze.outputs.ref-name }} 部署完成"
|
||
fi
|
||
|
||
# 发送到 Slack/钉钉/邮件等
|
||
|
||
- name: 更新文档
|
||
if: needs.analyze.outputs.trigger-source == 'tag'
|
||
run: |
|
||
echo "📚 更新版本文档"
|
||
# 自动更新 CHANGELOG 等
|