From c3b75a0eb7a9402ea184817fd08f09dc6c1e2617 Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 1 Mar 2026 00:03:31 +0300 Subject: [PATCH 1/4] fix deploy --- .github/workflows/deploy.yml | 41 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 98d39eb..a1dbc1c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -16,6 +16,11 @@ on: description: 'Commit hash to rollback to (optional, uses last successful if empty)' required: false type: string + dry_run: + description: 'Dry run (deploy only — no SSH, just show what would run)' + required: false + type: boolean + default: false jobs: deploy: @@ -24,6 +29,8 @@ jobs: if: | github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.action == 'deploy') + env: + DRY_RUN: ${{ github.event.inputs.dry_run == 'true' }} concurrency: group: production-deploy-telegram-helper-bot cancel-in-progress: false @@ -35,8 +42,27 @@ jobs: uses: actions/checkout@v4 with: ref: main + + - name: Dry run (simulate deploy steps) + if: github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true' + 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 " 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" + echo "" + echo "Secrets/vars required: SERVER_HOST, SERVER_USER, SSH_PRIVATE_KEY, SSH_PORT, TELEGRAM_BOT_TOKEN, TELEGRAM_TEST_BOT_TOKEN" + if [ -f docker-compose.yml ]; then + echo "✅ docker-compose.yml present in repo (validation would run on server from /home/prod)" + fi - name: Deploy to server + if: github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true' uses: appleboy/ssh-action@v1.0.0 with: host: ${{ vars.SERVER_HOST || secrets.SERVER_HOST }} @@ -50,9 +76,10 @@ jobs: echo "🚀 Starting deployment to production..." - cd /home/prod + sudo chown -R deploy:deploy /home/prod/bots/telegram-helper-bot || true + cd /home/prod/bots/telegram-helper-bot - # Сохраняем информацию о коммите + # Сохраняем информацию о коммите (до pull) — из репо telegram-helper-bot CURRENT_COMMIT=$(git rev-parse HEAD) COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s" || echo "Unknown") COMMIT_AUTHOR=$(git log -1 --pretty=format:"%an" || echo "Unknown") @@ -70,8 +97,6 @@ jobs: # Обновляем код echo "📥 Pulling latest changes from main..." - sudo chown -R deploy:deploy /home/prod/bots/telegram-helper-bot || true - cd /home/prod/bots/telegram-helper-bot git fetch origin main git reset --hard origin/main sudo chown -R deploy:deploy /home/prod/bots/telegram-helper-bot || true @@ -134,7 +159,7 @@ jobs: fi - name: Update deploy history - if: always() + if: always() && env.DRY_RUN != 'true' uses: appleboy/ssh-action@v1.0.0 with: host: ${{ vars.SERVER_HOST || secrets.SERVER_HOST }} @@ -155,7 +180,7 @@ jobs: fi - name: Send deployment notification - if: always() + if: always() && env.DRY_RUN != 'true' uses: appleboy/telegram-action@v1.0.0 with: to: ${{ secrets.TELEGRAM_CHAT_ID }} @@ -174,7 +199,7 @@ jobs: continue-on-error: true - name: Get PR body from merged PR - if: job.status == 'success' && github.event_name == 'push' + if: job.status == 'success' && github.event_name == 'push' && env.DRY_RUN != 'true' env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -209,7 +234,7 @@ jobs: continue-on-error: true - name: Send PR body to important logs - if: job.status == 'success' && github.event_name == 'push' && env.PR_BODY != '' + if: job.status == 'success' && github.event_name == 'push' && env.DRY_RUN != 'true' && env.PR_BODY != '' uses: appleboy/telegram-action@v1.0.0 with: to: ${{ secrets.IMPORTANT_LOGS_CHAT }} -- 2.49.1 From d963ea83ad976b5c3b5d16a0e10e93e1c8c2be0b Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 1 Mar 2026 00:14:40 +0300 Subject: [PATCH 2/4] fix deploy --- .github/workflows/deploy.yml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a1dbc1c..7ef4561 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -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 @@ -44,7 +47,7 @@ jobs: ref: main - 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:" @@ -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 }} -- 2.49.1 From 2ee6ea2b3832382909024ffdc01b35e2c08db26f Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 1 Mar 2026 00:20:36 +0300 Subject: [PATCH 3/4] fix ci & deploy --- .github/workflows/ci.yml | 6 +++--- .github/workflows/deploy.yml | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 795ec38..fdf64bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7ef4561..1e57e16 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,7 +2,7 @@ name: Deploy to Production on: push: - branches: [ main ] + branches: [ master ] workflow_dispatch: inputs: action: @@ -44,7 +44,7 @@ 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 == 'yes' @@ -53,7 +53,7 @@ jobs: 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 " 3. git fetch origin master && git reset --hard origin/master" echo " 4. python3 scripts/apply_migrations.py --db ... (if DB exists)" 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" @@ -99,9 +99,9 @@ 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) @@ -192,13 +192,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 @@ -208,14 +208,14 @@ jobs: run: | 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 @@ -247,7 +247,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 @@ -264,7 +264,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 @@ -322,7 +322,7 @@ jobs: # Откатываем код echo "🔄 Rolling back code..." - git fetch origin main + git fetch origin master git reset --hard "$ROLLBACK_COMMIT" # Исправляем права после отката @@ -374,12 +374,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 -- 2.49.1 From a5d221ecad5ecb95e79019763ed3e029d3777e15 Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 1 Mar 2026 00:34:44 +0300 Subject: [PATCH 4/4] fix deploy #3 --- .github/workflows/deploy.yml | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1e57e16..f1973dd 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -52,9 +52,9 @@ jobs: 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 master && git reset --hard origin/master" - 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" @@ -79,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") @@ -107,10 +119,8 @@ jobs: NEW_COMMIT=$(git rev-parse HEAD) echo "✅ Code updated: $CURRENT_COMMIT → $NEW_COMMIT" - # Применяем миграции БД перед перезапуском контейнера + # Применяем миграции БД 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" || { @@ -155,6 +165,10 @@ 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 else echo "❌ Container failed to start!" docker logs bots_telegram_bot --tail 50 || true -- 2.49.1