From bbdf9cc8701c3afa840c4e96acbcbcd653da2c30 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 20:02:10 +0330 Subject: [PATCH 01/14] Add CI/CD pipeline with Docker support - Add Dockerfile for containerizing the bot - Add GitHub Actions workflow for build, release, and deploy - Add docker-compose.yml for easy deployment - Add .env.example with all required environment variables - Support tag-based releases with format v*.*.* --- .dockerignore | 100 ++++++++++++++++++++++++++++++++++++ .env.example | 15 ++++++ .github/workflows/ci-cd.yml | 78 ++++++++++++++++++++++++++++ .idea/.gitignore | 5 ++ Dockerfile | 11 ++++ docker-compose.yml | 18 +++++++ 6 files changed, 227 insertions(+) create mode 100644 .dockerignore create mode 100644 .env.example create mode 100644 .github/workflows/ci-cd.yml create mode 100644 .idea/.gitignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fa8b5bd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,100 @@ +.Python +[Bb]in +[Ii]nclude +[Ll]ib +[Ll]ib64 +[Ll]ocal +[Ss]cripts +pyvenv.cfg +.venv +pip-selfcheck.json +.idea +*.iml +out +gen +*.zwc +*.zwc.old +*zcompdump* +.zsh_history +.zsh_sessions +.zcalc_history +._zinit +.zinit_lstupd +zsdoc/data +.directory_history +/tests/_output/* +!/tests/_output/.gitkeep +__pycache__/ +*.py[cod] +*$py.class +*.so +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST +*.manifest +*.spec +pip-log.txt +pip-delete-this-directory.txt +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ +*.mo +*.pot +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal +instance/ +.webassets-cache +.scrapy +docs/_build/ +.pybuilder/ +target/ +.ipynb_checkpoints +profile_default/ +ipython_config.py +.pdm.toml +__pypackages__/ +celerybeat-schedule +celerybeat.pid +*.sage.py +.env +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +.spyderproject +.spyproject +.ropeproject +/site +.mypy_cache/ +.dmypy.json +dmypy.json +.pyre/ +.pytype/ +cython_debug/ \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8508d84 --- /dev/null +++ b/.env.example @@ -0,0 +1,15 @@ +# Bot Configuration +BOT_TOKEN=your_telegram_bot_token_here +DATABASE_NAME=playlist.db +ADD_TRACK_TIME_WINDOW=60 +LOG_LEVEL=INFO + +# Docker Configuration (for docker-compose) +DOCKER_USERNAME=your_docker_hub_username + +# GitHub Actions Secrets (set these in repository settings > Secrets and variables > Actions) +# DOCKER_USERNAME=your_docker_hub_username +# DOCKER_PASSWORD=your_docker_hub_password_or_token +# SERVER_HOST=your_server_ip_or_hostname +# SERVER_USER=your_server_ssh_username +# SERVER_SSH_KEY=your_private_ssh_key_content diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml new file mode 100644 index 0000000..e222501 --- /dev/null +++ b/.github/workflows/ci-cd.yml @@ -0,0 +1,78 @@ +name: CI/CD Pipeline + +on: + push: + branches: [ main ] + tags: + - "v*.*.*" + pull_request: + branches: [ main ] + +jobs: + build: + runs-on: self-hosted + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build Docker image + run: | + docker buildx build --platform linux/amd64 -t bilbomusicbot:latest . + + - name: Test Docker image + run: | + docker run --rm bilbomusicbot:latest python -c "import bot; print('Import successful')" + + release: + needs: build + runs-on: self-hosted + if: startsWith(github.ref, 'refs/tags/v') + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + run: | + docker buildx build --platform linux/amd64,linux/arm64 \ + -t ${{ secrets.DOCKER_USERNAME }}/bilbomusicbot:latest \ + -t ${{ secrets.DOCKER_USERNAME }}/bilbomusicbot:${{ github.ref_name }} \ + --push . + + - name: Prepare server for deployment + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.SERVER_HOST }} + username: ${{ secrets.SERVER_USER }} + key: ${{ secrets.SERVER_SSH_KEY }} + script: | + cd /opt/bilbomusicbot + docker-compose pull + + deploy: + needs: release + runs-on: self-hosted + if: startsWith(github.ref, 'refs/tags/v') + steps: + - name: Deploy to server + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.SERVER_HOST }} + username: ${{ secrets.SERVER_USER }} + key: ${{ secrets.SERVER_SSH_KEY }} + script: | + cd /opt/bilbomusicbot + docker-compose down + docker-compose up -d + docker system prune -f \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..82447c4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.13-slim + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +CMD ["python", "bot.py"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4e077ba --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +services: + bilbomusicbot: + image: ${DOCKER_USERNAME}/bilbomusicbot:latest + container_name: bilbomusicbot + restart: unless-stopped + environment: + - BOT_TOKEN=${BOT_TOKEN} + volumes: + - ./data:/app/data + networks: + - bilbonet + +networks: + bilbonet: + driver: bridge + +volumes: + data: \ No newline at end of file From 77ee7833a82e811276380d2d0833fd2f489103a0 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 20:52:44 +0330 Subject: [PATCH 02/14] Switch to GitHub-hosted runners for build and release stages --- .github/workflows/ci-cd.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index e222501..4969c04 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -10,7 +10,7 @@ on: jobs: build: - runs-on: self-hosted + runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 @@ -28,7 +28,7 @@ jobs: release: needs: build - runs-on: self-hosted + runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/v') steps: - name: Checkout code From 0cf4c551b277cfb0d8bdbaf07aefc31077f43159 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 20:55:02 +0330 Subject: [PATCH 03/14] Fix: Add --load flag to make Docker image available for testing --- .github/workflows/ci-cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 4969c04..19e6917 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -20,7 +20,7 @@ jobs: - name: Build Docker image run: | - docker buildx build --platform linux/amd64 -t bilbomusicbot:latest . + docker buildx build --platform linux/amd64 -t bilbomusicbot:latest --load . - name: Test Docker image run: | From 4ea570c8b5c27359f5696685f576f0411d3e17fd Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 21:00:28 +0330 Subject: [PATCH 04/14] change the method of getting BOT_TOKEN --- .github/workflows/ci-cd.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 19e6917..ed3da69 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -24,7 +24,14 @@ jobs: - name: Test Docker image run: | - docker run --rm bilbomusicbot:latest python -c "import bot; print('Import successful')" + docker run --rm -e BOT_TOKEN=${{ secrets.BOT_TOKEN }} bilbomusicbot:latest python -c " + try: + import bot + print('✅ Bot module imported successfully') + except Exception as e: + print(f'❌ Import failed: {e}') + exit(1) + " release: needs: build From 2c23b73bace5f668ff2ed2964594fd56c8102e20 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 21:01:36 +0330 Subject: [PATCH 05/14] change the method of getting BOT_TOKEN --- .github/workflows/ci-cd.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index ed3da69..b0617b8 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -27,9 +27,9 @@ jobs: docker run --rm -e BOT_TOKEN=${{ secrets.BOT_TOKEN }} bilbomusicbot:latest python -c " try: import bot - print('✅ Bot module imported successfully') + print('Bot module imported successfully') except Exception as e: - print(f'❌ Import failed: {e}') + print(f'Import failed: {e}') exit(1) " From d1972e4dabc6057d589a2cbd19d2b4dc00df9833 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 21:10:13 +0330 Subject: [PATCH 06/14] ignore BOT_TOKEN --- .github/workflows/ci-cd.yml | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index b0617b8..3757ce1 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -24,13 +24,11 @@ jobs: - name: Test Docker image run: | - docker run --rm -e BOT_TOKEN=${{ secrets.BOT_TOKEN }} bilbomusicbot:latest python -c " - try: - import bot - print('Bot module imported successfully') - except Exception as e: - print(f'Import failed: {e}') - exit(1) + docker run --rm bilbomusicbot:latest python -c " + import sys + print('✅ Python environment working') + print('✅ Dependencies installed') + print('✅ Docker image built successfully') " release: From 131a7867dd85a4d060db3c8dcf06fa23a193bb44 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 21:43:41 +0330 Subject: [PATCH 07/14] Add prod environment reference for secrets access in ci-cd.yml --- .github/workflows/ci-cd.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 3757ce1..0497c03 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -11,6 +11,7 @@ on: jobs: build: runs-on: ubuntu-latest + environment: prod steps: - name: Checkout code uses: actions/checkout@v4 @@ -34,6 +35,7 @@ jobs: release: needs: build runs-on: ubuntu-latest + environment: prod if: startsWith(github.ref, 'refs/tags/v') steps: - name: Checkout code @@ -68,6 +70,7 @@ jobs: deploy: needs: release runs-on: self-hosted + environment: prod if: startsWith(github.ref, 'refs/tags/v') steps: - name: Deploy to server From 7fddcfe8105f29f70f0d516ee423563271a9ad2c Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 21:47:58 +0330 Subject: [PATCH 08/14] Revert ignored BOT_TOKEN --- .github/workflows/ci-cd.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 0497c03..6a31e1c 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -25,12 +25,13 @@ jobs: - name: Test Docker image run: | - docker run --rm bilbomusicbot:latest python -c " - import sys - print('✅ Python environment working') - print('✅ Dependencies installed') - print('✅ Docker image built successfully') - " + docker run --rm -e BOT_TOKEN=${{ secrets.BOT_TOKEN }} bilbomusicbot:latest python -c " + try: + import bot + print('Bot module imported successfully') + except Exception as e: + print(f'Import failed: {e}') + exit(1) release: needs: build From 1beae1e2f8905d8ae349276c5dfcc278e434741e Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 21:52:02 +0330 Subject: [PATCH 09/14] Fix: Add missing quote and prod environment reference --- .github/workflows/ci-cd.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 6a31e1c..05b8772 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -32,6 +32,7 @@ jobs: except Exception as e: print(f'Import failed: {e}') exit(1) + " release: needs: build From 5257325a4930e561ecede09f4877a9bfd664a0b4 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 15 Aug 2025 22:15:01 +0330 Subject: [PATCH 10/14] Fix: Use password authentication instead of key --- .github/workflows/ci-cd.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 05b8772..a5210a9 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -64,7 +64,7 @@ jobs: with: host: ${{ secrets.SERVER_HOST }} username: ${{ secrets.SERVER_USER }} - key: ${{ secrets.SERVER_SSH_KEY }} + password: ${{ secrets.SERVER_SSH_KEY }} script: | cd /opt/bilbomusicbot docker-compose pull @@ -80,7 +80,7 @@ jobs: with: host: ${{ secrets.SERVER_HOST }} username: ${{ secrets.SERVER_USER }} - key: ${{ secrets.SERVER_SSH_KEY }} + password: ${{ secrets.SERVER_SSH_KEY }} script: | cd /opt/bilbomusicbot docker-compose down From 730937ca253e6fde7e301c2ac398c859051de0b0 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 22 Aug 2025 01:46:42 +0330 Subject: [PATCH 11/14] Fix: create dir in hosted-server --- .env.example | 3 --- .github/workflows/ci-cd.yml | 7 +++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 8508d84..f50bc60 100644 --- a/.env.example +++ b/.env.example @@ -4,9 +4,6 @@ DATABASE_NAME=playlist.db ADD_TRACK_TIME_WINDOW=60 LOG_LEVEL=INFO -# Docker Configuration (for docker-compose) -DOCKER_USERNAME=your_docker_hub_username - # GitHub Actions Secrets (set these in repository settings > Secrets and variables > Actions) # DOCKER_USERNAME=your_docker_hub_username # DOCKER_PASSWORD=your_docker_hub_password_or_token diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index a5210a9..813e9a2 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -51,7 +51,9 @@ jobs: with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - + # if your server support ssh key use this format + # key: ${{ secrets.SERVER_SSH_KEY }} + - name: Build and push Docker image run: | docker buildx build --platform linux/amd64,linux/arm64 \ @@ -66,8 +68,9 @@ jobs: username: ${{ secrets.SERVER_USER }} password: ${{ secrets.SERVER_SSH_KEY }} script: | + mkdir -p /opt/bilbomusicbot cd /opt/bilbomusicbot - docker-compose pull + docker pull ${{ secrets.DOCKER_USERNAME }}/bilbomusicbot:latest deploy: needs: release From 81afc43fc97b29c6c49c09477a4dd87e374124c2 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 22 Aug 2025 05:41:42 +0330 Subject: [PATCH 12/14] Fix: Change deploy job to use ubuntu-latest instead of self-hosted runner --- .github/workflows/ci-cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 813e9a2..32c421a 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -74,7 +74,7 @@ jobs: deploy: needs: release - runs-on: self-hosted + runs-on: ubuntu-latest environment: prod if: startsWith(github.ref, 'refs/tags/v') steps: From 5b0fea5688a855045188a0f8a4aebb597fe9c698 Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 22 Aug 2025 05:53:48 +0330 Subject: [PATCH 13/14] Fix: Use 'docker compose' instead of 'docker-compose' command --- .github/workflows/ci-cd.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 32c421a..7b582be 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -86,6 +86,6 @@ jobs: password: ${{ secrets.SERVER_SSH_KEY }} script: | cd /opt/bilbomusicbot - docker-compose down - docker-compose up -d + docker compose down + docker compose up -d docker system prune -f \ No newline at end of file From 31a12a5df9efce21ff80de9f266305afcefdc9dc Mon Sep 17 00:00:00 2001 From: Art4Man Date: Fri, 22 Aug 2025 06:08:17 +0330 Subject: [PATCH 14/14] Fix: Copy docker-compose.yml to server and set environment variables --- .github/workflows/ci-cd.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 7b582be..50a1136 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -61,6 +61,15 @@ jobs: -t ${{ secrets.DOCKER_USERNAME }}/bilbomusicbot:${{ github.ref_name }} \ --push . + - name: Copy docker-compose.yml to server + uses: appleboy/scp-action@v0.1.7 + with: + host: ${{ secrets.SERVER_HOST }} + username: ${{ secrets.SERVER_USER }} + password: ${{ secrets.SERVER_SSH_KEY }} + source: "docker-compose.yml" + target: "/opt/bilbomusicbot/" + - name: Prepare server for deployment uses: appleboy/ssh-action@v1.0.3 with: @@ -86,6 +95,8 @@ jobs: password: ${{ secrets.SERVER_SSH_KEY }} script: | cd /opt/bilbomusicbot + export DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }} + export BOT_TOKEN=${{ secrets.BOT_TOKEN }} docker compose down docker compose up -d docker system prune -f \ No newline at end of file