2025/02/28

GitHub Actions 結合Teams Webhook發送通知

再Repo的secrets加入TEAMS_WEBHOOK_URL即可

name: 發送Teams通知

on:
  workflow_dispatch:
  push:
    branches:
      - master

jobs:
  # 通知到 Microsoft Teams
  notify:
    name: 通知到 Microsoft Teams
    runs-on: [ self-hosted, Ubuntu, 24.04 ]
    needs:
      - master
      - dev
    if: always() # 無論成功或失敗都執行
    steps:
      - name: 發送 Microsoft Teams 通知
        run: |
          # 透過 contains() 檢查是否有任何步驟失敗
          if [[ "${{ contains(needs.*.result, 'failure') }}" == "true" ]]; then
            STATUS="❌ **失敗通知** ⚠️\n\n**GitHub Actions 執行失敗!請檢查錯誤記錄。**"
          else
            STATUS="✅ **成功通知** 🚀\n\n**專案已成功部署!**"
          fi

          # 發送 Microsoft Teams 通知
          curl -H 'Content-Type: application/json' \
            -d '{
              "text": "'"$STATUS"'\n\n📂 **倉庫**:[${{ github.repository }}](https://github.com/${{ github.repository }})\n\n📝 **Commit Hash**:[${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }})\n\n👤 **觸發者**:[${{ github.actor }}](https://github.com/${{ github.actor }})\n\n🔗 [查看工作流程](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})"
            }' \
            ${{ secrets.TEAMS_WEBHOOK_URL }}