From 1a66c21f59f42214d2caf1a102264ce9351665d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E6=A0=91?= Date: Tue, 30 Dec 2025 20:37:05 +0800 Subject: [PATCH] =?UTF-8?q?ci(workflow):=20=E6=B7=BB=E5=8A=A0=E9=98=BF?= =?UTF-8?q?=E9=87=8C=E4=BA=91ESA=E4=BB=A3=E7=A0=81=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=B8=85=E7=90=86=E5=B7=A5=E4=BD=9C=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加GitHub Actions工作流用于在main分支推送时自动清理阿里云ESA的旧代码版本 默认保留最新版本,可通过变量配置保留数量 --- .github/workflows/del-esa-code.yml | 113 +++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 .github/workflows/del-esa-code.yml diff --git a/.github/workflows/del-esa-code.yml b/.github/workflows/del-esa-code.yml new file mode 100644 index 000000000..dd30c1f62 --- /dev/null +++ b/.github/workflows/del-esa-code.yml @@ -0,0 +1,113 @@ +name: Clean ESA Versions on Main + +on: + push: + branches: [ main ] + +permissions: + contents: read + +jobs: + clean-esa-versions: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + # 下载阿里云CLI https://help.aliyun.com/zh/cli/ + - name: Download Aliyun Cli + run: | + set -euo pipefail + /bin/bash -c "$(curl -fsSL https://aliyuncli.alicdn.com/install.sh)" + + # 配置阿里云CLI + - name: Configure Aliyun CLI + run: | + aliyun configure set \ + --profile AkProfile \ + --mode AK \ + --access-key-id ${{ secrets.ALIYUN_ACCESS_KEY_ID }} \ + --access-key-secret ${{ secrets.ALIYUN_ACCESS_KEY_SECRET }} \ + --region "cn-hangzhou" + + # 获取Pages名称(可配置) + - name: Get Pages Name + id: get-pages-name + run: | + # 从配置中读取Pages名称,这里使用默认值blog,可根据需要修改 + echo "pages_name=fuwari" >> $GITHUB_OUTPUT + + # 获取所有代码版本并清理旧版本 + - name: Clean ESA Code Versions + env: + PAGES_NAME: ${{ steps.get-pages-name.outputs.pages_name }} + # 配置保留的版本数,默认为1(只保留最新版本) + RETAIN_VERSIONS: ${{ vars.RETAIN_VERSIONS || '1' }} + run: | + set -euo pipefail + + echo "开始清理阿里云ESA版本..." + echo "目标Pages: $PAGES_NAME" + echo "保留版本数: $RETAIN_VERSIONS" + + # 获取所有版本信息 + VERSIONS_JSON=$(aliyun esa ListRoutineCodeVersions --region cn-hangzhou --Name $PAGES_NAME) + + # 解析版本数量 + TOTAL_COUNT=$(echo $VERSIONS_JSON | jq -r '.TotalCount') + echo "当前共有 $TOTAL_COUNT 个版本" + + # 如果版本数不超过保留数量,不需要清理 + if [ "$TOTAL_COUNT" -le "$RETAIN_VERSIONS" ]; then + echo "版本数 ($TOTAL_COUNT) 未超过保留数量 ($RETAIN_VERSIONS),无需清理" + echo "ESA会自动检测main分支更新并部署" + exit 0 + fi + + # 计算需要删除的版本数(保留指定数量的最新版本) + DELETE_COUNT=$((TOTAL_COUNT - RETAIN_VERSIONS)) + echo "需要删除 $DELETE_COUNT 个旧版本,保留最新的 $RETAIN_VERSIONS 个版本" + + # 解析版本列表,按创建时间排序(最早的在前) + VERSIONS_TO_DELETE=$(echo $VERSIONS_JSON | jq -r '.CodeVersions | sort_by(.CreateTime) | .[0:'$DELETE_COUNT'] | .[].CodeVersion') + + echo "将要删除的版本: $VERSIONS_TO_DELETE" + + # 删除旧版本 + DELETED_COUNT=0 + for VERSION in $VERSIONS_TO_DELETE; do + echo "正在删除版本: $VERSION" + DELETE_RESULT=$(aliyun esa DeleteRoutineCodeVersion --region cn-hangzhou --Name $PAGES_NAME --CodeVersion $VERSION) + STATUS=$(echo $DELETE_RESULT | jq -r '.Status') + + if [ "$STATUS" = "OK" ]; then + echo "版本 $VERSION 删除成功" + DELETED_COUNT=$((DELETED_COUNT + 1)) + else + echo "版本 $VERSION 删除失败: $DELETE_RESULT" + exit 1 + fi + done + + echo "版本清理完成!成功删除 $DELETED_COUNT 个旧版本" + + # 验证清理结果 + - name: Verify Clean Result + env: + PAGES_NAME: ${{ steps.get-pages-name.outputs.pages_name }} + RETAIN_VERSIONS: ${{ vars.RETAIN_VERSIONS || '1' }} + run: | + set -euo pipefail + + echo "验证清理结果..." + RESULT_JSON=$(aliyun esa ListRoutineCodeVersions --region cn-hangzhou --Name $PAGES_NAME) + REMAINING_COUNT=$(echo $RESULT_JSON | jq -r '.TotalCount') + + echo "清理后剩余版本数: $REMAINING_COUNT" + + if [ "$REMAINING_COUNT" -le "$RETAIN_VERSIONS" ]; then + echo "版本清理成功,当前版本数: $REMAINING_COUNT (保留设置: $RETAIN_VERSIONS)" + echo "ESA会自动检测main分支更新并开始部署" + else + echo "版本清理失败,仍有 $REMAINING_COUNT 个版本,期望保留: $RETAIN_VERSIONS" + exit 1 + fi \ No newline at end of file