diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..1fec732 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,93 @@ +name: Build & Deploy Bot + +on: + push: + branches: + - master + +jobs: + build-and-push: + name: Build and Push to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Create fake .env file for build + run: | + echo "BOT_TOKEN=fake" > .env + echo "TELEGRAM_API_ROOT=http://localhost:8081" >> .env + echo "TELEGRAM_API_ID=fake" >> .env + echo "TELEGRAM_API_HASH=fake" >> .env + echo "REDIS_PASSWORD=fake" >> .env + echo "REDIS_HOST=redis" >> .env + + - name: Login to Docker Hub + run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin + + - name: Build bot image + run: | + docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/next-downloader-bot:latest -f ./apps/bot/Dockerfile . + + - name: Push bot image to Docker Hub + run: | + docker push ${{ secrets.DOCKERHUB_USERNAME }}/next-downloader-bot:latest + + deploy: + name: Deploy to VPS + needs: build-and-push + runs-on: ubuntu-latest + environment: production + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup SSH key + run: | + mkdir -p ~/.ssh + echo "${{ secrets.VPS_SSH_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -p ${{ secrets.VPS_PORT }} -H ${{ secrets.VPS_HOST }} >> ~/.ssh/known_hosts + + - name: Ensure next-downloader-bot directory exists on VPS + run: | + ssh -i ~/.ssh/id_rsa -p ${{ secrets.VPS_PORT }} -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} "mkdir -p /home/${{ secrets.VPS_USER }}/next-downloader-bot" + + - name: Create real .env file for production + run: | + echo "BOT_TOKEN=${{ secrets.BOT_TOKEN }}" > .env + echo "TELEGRAM_API_ROOT=${{ secrets.TELEGRAM_API_ROOT }}" >> .env + echo "TELEGRAM_API_ID=${{ secrets.TELEGRAM_API_ID }}" >> .env + echo "TELEGRAM_API_HASH=${{ secrets.TELEGRAM_API_HASH }}" >> .env + echo "REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }}" >> .env + echo "REDIS_HOST=${{ secrets.REDIS_HOST }}" >> .env + + - name: Copy .env to VPS via SCP + uses: appleboy/scp-action@master + with: + host: ${{ secrets.VPS_HOST }} + username: ${{ secrets.VPS_USER }} + key: ${{ secrets.VPS_SSH_KEY }} + port: ${{ secrets.VPS_PORT }} + source: '.env' + target: '/home/${{ secrets.VPS_USER }}/next-downloader-bot/' + + - name: Copy docker-compose.yml to VPS via SCP + uses: appleboy/scp-action@master + with: + host: ${{ secrets.VPS_HOST }} + username: ${{ secrets.VPS_USER }} + key: ${{ secrets.VPS_SSH_KEY }} + port: ${{ secrets.VPS_PORT }} + source: 'docker-compose.yml' + target: '/home/${{ secrets.VPS_USER }}/next-downloader-bot/' + + - name: Login and deploy on VPS + run: | + ssh -i ~/.ssh/id_rsa -p ${{ secrets.VPS_PORT }} -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} " + cd /home/${{ secrets.VPS_USER }}/next-downloader-bot && \ + docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_TOKEN }} && \ + docker compose pull && \ + docker compose down && \ + docker compose up -d + " diff --git a/apps/bot/src/config/env.ts b/apps/bot/src/config/env.ts index 5a66275..7f2df13 100644 --- a/apps/bot/src/config/env.ts +++ b/apps/bot/src/config/env.ts @@ -7,7 +7,7 @@ export const envSchema = z.object({ .string() .transform((value) => Number.parseInt(value, 10)) .default('5000'), - REDIS_HOST: z.string(), + REDIS_HOST: z.string().default('redis'), REDIS_PASSWORD: z.string(), REDIS_PORT: z .string() diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 45f938a..4617437 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -33,8 +33,10 @@ services: restart: always depends_on: - redis + - telegram-bot-api telegram-bot-api: + restart: always env_file: - .env image: aiogram/telegram-bot-api:latest diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..473658e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,49 @@ +services: + redis: + env_file: + - .env + image: redis:8-alpine + restart: always + volumes: + - redis-data:/data + command: ['redis-server', '--requirepass', '${REDIS_PASSWORD}'] + deploy: + resources: + limits: + cpus: '0.50' + memory: 512M + reservations: + cpus: '0.25' + memory: 256M + healthcheck: + test: ['CMD', 'redis-cli', 'ping'] + interval: 30s + timeout: 10s + retries: 3 + start_period: 30s + + next-downloader-bot: + image: vchikalkin/next-downloader-bot:latest + env_file: + - .env + restart: always + depends_on: + - redis + - telegram-bot-api + + telegram-bot-api: + restart: always + deploy: + resources: + limits: + cpus: '0.50' + memory: 512M + env_file: + - .env + image: aiogram/telegram-bot-api:latest= + volumes: + - telegram-bot-api-data:/var/lib/telegram-bot-api + +volumes: + telegram-bot-api-data: + redis-data: