mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 06:33:37 +08:00
291 lines
9.1 KiB
YAML
291 lines
9.1 KiB
YAML
# 多环境状态管理示例
|
|
# 演示如何在多环境部署中使用状态缓存来管理不同环境的配置和状态
|
|
|
|
name: Cache State - 多环境管理
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
target_environments:
|
|
description: '目标环境(逗号分隔)'
|
|
required: true
|
|
default: 'dev,staging,production'
|
|
config_update:
|
|
description: '是否更新配置'
|
|
required: false
|
|
default: false
|
|
type: boolean
|
|
|
|
jobs:
|
|
setup-environments:
|
|
runs-on: ubuntu-latest
|
|
strategy:
|
|
matrix:
|
|
environment: [dev, staging, production]
|
|
outputs:
|
|
environments: ${{ steps.env-list.outputs.environments }}
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 环境特定配置
|
|
id: env-config
|
|
run: |
|
|
case "${{ matrix.environment }}" in
|
|
"dev")
|
|
echo "replicas=1" >> $GITHUB_OUTPUT
|
|
echo "resources=minimal" >> $GITHUB_OUTPUT
|
|
echo "debug=true" >> $GITHUB_OUTPUT
|
|
;;
|
|
"staging")
|
|
echo "replicas=2" >> $GITHUB_OUTPUT
|
|
echo "resources=standard" >> $GITHUB_OUTPUT
|
|
echo "debug=false" >> $GITHUB_OUTPUT
|
|
;;
|
|
"production")
|
|
echo "replicas=3" >> $GITHUB_OUTPUT
|
|
echo "resources=high" >> $GITHUB_OUTPUT
|
|
echo "debug=false" >> $GITHUB_OUTPUT
|
|
;;
|
|
esac
|
|
|
|
- name: 保存环境副本数配置
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'replicas-${{ matrix.environment }}'
|
|
state-value: ${{ steps.env-config.outputs.replicas }}
|
|
default-value: '1'
|
|
cache-prefix: 'env-config'
|
|
|
|
- name: 保存环境资源配置
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'resources-${{ matrix.environment }}'
|
|
state-value: ${{ steps.env-config.outputs.resources }}
|
|
default-value: 'minimal'
|
|
cache-prefix: 'env-config'
|
|
|
|
- name: 保存调试模式配置
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'debug-${{ matrix.environment }}'
|
|
state-value: ${{ steps.env-config.outputs.debug }}
|
|
default-value: 'true'
|
|
cache-prefix: 'env-config'
|
|
|
|
- name: 检查环境健康状态
|
|
id: health-check
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'health-status-${{ matrix.environment }}'
|
|
default-value: 'unknown'
|
|
action: 'get'
|
|
cache-prefix: 'health'
|
|
|
|
- name: 初始化环境健康状态
|
|
if: steps.health-check.outputs.state-value == 'unknown'
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'health-status-${{ matrix.environment }}'
|
|
state-value: 'healthy'
|
|
action: 'set'
|
|
cache-prefix: 'health'
|
|
|
|
- name: 环境配置总结
|
|
run: |
|
|
echo "🌍 环境 ${{ matrix.environment }} 配置:"
|
|
echo " - 副本数: ${{ steps.env-config.outputs.replicas }}"
|
|
echo " - 资源配置: ${{ steps.env-config.outputs.resources }}"
|
|
echo " - 调试模式: ${{ steps.env-config.outputs.debug }}"
|
|
echo " - 健康状态: ${{ steps.health-check.outputs.state-value }}"
|
|
|
|
compare-environments:
|
|
needs: setup-environments
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 获取开发环境配置
|
|
id: dev-config
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'replicas-dev'
|
|
action: 'get'
|
|
cache-prefix: 'env-config'
|
|
|
|
- name: 获取预发布环境配置
|
|
id: staging-config
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'replicas-staging'
|
|
action: 'get'
|
|
cache-prefix: 'env-config'
|
|
|
|
- name: 获取生产环境配置
|
|
id: prod-config
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'replicas-production'
|
|
action: 'get'
|
|
cache-prefix: 'env-config'
|
|
|
|
- name: 环境配置对比
|
|
run: |
|
|
echo "🔍 环境配置对比:"
|
|
echo " - 开发环境副本数: ${{ steps.dev-config.outputs.state-value }}"
|
|
echo " - 预发布环境副本数: ${{ steps.staging-config.outputs.state-value }}"
|
|
echo " - 生产环境副本数: ${{ steps.prod-config.outputs.state-value }}"
|
|
|
|
# 检查配置一致性
|
|
if [[ "${{ steps.dev-config.outputs.state-value }}" < "${{ steps.staging-config.outputs.state-value }}" ]] && \
|
|
[[ "${{ steps.staging-config.outputs.state-value }}" < "${{ steps.prod-config.outputs.state-value }}" ]]; then
|
|
echo "✅ 环境配置递增合理"
|
|
else
|
|
echo "⚠️ 环境配置可能需要检查"
|
|
fi
|
|
|
|
environment-health-monitor:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 模拟健康检查 - 开发环境
|
|
run: |
|
|
# 模拟健康检查逻辑
|
|
HEALTH_STATUS="healthy"
|
|
if [[ $((RANDOM % 10)) -eq 0 ]]; then
|
|
HEALTH_STATUS="unhealthy"
|
|
fi
|
|
echo "dev_health=${HEALTH_STATUS}" >> $GITHUB_ENV
|
|
|
|
- name: 更新开发环境健康状态
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'health-status-dev'
|
|
state-value: ${{ env.dev_health }}
|
|
action: 'set'
|
|
cache-prefix: 'health'
|
|
|
|
- name: 模拟健康检查 - 预发布环境
|
|
run: |
|
|
HEALTH_STATUS="healthy"
|
|
if [[ $((RANDOM % 15)) -eq 0 ]]; then
|
|
HEALTH_STATUS="degraded"
|
|
fi
|
|
echo "staging_health=${HEALTH_STATUS}" >> $GITHUB_ENV
|
|
|
|
- name: 更新预发布环境健康状态
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'health-status-staging'
|
|
state-value: ${{ env.staging_health }}
|
|
action: 'set'
|
|
cache-prefix: 'health'
|
|
|
|
- name: 模拟健康检查 - 生产环境
|
|
run: |
|
|
HEALTH_STATUS="healthy"
|
|
if [[ $((RANDOM % 20)) -eq 0 ]]; then
|
|
HEALTH_STATUS="critical"
|
|
fi
|
|
echo "prod_health=${HEALTH_STATUS}" >> $GITHUB_ENV
|
|
|
|
- name: 更新生产环境健康状态
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'health-status-production'
|
|
state-value: ${{ env.prod_health }}
|
|
action: 'set'
|
|
cache-prefix: 'health'
|
|
|
|
generate-environment-report:
|
|
needs: [setup-environments, environment-health-monitor]
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 收集所有环境状态
|
|
id: collect-status
|
|
run: |
|
|
echo "🔍 收集环境状态信息..."
|
|
|
|
# 创建状态收集脚本
|
|
cat > collect_status.sh << 'EOF'
|
|
#!/bin/bash
|
|
|
|
ENVIRONMENTS=("dev" "staging" "production")
|
|
|
|
echo "| 环境 | 副本数 | 资源配置 | 调试模式 | 健康状态 |"
|
|
echo "|------|--------|----------|----------|----------|"
|
|
|
|
for env in "${ENVIRONMENTS[@]}"; do
|
|
echo "| $env | - | - | - | - |"
|
|
done
|
|
EOF
|
|
|
|
chmod +x collect_status.sh
|
|
./collect_status.sh
|
|
|
|
- name: 获取开发环境完整状态
|
|
id: dev-status
|
|
run: |
|
|
echo "=== 开发环境状态 ==="
|
|
|
|
- name: 读取开发环境配置
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'replicas-dev'
|
|
action: 'get'
|
|
cache-prefix: 'env-config'
|
|
|
|
- name: 读取开发环境健康状态
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'health-status-dev'
|
|
action: 'get'
|
|
cache-prefix: 'health'
|
|
|
|
- name: 获取预发布环境完整状态
|
|
run: |
|
|
echo "=== 预发布环境状态 ==="
|
|
|
|
- name: 获取生产环境完整状态
|
|
run: |
|
|
echo "=== 生产环境状态 ==="
|
|
|
|
- name: 生成环境状态报告
|
|
run: |
|
|
echo "📊 多环境状态报告 - $(date)"
|
|
echo "=================================="
|
|
echo ""
|
|
echo "本次工作流程已完成以下操作:"
|
|
echo "✅ 环境配置初始化和更新"
|
|
echo "✅ 健康状态监控"
|
|
echo "✅ 配置对比分析"
|
|
echo ""
|
|
echo "所有环境状态已缓存,可供后续工作流使用。"
|
|
|
|
cleanup-old-states:
|
|
if: github.event.inputs.config_update == 'true'
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 清理过期的配置状态
|
|
run: |
|
|
echo "🧹 清理过期的配置状态..."
|
|
# 这里可以实现清理逻辑
|
|
# 例如,设置空值来清理特定的缓存状态
|
|
|
|
- name: 重置所有环境的临时状态
|
|
uses: .actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: 'temp-maintenance-mode'
|
|
state-value: ''
|
|
action: 'set'
|
|
cache-prefix: 'maintenance'
|