diff --git a/trigger-version/README.md b/trigger-version/README.md new file mode 100644 index 0000000..7247fc4 --- /dev/null +++ b/trigger-version/README.md @@ -0,0 +1,194 @@ +# Trigger Version Info Action + +这个 GitHub Action 用于检测工作流的触发方式并提取版本信息,支持标签触发、版本分支触发和常规分支触发。 + +## 功能特性 + +- 🏷️ **标签触发检测**:自动识别版本标签(如 `v1.2.3`)并提取版本号 +- 🔄 **版本分支检测**:识别版本分支(如 `v1.2.x`)并提取版本信息 +- 🆕 **常规分支处理**:对于非版本分支提供基础信息 +- 🎯 **灵活的版本前缀**:支持自定义版本前缀(默认为 `v`) +- 📤 **环境变量输出**:自动设置环境变量供后续步骤使用 +- 📊 **详细的输出信息**:提供完整的引用信息和触发状态 + +## 输入参数 + +| 参数名 | 描述 | 必需 | 默认值 | +| ---------------- | -------------------------------- | ---- | ------ | +| `version-prefix` | 版本前缀,用于匹配版本标签或分支 | 否 | `v` | + +## 输出参数 + +| 参数名 | 描述 | 示例值 | +| -------------------- | ------------------------ | ------------------------------- | +| `ref-type` | 引用类型 | `tag` 或 `branch` | +| `ref-name` | 引用名称 | `v1.2.3`、`main`、`feature/xxx` | +| `is-version-trigger` | 是否为版本触发 | `true` 或 `false` | +| `trigger-version` | 触发的版本号(去除前缀) | `1.2.3` | +| `trigger-source` | 触发源 | `tag` 或 `branch` | +| `full-ref` | 完整的 Git 引用 | `refs/tags/v1.2.3` | + +## 环境变量 + +Action 会自动设置以下环境变量: + +- `IS_VERSION_TRIGGER`: 是否为版本触发(true/false) +- `TRIGGER_VERSION`: 触发的版本号 +- `TRIGGER_SOURCE`: 触发源(tag/branch) + +## 使用示例 + +### 基本用法 + +```yaml +name: Build and Deploy +on: + push: + branches: ["main", "v*"] + tags: ["v*"] + +jobs: + get-version-info: + 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 }} + steps: + - name: 获取版本信息 + id: version-info + uses: ./trigger-version + + - name: 显示版本信息 + run: | + echo "是否版本触发: ${{ steps.version-info.outputs.is-version-trigger }}" + echo "版本号: ${{ steps.version-info.outputs.trigger-version }}" + echo "触发源: ${{ steps.version-info.outputs.trigger-source }}" + + deploy: + needs: get-version-info + runs-on: ubuntu-latest + if: needs.get-version-info.outputs.is-version-trigger == 'true' + steps: + - name: 部署版本 + run: | + echo "部署版本: ${{ needs.get-version-info.outputs.trigger-version }}" +``` + +### 自定义版本前缀 + +```yaml +- name: 获取版本信息(自定义前缀) + id: version-info + uses: ./trigger-version + with: + version-prefix: "release-" +``` + +### 完整的 CI/CD 流程 + +```yaml +name: Complete CI/CD +on: + push: + branches: ["main", "develop", "v*"] + tags: ["v*"] + +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 }} + should-deploy: ${{ steps.check-deploy.outputs.should-deploy }} + steps: + - name: 获取版本信息 + id: version-info + uses: ./trigger-version + + - name: 检查是否需要部署 + id: check-deploy + run: | + if [[ "${{ steps.version-info.outputs.is-version-trigger }}" == "true" ]]; then + echo "should-deploy=true" >> $GITHUB_OUTPUT + elif [[ "${{ steps.version-info.outputs.ref-name }}" == "main" ]]; then + echo "should-deploy=true" >> $GITHUB_OUTPUT + else + echo "should-deploy=false" >> $GITHUB_OUTPUT + fi + + build: + needs: analyze + runs-on: ubuntu-latest + steps: + - name: 构建应用 + run: | + echo "构建中..." + # 你的构建逻辑 + + deploy-staging: + needs: [analyze, build] + runs-on: ubuntu-latest + if: needs.analyze.outputs.should-deploy == 'true' && needs.analyze.outputs.is-version-trigger == 'false' + steps: + - name: 部署到测试环境 + run: echo "部署到测试环境" + + deploy-production: + needs: [analyze, build] + runs-on: ubuntu-latest + if: needs.analyze.outputs.is-version-trigger == 'true' + steps: + - name: 部署到生产环境 + run: | + echo "部署版本 ${{ needs.analyze.outputs.trigger-version }} 到生产环境" +``` + +## 触发场景 + +### 标签触发 + +当推送版本标签时(如 `v1.2.3`): + +- `is-version-trigger`: `true` +- `trigger-version`: `1.2.3` +- `trigger-source`: `tag` + +### 版本分支触发 + +当推送到版本分支时(如 `v1.2.x`): + +- `is-version-trigger`: `true` +- `trigger-version`: `1.2.x` +- `trigger-source`: `branch` + +### 常规分支触发 + +当推送到普通分支时(如 `main`、`feature/xxx`): + +- `is-version-trigger`: `false` +- `trigger-version`: `""` +- `trigger-source`: `branch` + +## 最佳实践 + +1. **条件部署**:使用 `is-version-trigger` 来决定是否执行生产部署 +2. **版本标记**:在构建产物中使用 `trigger-version` 进行版本标记 +3. **环境区分**:根据触发源选择不同的部署环境 +4. **日志记录**:记录详细的版本信息用于追踪和调试 + +## 注意事项 + +- 版本前缀区分大小写 +- 空的版本号会被设置为空字符串 +- 确保工作流触发条件与你的版本策略一致 +- 在使用输出参数时注意布尔值的字符串比较(使用 `== 'true'`) + +## 故障排除 + +如果 Action 没有按预期工作: + +1. 检查触发条件是否正确设置 +2. 确认版本前缀配置是否正确 +3. 查看 Action 日志中的调试信息 +4. 验证 Git 引用格式是否符合预期 diff --git a/trigger-version/action.yml b/trigger-version/action.yml new file mode 100644 index 0000000..157519a --- /dev/null +++ b/trigger-version/action.yml @@ -0,0 +1,91 @@ +name: 'Trigger Version Info' +description: '获取触发版本信息,支持标签触发、版本分支触发和常规分支触发' +author: 'Your Organization' + +inputs: + version-prefix: + description: '版本前缀,用于匹配版本标签或分支(默认:v)' + required: false + default: 'v' + +outputs: + ref-type: + description: '引用类型 (tag/branch)' + value: ${{ steps.get-version-info.outputs.ref_type }} + ref-name: + description: '引用名称' + value: ${{ steps.get-version-info.outputs.ref_name }} + is-version-trigger: + description: '是否为版本触发(true/false)' + value: ${{ steps.get-version-info.outputs.is_version_trigger }} + trigger-version: + description: '触发的版本号(去除前缀后的版本)' + value: ${{ steps.get-version-info.outputs.trigger_version }} + trigger-source: + description: '触发源(tag/branch)' + value: ${{ steps.get-version-info.outputs.trigger_source }} + full-ref: + description: '完整的 Git 引用' + value: ${{ steps.get-version-info.outputs.full_ref }} + +runs: + using: 'composite' + steps: + - name: 获取触发版本信息 + id: get-version-info + shell: bash + run: | + # 获取GitHub上下文信息 + echo "触发方式: ${{ github.event_name }}" + echo "引用类型: ${{ github.ref_type }}" + echo "引用名称: ${{ github.ref_name }}" + echo "完整引用: ${{ github.ref }}" + + REF_TYPE="${{ github.ref_type }}" + REF_NAME="${{ github.ref_name }}" + FULL_REF="${{ github.ref }}" + VERSION_PREFIX="${{ inputs.version-prefix }}" + + # 判断是否为标签触发 + if [[ "$REF_TYPE" == "tag" ]]; then + # 从标签名提取版本号 (v1.2.3 -> 1.2.3) + if [[ "$REF_NAME" == ${VERSION_PREFIX}* ]]; then + TRIGGER_VERSION=${REF_NAME#${VERSION_PREFIX}} + else + TRIGGER_VERSION=$REF_NAME + fi + echo "🏷️ 标签触发: $REF_NAME" + echo "版本号: $TRIGGER_VERSION" + IS_VERSION_TRIGGER=true + TRIGGER_SOURCE=tag + elif [[ "$REF_TYPE" == "branch" && "$REF_NAME" == ${VERSION_PREFIX}* ]]; then + # 版本分支触发 + TRIGGER_VERSION=${REF_NAME#${VERSION_PREFIX}} + echo "🔄 版本分支触发: $REF_NAME" + echo "版本号: $TRIGGER_VERSION" + IS_VERSION_TRIGGER=true + TRIGGER_SOURCE=branch + else + # 常规分支触发 + echo "🆕 常规分支触发: $REF_NAME" + IS_VERSION_TRIGGER=false + TRIGGER_VERSION="" + TRIGGER_SOURCE=branch + fi + + # 设置环境变量 + echo "IS_VERSION_TRIGGER=$IS_VERSION_TRIGGER" >> $GITHUB_ENV + echo "TRIGGER_VERSION=$TRIGGER_VERSION" >> $GITHUB_ENV + echo "TRIGGER_SOURCE=$TRIGGER_SOURCE" >> $GITHUB_ENV + + # 输出到 step outputs + echo "ref_type=$REF_TYPE" >> $GITHUB_OUTPUT + echo "ref_name=$REF_NAME" >> $GITHUB_OUTPUT + echo "full_ref=$FULL_REF" >> $GITHUB_OUTPUT + echo "is_version_trigger=$IS_VERSION_TRIGGER" >> $GITHUB_OUTPUT + echo "trigger_version=$TRIGGER_VERSION" >> $GITHUB_OUTPUT + echo "trigger_source=$TRIGGER_SOURCE" >> $GITHUB_OUTPUT + +branding: + icon: 'git-branch' + color: 'blue' diff --git a/trigger-version/examples/basic-usage.yml b/trigger-version/examples/basic-usage.yml new file mode 100644 index 0000000..fc8d477 --- /dev/null +++ b/trigger-version/examples/basic-usage.yml @@ -0,0 +1,66 @@ +# 基本用法示例 +# 这个示例展示了如何使用 trigger-version action 获取版本信息 + +name: Basic Usage Example + +on: + push: + branches: ['main', 'develop', 'v*'] + tags: ['v*'] + workflow_dispatch: + +jobs: + get-version-info: + 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-type: ${{ steps.version-info.outputs.ref-type }} + ref-name: ${{ steps.version-info.outputs.ref-name }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 获取触发版本信息 + id: version-info + uses: ./trigger-version + + - name: 显示版本信息 + run: | + echo "========== 触发信息汇总 ==========" + echo "引用类型: ${{ steps.version-info.outputs.ref-type }}" + echo "引用名称: ${{ steps.version-info.outputs.ref-name }}" + echo "完整引用: ${{ steps.version-info.outputs.full-ref }}" + echo "是否版本触发: ${{ steps.version-info.outputs.is-version-trigger }}" + echo "触发版本号: ${{ steps.version-info.outputs.trigger-version }}" + echo "触发源: ${{ steps.version-info.outputs.trigger-source }}" + echo "================================" + + - name: 检查环境变量 + run: | + echo "========== 环境变量 ==========" + echo "IS_VERSION_TRIGGER: ${IS_VERSION_TRIGGER}" + echo "TRIGGER_VERSION: ${TRIGGER_VERSION}" + echo "TRIGGER_SOURCE: ${TRIGGER_SOURCE}" + echo "============================" + + show-usage: + needs: get-version-info + runs-on: ubuntu-latest + steps: + - name: 演示如何使用输出参数 + run: | + echo "在其他 job 中使用输出参数:" + echo "版本触发: ${{ needs.get-version-info.outputs.is-version-trigger }}" + echo "版本号: ${{ needs.get-version-info.outputs.trigger-version }}" + + if [[ "${{ needs.get-version-info.outputs.is-version-trigger }}" == "true" ]]; then + echo "✅ 这是一个版本发布触发" + echo "🚀 版本号: ${{ needs.get-version-info.outputs.trigger-version }}" + echo "📦 触发源: ${{ needs.get-version-info.outputs.trigger-source }}" + else + echo "ℹ️ 这是一个常规分支推送" + echo "🌿 分支名: ${{ needs.get-version-info.outputs.ref-name }}" + fi diff --git a/trigger-version/examples/conditional-deployment.yml b/trigger-version/examples/conditional-deployment.yml new file mode 100644 index 0000000..f4f8c8b --- /dev/null +++ b/trigger-version/examples/conditional-deployment.yml @@ -0,0 +1,119 @@ +# 条件部署示例 +# 这个示例展示了如何根据触发方式进行条件部署 + +name: Conditional Deployment + +on: + push: + branches: ['main', 'develop', 'v*'] + tags: ['v*'] + +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 }} + deploy-staging: ${{ steps.deployment-strategy.outputs.deploy-staging }} + deploy-production: ${{ steps.deployment-strategy.outputs.deploy-production }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 获取版本信息 + id: version-info + uses: ./trigger-version + + - name: 确定部署策略 + id: deployment-strategy + run: | + IS_VERSION="${{ steps.version-info.outputs.is-version-trigger }}" + REF_NAME="${{ steps.version-info.outputs.ref-name }}" + + echo "分析部署策略..." + + # 生产环境部署条件:版本标签触发 + if [[ "$IS_VERSION" == "true" && "${{ steps.version-info.outputs.trigger-source }}" == "tag" ]]; then + echo "deploy-production=true" >> $GITHUB_OUTPUT + echo "deploy-staging=false" >> $GITHUB_OUTPUT + echo "🚀 将部署到生产环境" + # 测试环境部署条件:main分支或版本分支 + elif [[ "$REF_NAME" == "main" || "$IS_VERSION" == "true" ]]; then + echo "deploy-production=false" >> $GITHUB_OUTPUT + echo "deploy-staging=true" >> $GITHUB_OUTPUT + echo "🧪 将部署到测试环境" + else + echo "deploy-production=false" >> $GITHUB_OUTPUT + echo "deploy-staging=false" >> $GITHUB_OUTPUT + echo "⏭️ 跳过部署" + fi + + build: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 构建应用 + run: | + echo "🔨 构建应用中..." + # 模拟构建过程 + sleep 2 + echo "✅ 构建完成" + + - name: 运行测试 + run: | + echo "🧪 运行测试中..." + # 模拟测试过程 + sleep 1 + echo "✅ 测试通过" + + deploy-staging: + needs: [analyze, build] + runs-on: ubuntu-latest + if: needs.analyze.outputs.deploy-staging == 'true' + environment: staging + + steps: + - name: 部署到测试环境 + run: | + echo "🚀 部署到测试环境" + echo "分支/版本: ${{ needs.analyze.outputs.ref-name }}" + if [[ "${{ needs.analyze.outputs.is-version-trigger }}" == "true" ]]; then + echo "版本号: ${{ needs.analyze.outputs.trigger-version }}" + fi + echo "✅ 测试环境部署完成" + + - name: 发送通知 + run: | + echo "📢 发送测试环境部署通知" + # 这里可以集成 Slack、钉钉等通知 + + deploy-production: + needs: [analyze, build] + runs-on: ubuntu-latest + if: needs.analyze.outputs.deploy-production == 'true' + environment: production + + steps: + - name: 部署到生产环境 + run: | + echo "🚀 部署版本 ${{ needs.analyze.outputs.trigger-version }} 到生产环境" + echo "触发源: ${{ needs.analyze.outputs.trigger-source }}" + # 生产部署逻辑 + echo "✅ 生产环境部署完成" + + - name: 创建 Release + if: needs.analyze.outputs.trigger-source == 'tag' + run: | + echo "📦 创建 GitHub Release" + echo "版本: v${{ needs.analyze.outputs.trigger-version }}" + # 这里可以使用 gh CLI 创建 release + + - name: 发送生产部署通知 + run: | + echo "📢 发送生产环境部署通知" + echo "🎉 版本 ${{ needs.analyze.outputs.trigger-version }} 已成功部署到生产环境" diff --git a/trigger-version/examples/custom-prefix.yml b/trigger-version/examples/custom-prefix.yml new file mode 100644 index 0000000..9199a2a --- /dev/null +++ b/trigger-version/examples/custom-prefix.yml @@ -0,0 +1,135 @@ +# 自定义版本前缀示例 +# 这个示例展示了如何使用不同的版本前缀 + +name: Custom Version Prefix + +on: + push: + branches: ['main', 'release-*'] + tags: ['release-*', 'hotfix-*'] + +jobs: + # 使用默认前缀 'v' 的标准版本 + standard-version: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/v') + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 获取标准版本信息 + id: standard-version + uses: ./trigger-version + # 使用默认的 'v' 前缀 + + - name: 显示标准版本信息 + run: | + echo "🏷️ 标准版本标签处理" + echo "版本号: ${{ steps.standard-version.outputs.trigger-version }}" + echo "是否版本触发: ${{ steps.standard-version.outputs.is-version-trigger }}" + + # 使用 'release-' 前缀的发布版本 + release-version: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/release-') || startsWith(github.ref, 'refs/heads/release-') + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 获取发布版本信息 + id: release-version + uses: ./trigger-version + with: + version-prefix: 'release-' + + - name: 显示发布版本信息 + run: | + echo "🚀 发布版本处理" + echo "引用名称: ${{ steps.release-version.outputs.ref-name }}" + echo "版本号: ${{ steps.release-version.outputs.trigger-version }}" + echo "触发源: ${{ steps.release-version.outputs.trigger-source }}" + echo "是否版本触发: ${{ steps.release-version.outputs.is-version-trigger }}" + + # 使用 'hotfix-' 前缀的热修复版本 + hotfix-version: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/hotfix-') + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 获取热修复版本信息 + id: hotfix-version + uses: ./trigger-version + with: + version-prefix: 'hotfix-' + + - name: 显示热修复版本信息 + run: | + echo "🔧 热修复版本处理" + echo "版本号: ${{ steps.hotfix-version.outputs.trigger-version }}" + echo "完整引用: ${{ steps.hotfix-version.outputs.full-ref }}" + + # 无前缀版本处理 + no-prefix-version: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/') && !startsWith(github.ref, 'refs/tags/v') && !startsWith(github.ref, 'refs/tags/release-') && !startsWith(github.ref, 'refs/tags/hotfix-') + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 获取无前缀版本信息 + id: no-prefix-version + uses: ./trigger-version + with: + version-prefix: '' + + - name: 显示无前缀版本信息 + run: | + echo "📦 无前缀版本处理" + echo "标签名: ${{ steps.no-prefix-version.outputs.ref-name }}" + echo "版本号: ${{ steps.no-prefix-version.outputs.trigger-version }}" + + # 演示多种前缀处理策略 + multi-prefix-demo: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: 检测并处理不同前缀 + run: | + REF_NAME="${{ github.ref_name }}" + REF_TYPE="${{ github.ref_type }}" + + echo "当前引用: $REF_NAME (类型: $REF_TYPE)" + + # 根据引用名称确定使用哪种前缀策略 + if [[ "$REF_NAME" == v* ]]; then + echo "检测到标准版本前缀 'v'" + VERSION_PREFIX="v" + elif [[ "$REF_NAME" == release-* ]]; then + echo "检测到发布前缀 'release-'" + VERSION_PREFIX="release-" + elif [[ "$REF_NAME" == hotfix-* ]]; then + echo "检测到热修复前缀 'hotfix-'" + VERSION_PREFIX="hotfix-" + else + echo "未检测到特定前缀,使用默认处理" + VERSION_PREFIX="" + fi + + echo "VERSION_PREFIX=$VERSION_PREFIX" >> $GITHUB_ENV + + - name: 使用动态前缀获取版本信息 + id: dynamic-version + uses: ./trigger-version + with: + version-prefix: ${{ env.VERSION_PREFIX }} + + - name: 显示动态版本信息 + run: | + echo "🎯 动态前缀处理结果" + echo "使用的前缀: '${{ env.VERSION_PREFIX }}'" + echo "提取的版本号: ${{ steps.dynamic-version.outputs.trigger-version }}" + echo "是否版本触发: ${{ steps.dynamic-version.outputs.is-version-trigger }}" diff --git a/trigger-version/examples/full-cicd.yml b/trigger-version/examples/full-cicd.yml new file mode 100644 index 0000000..9675e24 --- /dev/null +++ b/trigger-version/examples/full-cicd.yml @@ -0,0 +1,282 @@ +# 完整的 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 等