Compare commits
6 Commits
937c54ecfb
...
dev-15
| Author | SHA1 | Date | |
|---|---|---|---|
| 24751caeb4 | |||
| cab85ecbf5 | |||
| a5d221ecad | |||
| 2ee6ea2b38 | |||
| 118189da82 | |||
| d963ea83ad |
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@@ -4,7 +4,7 @@ on:
|
||||
push:
|
||||
branches: [ 'dev-*', 'feature-*', 'fix-*' ]
|
||||
pull_request:
|
||||
branches: [ 'dev-*', 'feature-*', 'fix-*', 'main' ]
|
||||
branches: [ 'dev-*', 'feature-*', 'fix-*', 'master' ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
@@ -72,7 +72,7 @@ jobs:
|
||||
|
||||
✅ All tests passed! Code quality checks completed successfully.
|
||||
|
||||
🔗 View details: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
🔗 View details: ${{ vars.GITEA_PUBLIC_URL || github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
continue-on-error: true
|
||||
|
||||
- name: Send test failure notification
|
||||
@@ -91,5 +91,5 @@ jobs:
|
||||
|
||||
❌ Tests failed! Deployment blocked. Please fix the issues and try again.
|
||||
|
||||
🔗 View details: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
🔗 View details: ${{ vars.GITEA_PUBLIC_URL || github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
continue-on-error: true
|
||||
87
.github/workflows/deploy.yml
vendored
87
.github/workflows/deploy.yml
vendored
@@ -2,7 +2,7 @@ name: Deploy to Production
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
branches: [ master ]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
action:
|
||||
@@ -17,10 +17,13 @@ on:
|
||||
required: false
|
||||
type: string
|
||||
dry_run:
|
||||
description: 'Dry run (deploy only — no SSH, just show what would run)'
|
||||
required: false
|
||||
type: boolean
|
||||
default: false
|
||||
description: 'Dry run (only for deploy — no SSH, only show planned steps)'
|
||||
required: true
|
||||
type: choice
|
||||
default: no
|
||||
options:
|
||||
- no
|
||||
- yes
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
@@ -30,7 +33,7 @@ jobs:
|
||||
github.event_name == 'push' ||
|
||||
(github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'deploy')
|
||||
env:
|
||||
DRY_RUN: ${{ github.event.inputs.dry_run == 'true' }}
|
||||
DRY_RUN: ${{ github.event.inputs.dry_run == 'yes' }}
|
||||
concurrency:
|
||||
group: production-deploy-telegram-helper-bot
|
||||
cancel-in-progress: false
|
||||
@@ -41,17 +44,17 @@ jobs:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: main
|
||||
ref: master
|
||||
|
||||
- name: Dry run (simulate deploy steps)
|
||||
if: github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true'
|
||||
if: github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'yes'
|
||||
run: |
|
||||
echo "🔍 DRY RUN — no SSH, no changes on server"
|
||||
echo "Would run on server:"
|
||||
echo " 1. cd /home/prod/bots/telegram-helper-bot"
|
||||
echo " 2. CURRENT_COMMIT=\$(git rev-parse HEAD); write to .deploy_history_telegram_helper_bot.txt"
|
||||
echo " 3. git fetch origin main && git reset --hard origin/main"
|
||||
echo " 4. python3 scripts/apply_migrations.py --db ... (if DB exists)"
|
||||
echo " 2. Backup DB → database/tg-bot-database_YYYYMMDD-HHMMSS.db (удаляется при успехе)"
|
||||
echo " 3. CURRENT_COMMIT + history; git fetch origin master && git reset --hard origin/master"
|
||||
echo " 4. apply_migrations.py (бэкап БД делается в шаге 1, при успехе удаляется в конце)"
|
||||
echo " 5. docker-compose -f /home/prod/docker-compose.yml config (validate)"
|
||||
echo " 6. docker-compose stop telegram-bot; build --pull telegram-bot; up -d telegram-bot"
|
||||
echo " 7. sleep 10; check container bots_telegram_bot"
|
||||
@@ -62,7 +65,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Deploy to server
|
||||
if: github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true'
|
||||
if: github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'yes'
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
with:
|
||||
host: ${{ vars.SERVER_HOST || secrets.SERVER_HOST }}
|
||||
@@ -76,9 +79,21 @@ jobs:
|
||||
|
||||
echo "🚀 Starting deployment to production..."
|
||||
|
||||
DB_PATH="/home/prod/bots/telegram-helper-bot/database/tg-bot-database.db"
|
||||
DB_DIR="/home/prod/bots/telegram-helper-bot/database"
|
||||
BACKUP_FILE=""
|
||||
|
||||
sudo chown -R deploy:deploy /home/prod/bots/telegram-helper-bot || true
|
||||
cd /home/prod/bots/telegram-helper-bot
|
||||
|
||||
# Бэкап БД в самом начале; при успешном деплое удалим в конце
|
||||
if [ -f "$DB_PATH" ]; then
|
||||
echo "💾 Creating database backup (before any changes)..."
|
||||
BACKUP_NAME="tg-bot-database_$(date +%Y%m%d-%H%M%S).db"
|
||||
BACKUP_FILE="${DB_DIR}/${BACKUP_NAME}"
|
||||
cp "$DB_PATH" "$BACKUP_FILE" && echo "✅ Backup: $BACKUP_FILE" || { echo "❌ Backup failed!"; exit 1; }
|
||||
fi
|
||||
|
||||
# Сохраняем информацию о коммите (до pull) — из репо telegram-helper-bot
|
||||
CURRENT_COMMIT=$(git rev-parse HEAD)
|
||||
COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s" || echo "Unknown")
|
||||
@@ -96,21 +111,24 @@ jobs:
|
||||
tail -n "$HISTORY_SIZE" "$HISTORY_FILE" > "${HISTORY_FILE}.tmp" && mv "${HISTORY_FILE}.tmp" "$HISTORY_FILE"
|
||||
|
||||
# Обновляем код
|
||||
echo "📥 Pulling latest changes from main..."
|
||||
git fetch origin main
|
||||
git reset --hard origin/main
|
||||
echo "📥 Pulling latest changes from master..."
|
||||
git fetch origin master
|
||||
git reset --hard origin/master
|
||||
sudo chown -R deploy:deploy /home/prod/bots/telegram-helper-bot || true
|
||||
|
||||
NEW_COMMIT=$(git rev-parse HEAD)
|
||||
echo "✅ Code updated: $CURRENT_COMMIT → $NEW_COMMIT"
|
||||
|
||||
# Применяем миграции БД перед перезапуском контейнера
|
||||
# Применяем миграции БД (нужен venv с зависимостями: aiosqlite и др.)
|
||||
echo "🔄 Applying database migrations..."
|
||||
DB_PATH="/home/prod/bots/telegram-helper-bot/database/tg-bot-database.db"
|
||||
|
||||
if [ -f "$DB_PATH" ]; then
|
||||
cd /home/prod/bots/telegram-helper-bot
|
||||
python3 scripts/apply_migrations.py --db "$DB_PATH" || {
|
||||
if [ ! -d .venv ]; then
|
||||
echo "📦 Creating .venv for migrations..."
|
||||
python3 -m venv .venv
|
||||
fi
|
||||
.venv/bin/pip install -q -r requirements.txt
|
||||
.venv/bin/python scripts/apply_migrations.py --db "$DB_PATH" || {
|
||||
echo "❌ Ошибка при применении миграций!"
|
||||
exit 1
|
||||
}
|
||||
@@ -152,6 +170,12 @@ jobs:
|
||||
|
||||
if docker ps | grep -q bots_telegram_bot; then
|
||||
echo "✅ Container is running"
|
||||
# Успешный деплой — удаляем бэкап (при ошибке на любом шаге бэкап остаётся для rollback)
|
||||
if [ -n "${BACKUP_FILE:-}" ] && [ -f "$BACKUP_FILE" ]; then
|
||||
rm -f "$BACKUP_FILE" && echo "✅ Backup removed (deploy success)"
|
||||
fi
|
||||
# Удаляем остановленные контейнеры, чтобы не копились после stop/recreate
|
||||
docker container prune -f && echo "✅ Stopped containers pruned" || true
|
||||
else
|
||||
echo "❌ Container failed to start!"
|
||||
docker logs bots_telegram_bot --tail 50 || true
|
||||
@@ -189,13 +213,13 @@ jobs:
|
||||
${{ job.status == 'success' && '✅' || '❌' }} Deployment: ${{ job.status }}
|
||||
|
||||
📦 Repository: telegram-helper-bot
|
||||
🌿 Branch: main
|
||||
🌿 Branch: master
|
||||
📝 Commit: ${{ github.sha }}
|
||||
👤 Author: ${{ github.actor }}
|
||||
|
||||
${{ job.status == 'success' && '✅ Deployment successful! Container restarted with migrations applied.' || '❌ Deployment failed! Check logs for details.' }}
|
||||
|
||||
🔗 View details: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
🔗 View details: ${{ vars.GITEA_PUBLIC_URL || github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
continue-on-error: true
|
||||
|
||||
- name: Get PR body from merged PR
|
||||
@@ -203,16 +227,20 @@ jobs:
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
if ! command -v gh >/dev/null 2>&1; then
|
||||
echo "⏭️ gh CLI not installed in runner, skipping PR body extraction"
|
||||
exit 0
|
||||
fi
|
||||
echo "🔍 Searching for merged PR associated with commit ${{ github.sha }}..."
|
||||
|
||||
# Находим последний мерженный PR для main ветки по merge commit SHA
|
||||
# Находим последний мерженный PR для master по merge commit SHA
|
||||
COMMIT_SHA="${{ github.sha }}"
|
||||
PR_NUMBER=$(gh pr list --state merged --base main --limit 10 --json number,mergeCommit --jq ".[] | select(.mergeCommit.oid == \"$COMMIT_SHA\") | .number" | head -1)
|
||||
PR_NUMBER=$(gh pr list --state merged --base master --limit 10 --json number,mergeCommit --jq ".[] | select(.mergeCommit.oid == \"$COMMIT_SHA\") | .number" | head -1)
|
||||
|
||||
# Если не нашли по merge commit, ищем последний мерженный PR
|
||||
if [ -z "$PR_NUMBER" ]; then
|
||||
echo "⚠️ PR not found by merge commit, trying to get latest merged PR..."
|
||||
PR_NUMBER=$(gh pr list --state merged --base main --limit 1 --json number --jq '.[0].number')
|
||||
PR_NUMBER=$(gh pr list --state merged --base master --limit 1 --json number --jq '.[0].number')
|
||||
fi
|
||||
|
||||
if [ -n "$PR_NUMBER" ] && [ "$PR_NUMBER" != "null" ]; then
|
||||
@@ -244,7 +272,7 @@ jobs:
|
||||
|
||||
${{ env.PR_BODY }}
|
||||
|
||||
🔗 PR: ${{ github.server_url }}/${{ github.repository }}/pull/${{ env.PR_NUMBER }}
|
||||
🔗 PR: ${{ vars.GITEA_PUBLIC_URL || github.server_url }}/${{ github.repository }}/pull/${{ env.PR_NUMBER }}
|
||||
📝 Commit: ${{ github.sha }}
|
||||
continue-on-error: true
|
||||
|
||||
@@ -261,7 +289,7 @@ jobs:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: main
|
||||
ref: master
|
||||
|
||||
- name: Rollback on server
|
||||
uses: appleboy/ssh-action@v1.0.0
|
||||
@@ -319,7 +347,7 @@ jobs:
|
||||
|
||||
# Откатываем код
|
||||
echo "🔄 Rolling back code..."
|
||||
git fetch origin main
|
||||
git fetch origin master
|
||||
git reset --hard "$ROLLBACK_COMMIT"
|
||||
|
||||
# Исправляем права после отката
|
||||
@@ -360,6 +388,7 @@ jobs:
|
||||
tail -n "$HISTORY_SIZE" "$HISTORY_FILE" > "${HISTORY_FILE}.tmp" && mv "${HISTORY_FILE}.tmp" "$HISTORY_FILE"
|
||||
|
||||
echo "✅ Rollback completed successfully"
|
||||
docker container prune -f && echo "✅ Stopped containers pruned" || true
|
||||
|
||||
- name: Send rollback notification
|
||||
if: always()
|
||||
@@ -371,12 +400,12 @@ jobs:
|
||||
${{ job.status == 'success' && '🔄' || '❌' }} Rollback: ${{ job.status }}
|
||||
|
||||
📦 Repository: telegram-helper-bot
|
||||
🌿 Branch: main
|
||||
🌿 Branch: master
|
||||
📝 Rolled back to: ${{ github.event.inputs.rollback_commit || 'Last successful commit' }}
|
||||
👤 Triggered by: ${{ github.actor }}
|
||||
|
||||
${{ job.status == 'success' && '✅ Rollback completed successfully! Services restored to previous version.' || '❌ Rollback failed! Check logs for details.' }}
|
||||
|
||||
🔗 View details: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
🔗 View details: ${{ vars.GITEA_PUBLIC_URL || github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
continue-on-error: true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user