Skip to content

Update app.js

Update app.js #41

Workflow file for this run

name: CD Pipeline
on:
push:
branches:
- main
env:
HOST: 0.tcp.jp.ngrok.io
PORT: 13088
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: ${{ env.HOST }}
port: ${{ env.PORT }}
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: ${{ env.HOST }}
port: ${{ env.PORT }}
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🔍查看詳情: [OPEN URL](https://github.com/CYHFREDA/specialtopic/actions/runs/${GITHUB_RUN_ID})" \
--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🔍查看詳情: [OPEN URL](https://github.com/CYHFREDA/specialtopic/actions/runs/${GITHUB_RUN_ID})" \
--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": "🎉 *CD 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": "⚠️ *CD Pipeline 失敗!*\n部署失敗於分支: *'"${GITHUB_REF}"'*\n提交信息: *'"${GITHUB_SHA}"'*\n🔍查看詳情 : [OPEN URL](https://github.com/CYHFREDA/specialtopic/actions/runs/'"${GITHUB_RUN_ID}"')"
}' \
"${{ secrets.MATTERMOST_WEBHOOK_URL }}"