-
Notifications
You must be signed in to change notification settings - Fork 0
87 lines (77 loc) · 3.59 KB
/
cd.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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 }}"