deploy.yml 改自动触发:build 成功后 workflow_run 链式触发
Build Docker Image / build (push) Has been cancelled

- on.workflow_run: 监听 Build Docker Image 完成事件,分支限 custom
- if 条件:仅在 build 成功时跑 deploy(失败时跳过,避免部署半成品)
- workflow_dispatch 保留作为手动备选(重新部署当前镜像 / 应急脚本)
- 脚本生成改 > 覆盖(原 >> 会累积历史脚本)+ 加 set -e 失败即停
- 加 Deploy summary 步骤把触发链路信息写入 GITHUB_STEP_SUMMARY
  方便从 UI 看到本次 deploy 跟在哪次 build 后面

CLAUDE.md 同步更新 workflow 清单 + 流程图:现在 push → build →
deploy 全自动 CD,仅需在 repo Variables 里配 CUSTOM_DEPLOY_SCRIPTS
脚本内容才能产生实际部署效果。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-02 21:07:09 +08:00
parent 4e8bbc0e5c
commit 1d89af2869
2 changed files with 37 additions and 8 deletions
+31 -3
View File
@@ -1,17 +1,45 @@
name: Deploy Docker Image
on:
workflow_dispatch
# 自动触发:build-image workflow 成功完成后跑
# workflow_run 是 cross-workflow 依赖:build 成功 → deploy 自动跟上
workflow_run:
workflows: ["Build Docker Image"]
types: [completed]
branches: [custom]
# 手动触发:保留作为应急通道(重新部署当前镜像 / 跑临时脚本)
workflow_dispatch:
jobs:
build:
deploy:
runs-on: ubuntu-latest
# 只在 build 成功后跑;手动触发时 workflow_run 字段不存在,
# || true 保证手动跑也能继续
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Execute custom script
# 用 > 覆盖而不是 >> 追加,避免历史脚本累积
run: |
cat >> deploy.sh <<EOF
cat > deploy.sh <<'EOF'
#!/bin/sh
set -e
${{ vars.CUSTOM_DEPLOY_SCRIPTS }}
EOF
chmod +x deploy.sh
./deploy.sh
- name: Deploy summary
if: always()
run: |
{
echo "## Deploy Summary"
echo ""
echo "| 项 | 值 |"
echo "|---|---|"
echo "| 触发方式 | \`${{ github.event_name }}\` |"
if [ "${{ github.event_name }}" = "workflow_run" ]; then
echo "| 触发自 | \`${{ github.event.workflow_run.name }}\` run #${{ github.event.workflow_run.run_number }} |"
echo "| 上游 build 结果 | \`${{ github.event.workflow_run.conclusion }}\` |"
echo "| 上游 build commit | \`${{ github.event.workflow_run.head_sha }}\` |"
fi
} >> "$GITHUB_STEP_SUMMARY"