feat:部署流水线增加钉钉通知
This commit is contained in:
@@ -17,6 +17,9 @@ jobs:
|
|||||||
build-and-deploy:
|
build-and-deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
- name: 记录开始时间
|
||||||
|
run: echo "JOB_START=$(date +%s)" >> "$GITHUB_ENV"
|
||||||
|
|
||||||
- name: 拉取代码
|
- name: 拉取代码
|
||||||
uses: https://gitee.com/liu_zhengchi/checkout@v4
|
uses: https://gitee.com/liu_zhengchi/checkout@v4
|
||||||
|
|
||||||
@@ -121,3 +124,72 @@ jobs:
|
|||||||
- name: 部署结果
|
- name: 部署结果
|
||||||
if: always()
|
if: always()
|
||||||
run: echo "镜像 ${{ env.IMAGE }}:${{ steps.meta.outputs.tag }} 部署流程结束"
|
run: echo "镜像 ${{ env.IMAGE }}:${{ steps.meta.outputs.tag }} 部署流程结束"
|
||||||
|
|
||||||
|
- name: 钉钉通知(成功)
|
||||||
|
if: success()
|
||||||
|
continue-on-error: true
|
||||||
|
env:
|
||||||
|
WEBHOOK: ${{ secrets.ZT_CLOSET_DINGTALK_WEBHOOK }}
|
||||||
|
REPO: ${{ gitea.repository }}
|
||||||
|
REF: ${{ gitea.ref }}
|
||||||
|
SHA: ${{ gitea.sha }}
|
||||||
|
ACTOR: ${{ gitea.actor }}
|
||||||
|
RUN_URL: ${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }}
|
||||||
|
IMAGE_REF: ${{ env.IMAGE }}:${{ steps.meta.outputs.tag }}
|
||||||
|
run: |
|
||||||
|
set -uo pipefail
|
||||||
|
if [ -z "${WEBHOOK:-}" ]; then
|
||||||
|
echo "未配置 ZT_CLOSET_DINGTALK_WEBHOOK,跳过钉钉通知"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
REF_NAME="${REF#refs/heads/}"; REF_NAME="${REF_NAME#refs/tags/}"
|
||||||
|
SHORT_SHA="$(printf '%s' "$SHA" | cut -c1-7)"
|
||||||
|
COST=$(( $(date +%s) - ${JOB_START:-$(date +%s)} ))
|
||||||
|
TEXT="### ✅ 构建并部署成功\n\n"
|
||||||
|
TEXT="${TEXT}> **zt-closet-manage-backend** · 构建并部署\n\n"
|
||||||
|
TEXT="${TEXT}- **📦 仓库**:$REPO\n"
|
||||||
|
TEXT="${TEXT}- **🌿 分支/标签**:$REF_NAME\n"
|
||||||
|
TEXT="${TEXT}- **🔖 提交**:$SHORT_SHA\n"
|
||||||
|
TEXT="${TEXT}- **👤 触发者**:$ACTOR\n"
|
||||||
|
TEXT="${TEXT}- **🐳 镜像**:$IMAGE_REF\n"
|
||||||
|
TEXT="${TEXT}- **⏱️ 耗时**:$((COST/60)) 分 $((COST%60)) 秒\n"
|
||||||
|
TEXT="${TEXT}- **🕐 时间**:$(date '+%Y-%m-%d %H:%M:%S')\n\n"
|
||||||
|
TEXT="${TEXT}[查看运行详情]($RUN_URL)"
|
||||||
|
printf '%s' "{\"msgtype\":\"markdown\",\"markdown\":{\"title\":\"✅ 构建并部署成功 · zt-closet-manage-backend\",\"text\":\"$TEXT\"}}" > /tmp/dingtalk.json
|
||||||
|
RESP="$(curl -sS -X POST "$WEBHOOK" -H 'Content-Type: application/json' -d @/tmp/dingtalk.json)"
|
||||||
|
echo "钉钉响应: $RESP"
|
||||||
|
|
||||||
|
- name: 钉钉通知(失败)
|
||||||
|
if: failure()
|
||||||
|
continue-on-error: true
|
||||||
|
env:
|
||||||
|
WEBHOOK: ${{ secrets.ZT_CLOSET_DINGTALK_WEBHOOK }}
|
||||||
|
REPO: ${{ gitea.repository }}
|
||||||
|
REF: ${{ gitea.ref }}
|
||||||
|
SHA: ${{ gitea.sha }}
|
||||||
|
ACTOR: ${{ gitea.actor }}
|
||||||
|
RUN_URL: ${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }}
|
||||||
|
IMAGE_REF: ${{ env.IMAGE }}:${{ steps.meta.outputs.tag }}
|
||||||
|
run: |
|
||||||
|
set -uo pipefail
|
||||||
|
if [ -z "${WEBHOOK:-}" ]; then
|
||||||
|
echo "未配置 ZT_CLOSET_DINGTALK_WEBHOOK,跳过钉钉通知"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
REF_NAME="${REF#refs/heads/}"; REF_NAME="${REF_NAME#refs/tags/}"
|
||||||
|
SHORT_SHA="$(printf '%s' "$SHA" | cut -c1-7)"
|
||||||
|
COST=$(( $(date +%s) - ${JOB_START:-$(date +%s)} ))
|
||||||
|
TEXT="### ❌ 构建并部署失败\n\n"
|
||||||
|
TEXT="${TEXT}> **zt-closet-manage-backend** · 构建并部署\n\n"
|
||||||
|
TEXT="${TEXT}- **📦 仓库**:$REPO\n"
|
||||||
|
TEXT="${TEXT}- **🌿 分支/标签**:$REF_NAME\n"
|
||||||
|
TEXT="${TEXT}- **🔖 提交**:$SHORT_SHA\n"
|
||||||
|
TEXT="${TEXT}- **👤 触发者**:$ACTOR\n"
|
||||||
|
TEXT="${TEXT}- **🐳 镜像**:$IMAGE_REF\n"
|
||||||
|
TEXT="${TEXT}- **⏱️ 耗时**:$((COST/60)) 分 $((COST%60)) 秒\n"
|
||||||
|
TEXT="${TEXT}- **🕐 时间**:$(date '+%Y-%m-%d %H:%M:%S')\n"
|
||||||
|
TEXT="${TEXT}- **🧭 失败定位**:请点击下方链接查看失败步骤与日志\n\n"
|
||||||
|
TEXT="${TEXT}[查看失败日志]($RUN_URL)"
|
||||||
|
printf '%s' "{\"msgtype\":\"markdown\",\"markdown\":{\"title\":\"❌ 构建并部署失败 · zt-closet-manage-backend\",\"text\":\"$TEXT\"}}" > /tmp/dingtalk.json
|
||||||
|
RESP="$(curl -sS -X POST "$WEBHOOK" -H 'Content-Type: application/json' -d @/tmp/dingtalk.json)"
|
||||||
|
echo "钉钉响应: $RESP"
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ curl http://localhost:8081/ping
|
|||||||
| 推送 `v*` 标签 | 如 `git tag v1.0.0 && git push origin v1.0.0` |
|
| 推送 `v*` 标签 | 如 `git tag v1.0.0 && git push origin v1.0.0` |
|
||||||
| 手动触发 | Gitea 仓库页面 Actions 标签页 → 选择「构建并部署」→ Run workflow |
|
| 手动触发 | Gitea 仓库页面 Actions 标签页 → 选择「构建并部署」→ Run workflow |
|
||||||
|
|
||||||
流程:拉取代码 → 构建镜像(多阶段 Dockerfile,静态编译)→ 推送 `git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend`(tag + latest)→ SSH 部署生产机 → `/ping` 健康检查;**检查失败自动输出日志并回滚到旧镜像**。
|
流程:拉取代码 → 构建镜像(多阶段 Dockerfile,静态编译)→ 推送 `git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend`(tag + latest)→ SSH 部署生产机 → `/ping` 健康检查;**检查失败自动输出日志并回滚到旧镜像**。流程结束后通过钉钉机器人推送**成功/失败通知**(含仓库、分支/标签、提交、触发者、镜像、耗时与运行详情链接)。
|
||||||
|
|
||||||
> 管理后台无数据库迁移命令(`admin_users` 表由服务启动时 GORM AutoMigrate 幂等创建),因此没有业务服务那样的迁移流水线。
|
> 管理后台无数据库迁移命令(`admin_users` 表由服务启动时 GORM AutoMigrate 幂等创建),因此没有业务服务那样的迁移流水线。
|
||||||
|
|
||||||
@@ -253,6 +253,7 @@ curl http://localhost:8081/ping
|
|||||||
| Secret | `ZT_CLOSET_MANAGE_REGISTRY_USER` | Gitea 镜像仓库登录用户名 |
|
| Secret | `ZT_CLOSET_MANAGE_REGISTRY_USER` | Gitea 镜像仓库登录用户名 |
|
||||||
| Secret | `ZT_CLOSET_MANAGE_REGISTRY_TOKEN` | Gitea 镜像仓库登录令牌 |
|
| Secret | `ZT_CLOSET_MANAGE_REGISTRY_TOKEN` | Gitea 镜像仓库登录令牌 |
|
||||||
| Secret | `ZT_CLOSET_MANAGE_PROD_SSH_KEY` | 生产机部署 SSH 私钥 |
|
| Secret | `ZT_CLOSET_MANAGE_PROD_SSH_KEY` | 生产机部署 SSH 私钥 |
|
||||||
|
| Secret | `ZT_CLOSET_DINGTALK_WEBHOOK` | 钉钉机器人 Webhook 完整地址(**与业务服务同名同值**,共用同一机器人;构建部署成功/失败通知;未配置则自动跳过通知) |
|
||||||
| Variable | `ZT_CLOSET_MANAGE_PROD_SSH_HOST` | 生产机 IP/域名 |
|
| Variable | `ZT_CLOSET_MANAGE_PROD_SSH_HOST` | 生产机 IP/域名 |
|
||||||
| Variable | `ZT_CLOSET_MANAGE_PROD_SSH_USER` | 生产机 SSH 用户 |
|
| Variable | `ZT_CLOSET_MANAGE_PROD_SSH_USER` | 生产机 SSH 用户 |
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user