Skip to content

Update cd.yml

Update cd.yml #32

Workflow file for this run

name: CD Pipeline
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Backup current branch packages
uses: appleboy/[email protected]
with:
host: 0.tcp.jp.ngrok.io
port: 19772
username: root
key: ${{ secrets.specialtopic }}
script: |
set -e # 當腳本出現錯誤時終止執行
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
if [ ! -d /opt/backupdata ]; then
mkdir -p /opt/backupdata
fi
cd /opt/specialtopic
tar -zcvf /opt/backupdata/backup_$TIMESTAMP.tar.gz .
echo "Backup completed at $TIMESTAMP"
# 清理舊備份
cd /opt/backupdata
ls -tp | grep -v '/$' | tail -n +4 | xargs -I {} rm -- {}
echo "刪除舊的備份,只保留最新的3個"
- name: Deploy via SSH
uses: appleboy/[email protected]
with:
host: 0.tcp.jp.ngrok.io
port: 19772
username: root
key: ${{ secrets.specialtopic }}
script: |
set -e # 當腳本出現錯誤時終止執行
cd /opt/specialtopic
git pull origin main && docker-compose down && docker-compose up -d --build
docker exec nginx nginx -s reload
docker ps
- name: Notify on successful deployment via Telegram
if: success() # 只有在成功時發送通知
run: |
curl -s --data "chat_id=${{ secrets.TELEGRAM_CHAT_ID }}" \
--data "text=🎉 *CD 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=⚠️ *CD Pipeline 失敗!*%0A%0A部署失敗於分支: *${GITHUB_REF}*%0A提交信息: *${GITHUB_SHA}*%0A%0A🔍 請檢查日誌以獲取詳細信息。" \
--data "parse_mode=MarkdownV2" \
"https://api.telegram.org/bot${{ secrets.TELEGRAM_BOT_TOKEN }}/sendMessage"