相信在大厂工作过的人都很有感受,大厂的基建做得都特别好,CI/CD,以及IDE插件,以及各种提效工具;
那么当你到一个新创团队,你是继续忍受massive的研发流程呢,还是学习大厂一样,尽力精进你的研发流程;我觉得人应该有点追求,即使被流放到一个偏远的小岛,也要精致的过好每一天,这是对自己内心的一种负责;
那么如何在Github Actions中部署通知发送到Lark群组里?
核心步骤
- 在 Lark 群组中添加一个「群机器人」,并获取它的 Webhook URL。
- 将 Webhook URL 作为加密secret存储在 GitHub 仓库中。
- 在 GitHub Actions 工作流中,使用
curl
命令或现成的 Action 向这个 URL 发送 POST 请求。
详细操作指南
第一步:在 Lark 群中创建机器人并获取 Webhook
- 打开你想要接收通知的 Lark 群聊。
- 点击群聊右上角的「···」-> 「设置」-> 「群机器人」。
- 点击「添加机器人」,在列表里找到「自定义机器人」。
- 为你的机器人起一个名字(例如:
GitHub Deploy Bot
),并同意协议。
- 创建成功后,最关键的一步:复制生成的
Webhook
地址。这个地址格式类似:https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
- (可选) 你可以设置机器人的头像,并选择「消息卡片」的主题颜色。
第二步:在 GitHub 仓库中配置 Secret
- 进入你的 GitHub 仓库 -> 「Settings」 -> 「Secrets and variables」 -> 「Actions」。
- 点击 「New repository secret」。
- Name 输入:
LARK_WEBHOOK_URL
(推荐,全部大写,下划线分隔)。
- Value 输入:你刚刚从 Lark 复制的那一长串 Webhook URL。
- 点击 「Add secret」 保存。
第三步:编写 GitHub Actions 工作流文件
在你的项目
.github/workflows
目录下创建文件,例如 deploy-notify-lark.yml
。以下是 最推荐、最清晰 的实现方式,使用
curl
命令直接发送:name: "Deploy and Notify to Lark" on: push: branches: [main] workflow_dispatch: jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Run deployment run: echo "Running your actual deployment commands here..." # 例如: npm run deploy, ./deploy.sh, etc. notify: runs-on: ubuntu-latest needs: [deploy] # 依赖于 deploy 作业 if: always() # 无论部署成功失败都发送通知 steps: - name: Notify Lark on Success if: success() # 只有当 deploy 作业成功时运行 run: | curl -X POST \ "${{ secrets.LARK_WEBHOOK_URL }}" \ -H 'Content-Type: application/json' \ -d '{ "msg_type": "interactive", "card": { "elements": [{ "tag": "div", "text": { "content": "🎉 **Deployment Succeeded!**\n**Repository:** ${{ github.repository }}\n**Branch:** ${{ github.ref_name }}\n**Commit:** ${{ github.sha }}\n[View Workflow Run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})", "tag": "lark_md" } }], "header": { "title": { "content": "🚀 Deployment Update", "tag": "plain_text" }, "template": "green" } } }' - name: Notify Lark on Failure if: failure() # 只有当 deploy 作业失败时运行 run: | curl -X POST \ "${{ secrets.LARK_WEBHOOK_URL }}" \ -H 'Content-Type: application/json' \ -d '{ "msg_type": "interactive", "card": { "elements": [{ "tag": "div", "text": { "content": "❌ **Deployment Failed!**\n**Repository:** ${{ github.repository }}\n**Branch:** ${{ github.ref_name }}\n**Commit:** ${{ github.sha }}\n[View Workflow Run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})", "tag": "lark_md" } }], "header": { "title": { "content": "🚨 Deployment Update", "tag": "plain_text" }, "template": "red" } } }'
关键点解释
needs: [deploy]
: 确保notify
作业在deploy
作业完成后才运行。
if: always()
: 确保即使部署失败,通知作业也会执行,告诉你失败的消息。
if: success()
和if: failure()
: 在通知作业内部,根据部署结果发送不同内容和样式的消息卡片。
- 消息格式: 我们使用了 Lark 的
interactive
类型(即消息卡片),并使用了 Markdown 语法 (lark_md
)。header
的template
颜色可以设置为blue
(信息)、green
(成功)、red
(失败)、yellow
(警告)。
- 链接: 消息中包含了指向本次 Workflow Run 的详细日志链接,非常便于快速排查问题。