mirror of
https://git.bjxgj.com/xgj/xgj-actions.git
synced 2025-10-14 06:33:37 +08:00
264 lines
8.5 KiB
YAML
264 lines
8.5 KiB
YAML
# 删除功能演示
|
|
# 展示如何使用 cache-state action 的删除功能
|
|
|
|
name: Cache State - 删除功能演示
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
demo_type:
|
|
description: '演示类型'
|
|
required: true
|
|
default: 'basic'
|
|
type: choice
|
|
options:
|
|
- basic
|
|
- cleanup
|
|
- conditional
|
|
- batch
|
|
|
|
jobs:
|
|
demo-basic-delete:
|
|
if: ${{ inputs.demo_type == 'basic' }}
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 设置测试状态
|
|
id: setup-state
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "demo-delete-test"
|
|
state-value: "test-value-${{ github.run_id }}"
|
|
action: "set"
|
|
|
|
- name: 验证状态已设置
|
|
id: verify-set
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "demo-delete-test"
|
|
default-value: "not-found"
|
|
action: "get"
|
|
|
|
- name: 显示设置结果
|
|
run: |
|
|
echo "设置的值: ${{ steps.verify-set.outputs.state-value }}"
|
|
echo "缓存命中: ${{ steps.verify-set.outputs.cache-hit }}"
|
|
|
|
if [[ "${{ steps.verify-set.outputs.cache-hit }}" == "true" ]]; then
|
|
echo "✅ 状态设置成功"
|
|
else
|
|
echo "❌ 状态设置失败"
|
|
exit 1
|
|
fi
|
|
|
|
- name: 删除状态
|
|
id: delete-state
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "demo-delete-test"
|
|
action: "del"
|
|
|
|
- name: 验证删除结果
|
|
run: |
|
|
echo "删除操作: ${{ steps.delete-state.outputs.deleted }}"
|
|
echo "缓存键: ${{ steps.delete-state.outputs.cache-key }}"
|
|
|
|
if [[ "${{ steps.delete-state.outputs.deleted }}" == "true" ]]; then
|
|
echo "✅ 删除操作执行成功"
|
|
else
|
|
echo "❌ 删除操作失败"
|
|
exit 1
|
|
fi
|
|
|
|
- name: 验证状态已删除
|
|
id: verify-deleted
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "demo-delete-test"
|
|
default-value: "fallback-value"
|
|
action: "get"
|
|
|
|
- name: 确认删除效果
|
|
run: |
|
|
echo "验证获取的值: ${{ steps.verify-deleted.outputs.state-value }}"
|
|
echo "缓存命中: ${{ steps.verify-deleted.outputs.cache-hit }}"
|
|
echo "使用默认值: ${{ steps.verify-deleted.outputs.used-default }}"
|
|
|
|
if [[ "${{ steps.verify-deleted.outputs.cache-hit }}" == "false" && "${{ steps.verify-deleted.outputs.used-default }}" == "true" ]]; then
|
|
echo "✅ 状态已成功删除,使用默认值"
|
|
else
|
|
echo "❌ 状态删除验证失败"
|
|
exit 1
|
|
fi
|
|
|
|
demo-cleanup-workflow:
|
|
if: ${{ inputs.demo_type == 'cleanup' }}
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 创建多个测试状态
|
|
run: |
|
|
echo "创建多个测试状态用于清理演示..."
|
|
|
|
- name: 设置构建状态
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "build-status-${{ github.run_id }}"
|
|
state-value: "completed"
|
|
action: "set"
|
|
|
|
- name: 设置部署状态
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "deploy-status-${{ github.run_id }}"
|
|
state-value: "success"
|
|
action: "set"
|
|
|
|
- name: 设置测试状态
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "test-status-${{ github.run_id }}"
|
|
state-value: "passed"
|
|
action: "set"
|
|
|
|
- name: 模拟工作流结束后的清理
|
|
run: |
|
|
echo "🧹 开始清理工作流状态..."
|
|
|
|
- name: 清理构建状态
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "build-status-${{ github.run_id }}"
|
|
action: "del"
|
|
|
|
- name: 清理部署状态
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "deploy-status-${{ github.run_id }}"
|
|
action: "del"
|
|
|
|
- name: 清理测试状态
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "test-status-${{ github.run_id }}"
|
|
action: "del"
|
|
|
|
- name: 清理完成
|
|
run: |
|
|
echo "✅ 所有状态已清理完成"
|
|
|
|
demo-conditional-delete:
|
|
if: ${{ inputs.demo_type == 'conditional' }}
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 检查现有状态
|
|
id: check-state
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "conditional-delete-test"
|
|
default-value: "none"
|
|
action: "get"
|
|
|
|
- name: 显示当前状态
|
|
run: |
|
|
echo "当前状态: ${{ steps.check-state.outputs.state-value }}"
|
|
echo "缓存命中: ${{ steps.check-state.outputs.cache-hit }}"
|
|
|
|
- name: 条件性删除 - 如果状态存在
|
|
if: ${{ steps.check-state.outputs.cache-hit == 'true' }}
|
|
id: conditional-delete
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "conditional-delete-test"
|
|
action: "del"
|
|
|
|
- name: 条件性设置 - 如果状态不存在
|
|
if: ${{ steps.check-state.outputs.cache-hit != 'true' }}
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "conditional-delete-test"
|
|
state-value: "newly-created-${{ github.run_id }}"
|
|
action: "set"
|
|
|
|
- name: 显示操作结果
|
|
run: |
|
|
if [[ "${{ steps.check-state.outputs.cache-hit }}" == "true" ]]; then
|
|
echo "🗑️ 执行了删除操作"
|
|
echo "删除结果: ${{ steps.conditional-delete.outputs.deleted }}"
|
|
else
|
|
echo "🆕 执行了创建操作"
|
|
fi
|
|
|
|
demo-batch-operations:
|
|
if: ${{ inputs.demo_type == 'batch' }}
|
|
runs-on: ubuntu-latest
|
|
strategy:
|
|
matrix:
|
|
operation: [create, delete]
|
|
state_id: [1, 2, 3]
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 批量创建状态
|
|
if: ${{ matrix.operation == 'create' }}
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "batch-state-${{ matrix.state_id }}"
|
|
state-value: "batch-value-${{ matrix.state_id }}-${{ github.run_id }}"
|
|
action: "set"
|
|
|
|
- name: 批量删除状态
|
|
if: ${{ matrix.operation == 'delete' }}
|
|
uses: actions/xgj/cache-state@v1
|
|
with:
|
|
state-key: "batch-state-${{ matrix.state_id }}"
|
|
action: "del"
|
|
|
|
- name: 显示操作结果
|
|
run: |
|
|
echo "操作: ${{ matrix.operation }}"
|
|
echo "状态ID: ${{ matrix.state_id }}"
|
|
echo "✅ 批量操作 ${{ matrix.operation }} 完成"
|
|
|
|
demo-summary:
|
|
needs: [demo-basic-delete, demo-cleanup-workflow, demo-conditional-delete, demo-batch-operations]
|
|
if: always()
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: 生成演示总结
|
|
run: |
|
|
echo "## 🗑️ 删除功能演示总结" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "### 演示类型: ${{ inputs.demo_type }}" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
|
|
case "${{ inputs.demo_type }}" in
|
|
"basic")
|
|
echo "✅ **基础删除演示**: 展示了设置、删除、验证的完整流程" >> $GITHUB_STEP_SUMMARY
|
|
;;
|
|
"cleanup")
|
|
echo "✅ **清理工作流演示**: 展示了批量清理多个状态的用法" >> $GITHUB_STEP_SUMMARY
|
|
;;
|
|
"conditional")
|
|
echo "✅ **条件删除演示**: 展示了基于状态存在性的条件操作" >> $GITHUB_STEP_SUMMARY
|
|
;;
|
|
"batch")
|
|
echo "✅ **批量操作演示**: 展示了并行批量创建和删除状态" >> $GITHUB_STEP_SUMMARY
|
|
;;
|
|
esac
|
|
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "### 核心特性" >> $GITHUB_STEP_SUMMARY
|
|
echo "- 🗑️ **状态删除**: 通过删除标记实现状态清除" >> $GITHUB_STEP_SUMMARY
|
|
echo "- 🔄 **即时生效**: 删除后立即生效,后续访问使用默认值" >> $GITHUB_STEP_SUMMARY
|
|
echo "- 📊 **状态透明**: 通过 \`deleted\` 输出明确告知删除操作" >> $GITHUB_STEP_SUMMARY
|
|
echo "- 🛡️ **安全可靠**: 基于缓存覆盖机制,不会影响其他状态" >> $GITHUB_STEP_SUMMARY
|