From eec3702ce7024d26d50b99cbfc1370f018aa4626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E8=AF=9A?= Date: Wed, 7 Jan 2026 17:39:05 +0800 Subject: [PATCH] add: notify --- .gitea/workflows/push.yml | 75 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/push.yml b/.gitea/workflows/push.yml index db769f2..42b359b 100644 --- a/.gitea/workflows/push.yml +++ b/.gitea/workflows/push.yml @@ -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." \ No newline at end of file + 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 < wechat_payload.json + { + "msgtype": "markdown", + "markdown": { + "content": "### $RESULT_ICON $MSG_TITLE\n> **项目**: [${{ gitea.repository }}](${{ gitea.server_url }}/${{ gitea.repository }})\n> **状态**: $MSG_TITLE\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 < 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> **状态**: $MSG_TITLE\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 \ No newline at end of file