Update ci.yml #56
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI Pipeline | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Cache Node.js dependencies | |
uses: actions/cache@v3 | |
with: | |
path: ./backend/node_modules | |
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-node- | |
- name: Install dependencies | |
run: npm install | |
working-directory: ./backend | |
- name: Set up Docker | |
uses: docker/setup-buildx-action@v2 | |
- name: Install Docker Compose | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y docker-compose | |
- name: Build and run Docker containers | |
run: | | |
docker-compose up -d --build | |
docker ps | |
- name: Wait for services to be ready | |
run: | | |
sleep 30 # 等待容器啟動 | |
- name: Run backend tests | |
run: | | |
docker exec backend npm test | |
- name: Run control-backend tests | |
run: | | |
docker exec control-backend npm test | |
- name: Shutdown containers | |
run: | | |
docker-compose down | |
docker volume prune -f | |
- name: Notify on successful deployment via Telegram | |
if: success() # 只有在成功時發送通知 | |
run: | | |
curl -s --data "chat_id=${{ secrets.TELEGRAM_CHAT_ID }}" \ | |
--data "text=🎉 *CI Pipeline 成功!*%0A%0A部署成功於分支: *${GITHUB_REF}*%0A提交信息: *${GITHUB_SHA}*%0A%0A🔍 請檢查應用程序運行狀態。" \ | |
--data "parse_mode=MarkdownV2" \ | |
"https://api.telegram.org/bot${{ secrets.TELEGRAM_BOT_TOKEN }}/sendMessage" | |
- name: Notify on failure via Telegram | |
if: failure() # 只有在失敗時發送通知 | |
run: | | |
curl -s --data "chat_id=${{ secrets.TELEGRAM_CHAT_ID }}" \ | |
--data "text=⚠️ *CI Pipeline 失敗!*%0A%0A部署失敗於分支: *${GITHUB_REF}*%0A提交信息: *${GITHUB_SHA}*%0A%0A🔍 請檢查日誌以獲取詳細信息。" \ | |
--data "parse_mode=MarkdownV2" \ | |
"https://api.telegram.org/bot${{ secrets.TELEGRAM_BOT_TOKEN }}/sendMessage" | |
- name: Notify on successful deployment via Mattermost | |
if: success() # 只有在成功時發送通知 | |
run: | | |
curl -X POST -H 'Content-Type: application/json' \ | |
-d '{ | |
"text": "*🎉 CI Pipeline 成功!*\n部署成功於分支: *'"${GITHUB_REF}"'*\n提交信息: *'"${GITHUB_SHA}"'*\n查看詳情: [OPEN URL](https://github.com/CYHFREDA/specialtopic/actions/runs/'"${GITHUB_RUN_ID}"')" | |
}' \ | |
"${{ secrets.MATTERMOST_WEBHOOK_URL }}" | |
- name: Notify on failure via Mattermost | |
if: failure() # 只有在失敗時發送通知 | |
run: | | |
curl -X POST -H 'Content-Type: application/json' \ | |
-d '{ | |
"text": "⚠️ *CI Pipeline 失敗!*\\n\\n部署失敗於分支: *'"${GITHUB_REF}"'*\\n提交信息: *'"${GITHUB_SHA}"'*\\n\\n🔍 請檢查日誌以獲取詳細信息。" | |
}' \ | |
"${{ secrets.MATTERMOST_WEBHOOK_URL }}" |