add: notify

This commit is contained in:
孙诚
2026-01-07 17:39:05 +08:00
parent 620effd1f8
commit eec3702ce7

View File

@@ -59,8 +59,81 @@ jobs:
name: Cleanup Dangling Images
runs-on: ubuntu-latest
needs: deploy
if: always()
steps:
- name: Remove dangling images
run: |
docker rm $(docker images -f "dangling=true" -q) || true
echo "Cleanup completed."
notify:
name: WeChat Notification
runs-on: ubuntu-latest
needs: [build, deploy]
if: always()
steps:
- name: Send WeChat Notification
run: |
# 判断结果:只有 build 和 deploy 都成功才算成功
if [[ "${{ needs.build.result }}" == "success" && "${{ needs.deploy.result }}" == "success" ]]; then
MSG_TITLE="构建并部署成功"
RESULT_ICON="✅"
MSG_COLOR="info"
else
MSG_TITLE="自动部署发现异常"
RESULT_ICON="❌"
MSG_COLOR="warning"
fi
WEBHOOK_URL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=96f9fa23-a959-4492-ac3a-7415fae19680"
# 准备 Markdown 内容
cat <<EOF > wechat_payload.json
{
"msgtype": "markdown",
"markdown": {
"content": "### $RESULT_ICON $MSG_TITLE\n> **项目**: [${{ gitea.repository }}](${{ gitea.server_url }}/${{ gitea.repository }})\n> **状态**: <font color=\"$MSG_COLOR\">$MSG_TITLE</font>\n> **分支**: \`${{ gitea.ref_name }}\`\n> **触发者**: ${{ gitea.actor }}\n> **任务编号**: [#${{ gitea.run_number }}](${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }})\n> **构建状态**: ${{ needs.build.result || 'skipped' }}\n> **部署状态**: ${{ needs.deploy.result || 'skipped' }}\n> **提交详情**: [${{ gitea.sha }}](${{ gitea.server_url }}/${{ gitea.repository }}/commit/${{ gitea.sha }})"
}
}
EOF
curl -s -X POST "$WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d @wechat_payload.json
notify:
name: WeChat Notification
runs-on: ubuntu-latest
needs: [build, deploy, cleanup]
if: always()
steps:
- name: Send WeChat Notification
run: |
# 判断整体结果
if [[ "${{ needs.build.result }}" == "success" && "${{ needs.deploy.result }}" == "success" ]]; then
MSG_TITLE="构建并部署成功"
RESULT_ICON="✅"
MSG_COLOR="info"
else
MSG_TITLE="自动部署发现异常"
RESULT_ICON="❌"
MSG_COLOR="warning"
fi
WEBHOOK_URL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=96f9fa23-a959-4492-ac3a-7415fae19680"
# 准备 Markdown 内容
# 使用 heredoc 简化 JSON 生成
cat <<EOF > wechat_payload.json
{
"msgtype": "markdown",
"markdown": {
"content": "### $RESULT_ICON $MSG_TITLE\n> **项目名称**: [${{ gitea.repository }}](${{ gitea.server_url }}/${{ gitea.repository }})\n> **流水线**: ${{ gitea.workflow }}\n> **运行编号**: [#${{ gitea.run_number }}](${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }})\n> **状态**: <font color=\"$MSG_COLOR\">$MSG_TITLE</font>\n> **分支**: \`${{ gitea.ref_name }}\`\n> **触发者**: ${{ gitea.actor }}\n> **提交详情**: [${{ gitea.sha }}](${{ gitea.server_url }}/${{ gitea.repository }}/commit/${{ gitea.sha }})"
}
}
EOF
# 发送请求
curl -s -X POST "\$WEBHOOK_URL" \\
-H "Content-Type: application/json" \\
-d @wechat_payload.json