From b22efab5dcdd01465b137f0bdeb391722b83d14e Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:23:59 +0530 Subject: [PATCH 01/60] Create dockerfile --- dockerfile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 dockerfile diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..a693100 --- /dev/null +++ b/dockerfile @@ -0,0 +1,14 @@ +# Use official Python image +FROM python:3.10 + +# Set the working directory inside the container +WORKDIR /app + +# Copy all files, including the resources folder +COPY . . + +# Install dependencies +RUN pip install pygame + +# Run the final game script +CMD ["python", "7_final_code_background_music_image.py"] From c25c89ccc3047f48a4a443e0906ba0e1b742ff26 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:24:58 +0530 Subject: [PATCH 02/60] Create main.yml --- .github/workflows/main.yml | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..da38647 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,41 @@ +name: Docker Build and Deploy + +on: + push: + branches: + - main # Runs on push to main branch + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + # Step 1: Checkout the code + - name: Checkout Repository + uses: actions/checkout@v3 + + # Step 2: Set up Docker Buildx (for multi-platform builds) + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + # Step 3: Log in to Docker Hub + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{DOCKER_HUB_USERNAME }} + password: ${{DOCKER_HUB_PASSWORD }} + + # Step 4: Build and push the Docker image + - name: Build and Push Docker Image + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{DOCKER_HUB_USERNAME }}/python-app:test + + # Step 5: Deploy using Docker Compose + - name: Deploy with Docker Compose + run: | + docker-compose down + docker-compose up -d From 3a6da107633d4f24f036166c65cc4634432eeea6 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:31:41 +0530 Subject: [PATCH 03/60] Update main.yml --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index da38647..275de91 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,8 +22,8 @@ jobs: - name: Log in to Docker Hub uses: docker/login-action@v2 with: - username: ${{DOCKER_HUB_USERNAME }} - password: ${{DOCKER_HUB_PASSWORD }} + username: ${{secrets.DOCKER_HUB_USERNAME }} + password: ${{secrets.DOCKER_HUB_PASSWORD }} # Step 4: Build and push the Docker image - name: Build and Push Docker Image @@ -32,7 +32,7 @@ jobs: context: . file: ./Dockerfile push: true - tags: ${{DOCKER_HUB_USERNAME }}/python-app:test + tags: ${{secrets.DOCKER_HUB_USERNAME }}/python-app:test # Step 5: Deploy using Docker Compose - name: Deploy with Docker Compose From 5eb41ec205cc219d1e23ad4aadb99dbacee6f093 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:36:10 +0530 Subject: [PATCH 04/60] Update main.yml From 4a403471a683c45c11b909fb698d66fbca86be55 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:41:21 +0530 Subject: [PATCH 05/60] Update main.yml From d3c7e7f45affcc4eb250bc61e5b678511d180682 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:46:02 +0530 Subject: [PATCH 06/60] Update main.yml --- .github/workflows/main.yml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 275de91..03baf01 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,8 +22,8 @@ jobs: - name: Log in to Docker Hub uses: docker/login-action@v2 with: - username: ${{secrets.DOCKER_HUB_USERNAME }} - password: ${{secrets.DOCKER_HUB_PASSWORD }} + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_PASSWORD }} # Step 4: Build and push the Docker image - name: Build and Push Docker Image @@ -32,9 +32,15 @@ jobs: context: . file: ./Dockerfile push: true - tags: ${{secrets.DOCKER_HUB_USERNAME }}/python-app:test + tags: ${{ secrets.DOCKER_HUB_USERNAME }}/python-app:test - # Step 5: Deploy using Docker Compose + # Step 5: Install Docker Compose (Fix for "docker-compose: command not found") + - name: Install Docker Compose + run: | + sudo apt-get update + sudo apt-get install -y docker-compose + + # Step 6: Deploy using Docker Compose - name: Deploy with Docker Compose run: | docker-compose down From 8fe21ba93990aeeb620440dac8c3b7d8cfe57fba Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:51:00 +0530 Subject: [PATCH 07/60] Create docker-compose.yml --- docker-compose.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3ff048a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3.8' + +services: + python-app: + container_name: python-app-container + image: $DOCKER_HUB_USERNAME/python-app:test # Replace with your Docker Hub username + restart: always + ports: + - "5000:5000" # Adjust if your app runs on a different port + environment: + - ENV=production + volumes: + - .:/app # Mount current directory to container From f294db48a86c1ca1c86221ba4bbeb88b7993a792 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:51:27 +0530 Subject: [PATCH 08/60] Update main.yml From 4253fd5dbd078abe2bc07f0ad02fe3a90dd14aef Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:55:31 +0530 Subject: [PATCH 09/60] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3ff048a..00c406e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: '3.8' services: python-app: container_name: python-app-container - image: $DOCKER_HUB_USERNAME/python-app:test # Replace with your Docker Hub username + image: $secrets.DOCKER_HUB_USERNAME/python-app:test # Replace with your Docker Hub username restart: always ports: - "5000:5000" # Adjust if your app runs on a different port From 641e70cd0e29699cad21fb87a7f72c5b48aefa4d Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 17:55:53 +0530 Subject: [PATCH 10/60] Update main.yml From f24858415a8fedd28b517725810fb2f7666d4d2d Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 18:00:34 +0530 Subject: [PATCH 11/60] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 00c406e..0177c57 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: '3.8' services: python-app: container_name: python-app-container - image: $secrets.DOCKER_HUB_USERNAME/python-app:test # Replace with your Docker Hub username + image: jobinaj/python-app:test # Replace with your Docker Hub username restart: always ports: - "5000:5000" # Adjust if your app runs on a different port From 02a2b0a830052f26e1db6c0c9c5cce22ed6a89f2 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 18:01:04 +0530 Subject: [PATCH 12/60] Update main.yml From 1b04915cf551b2817155a30299d272b704ecbfc4 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 21:02:09 +0530 Subject: [PATCH 13/60] Update main.yml --- .github/workflows/main.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 03baf01..cc9f70e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -42,6 +42,5 @@ jobs: # Step 6: Deploy using Docker Compose - name: Deploy with Docker Compose - run: | - docker-compose down + run: docker-compose up -d From bcb0d6db26c37e3c2979120c4bc532cf962d3eff Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 21:37:05 +0530 Subject: [PATCH 14/60] Update docker-compose.yml --- docker-compose.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0177c57..5d4d859 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,11 +3,12 @@ version: '3.8' services: python-app: container_name: python-app-container - image: jobinaj/python-app:test # Replace with your Docker Hub username + image: jobinaj/python-app:test restart: always ports: - - "5000:5000" # Adjust if your app runs on a different port + - "5000:5000" environment: - ENV=production + working_dir: /app volumes: - - .:/app # Mount current directory to container + - ./7_final_code_background_music_image.py:/app/7_final_code_background_music_image.py From 4fb809de941b755648b06359187fe3960fcab617 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 21:45:45 +0530 Subject: [PATCH 15/60] Update dockerfile --- dockerfile | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/dockerfile b/dockerfile index a693100..bc457da 100644 --- a/dockerfile +++ b/dockerfile @@ -1,14 +1,17 @@ -# Use official Python image -FROM python:3.10 +# Use an official Python image +FROM python:3.9-slim # Set the working directory inside the container WORKDIR /app -# Copy all files, including the resources folder +# Copy all project files into the container COPY . . -# Install dependencies -RUN pip install pygame +# Install required dependencies +RUN pip install -r requirements.txt -# Run the final game script +# Expose a display port if using GUI +ENV DISPLAY=:0 + +# Run the game CMD ["python", "7_final_code_background_music_image.py"] From 1f5cc91ed7e91206890e8295dbd2e3ca5c082d21 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 21:46:26 +0530 Subject: [PATCH 16/60] Update docker-compose.yml --- docker-compose.yml | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 5d4d859..c378622 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,12 @@ version: '3.8' services: - python-app: - container_name: python-app-container - image: jobinaj/python-app:test - restart: always - ports: - - "5000:5000" - environment: - - ENV=production - working_dir: /app + snake-game: + build: ./1_snake_game + container_name: snake_game_container volumes: - - ./7_final_code_background_music_image.py:/app/7_final_code_background_music_image.py + - ./1_snake_game:/app + environment: + - DISPLAY=:0 + stdin_open: true + tty: true From 7ae1e73e9cb1a60818446a4b7f7e24587b965047 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 21:47:37 +0530 Subject: [PATCH 17/60] Create requirements.txt --- 1_snake_game/requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 1_snake_game/requirements.txt diff --git a/1_snake_game/requirements.txt b/1_snake_game/requirements.txt new file mode 100644 index 0000000..0cb7ff1 --- /dev/null +++ b/1_snake_game/requirements.txt @@ -0,0 +1 @@ +pygame From df20f328018085f5a395da8a6533e1f54aee49ec Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:02:42 +0530 Subject: [PATCH 18/60] Update dockerfile --- dockerfile | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/dockerfile b/dockerfile index bc457da..b768b7c 100644 --- a/dockerfile +++ b/dockerfile @@ -1,6 +1,18 @@ # Use an official Python image FROM python:3.9-slim +# Install necessary system dependencies for pygame +RUN apt-get update && apt-get install -y \ + libglib2.0-0 \ + libglib2.0-dev \ + libgthread-2.0-0 \ + libasound2-dev \ + libpulse-dev \ + libx11-6 \ + libxext6 \ + libxrender1 \ + && rm -rf /var/lib/apt/lists/* + # Set the working directory inside the container WORKDIR /app @@ -8,9 +20,9 @@ WORKDIR /app COPY . . # Install required dependencies -RUN pip install -r requirements.txt +RUN pip install --no-cache-dir -r requirements.txt -# Expose a display port if using GUI +# Set environment variable to avoid GUI errors ENV DISPLAY=:0 # Run the game From 704ab64a08b6b8a58d38b25cc27fd8646b5b52ab Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:21:46 +0530 Subject: [PATCH 19/60] Create docker-deploy.yml --- docker-deploy.yml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 docker-deploy.yml diff --git a/docker-deploy.yml b/docker-deploy.yml new file mode 100644 index 0000000..b6ae920 --- /dev/null +++ b/docker-deploy.yml @@ -0,0 +1,38 @@ +name: Build and Deploy Python App + +on: + push: + branches: + - main # Adjust if using a different branch + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + run: | + docker build -t ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test . + docker push ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test + + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Deploy using Docker Compose + run: | + ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.SERVER_IP }} << 'EOF' + docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test + docker-compose up -d --force-recreate + EOF From 99d99f90258309c9a00010fca7bb954666090052 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:22:22 +0530 Subject: [PATCH 20/60] Update docker-compose.yml --- docker-compose.yml | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c378622..13a6149 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,7 @@ -version: '3.8' - +version: '3' services: - snake-game: - build: ./1_snake_game - container_name: snake_game_container - volumes: - - ./1_snake_game:/app - environment: - - DISPLAY=:0 - stdin_open: true - tty: true + python-game: + image: jobinaj/python-snake-game:test + restart: always + ports: + - "5000:5000" From 7c8ce8d0f50b36dffe548bee9d65bbfd4c3dcd4a Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:22:49 +0530 Subject: [PATCH 21/60] Update dockerfile --- dockerfile | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/dockerfile b/dockerfile index b768b7c..4ace6bf 100644 --- a/dockerfile +++ b/dockerfile @@ -1,29 +1,5 @@ -# Use an official Python image FROM python:3.9-slim - -# Install necessary system dependencies for pygame -RUN apt-get update && apt-get install -y \ - libglib2.0-0 \ - libglib2.0-dev \ - libgthread-2.0-0 \ - libasound2-dev \ - libpulse-dev \ - libx11-6 \ - libxext6 \ - libxrender1 \ - && rm -rf /var/lib/apt/lists/* - -# Set the working directory inside the container WORKDIR /app - -# Copy all project files into the container COPY . . - -# Install required dependencies -RUN pip install --no-cache-dir -r requirements.txt - -# Set environment variable to avoid GUI errors -ENV DISPLAY=:0 - -# Run the game -CMD ["python", "7_final_code_background_music_image.py"] +RUN pip install pygame +CMD ["python", "8_final_code_background_music.py"] From 064b8554ef36793ad3f987c200cf162263334637 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:24:15 +0530 Subject: [PATCH 22/60] Update docker-compose.yml From 4109eb3e0a6a402ae5497c92cc97d576334d3bd3 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:31:27 +0530 Subject: [PATCH 23/60] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 13a6149..365ad38 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '3' services: python-game: - image: jobinaj/python-snake-game:test + image: jobinaj/python-app restart: always ports: - "5000:5000" From 05ca0dbe8ebe8c93e3a7be451ea0c7083125bd5f Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:37:23 +0530 Subject: [PATCH 24/60] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 365ad38..13a6149 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '3' services: python-game: - image: jobinaj/python-app + image: jobinaj/python-snake-game:test restart: always ports: - "5000:5000" From be1830e5f7e9094c10b0ac60014a2886f33b6faf Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:39:24 +0530 Subject: [PATCH 25/60] Update docker-compose.yml From dbf5ffd3b6dc4775e51b98937af9376082bdca3b Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:47:19 +0530 Subject: [PATCH 26/60] Update docker-compose.yml From 54870935c2e7d58e412e07958ce8e25ef80b2ead Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:51:36 +0530 Subject: [PATCH 27/60] Update docker-deploy.yml --- docker-deploy.yml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/docker-deploy.yml b/docker-deploy.yml index b6ae920..5396eab 100644 --- a/docker-deploy.yml +++ b/docker-deploy.yml @@ -15,8 +15,14 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 + - name: Debug Secrets (Check if they are available) + run: echo "Docker Username: ${{ secrets.DOCKER_USERNAME }}" + + - name: Enable Docker CLI authentication + run: echo "{ \"credsStore\": \"desktop\" }" > ~/.docker/config.json + - name: Login to Docker Hub - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} @@ -30,9 +36,15 @@ jobs: needs: build runs-on: ubuntu-latest steps: + - name: Set up SSH Key + run: | + echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key + chmod 600 private_key + - name: Deploy using Docker Compose run: | - ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.SERVER_IP }} << 'EOF' + ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' + docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test docker-compose up -d --force-recreate EOF From c7834e0cc2d5023a595205737c5e127cdfe78ce8 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:53:59 +0530 Subject: [PATCH 28/60] Update docker-compose.yml From b85e9e00b69490a579a3f41ccc6839e42ef55dac Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 22:56:13 +0530 Subject: [PATCH 29/60] Update docker-deploy.yml --- docker-deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-deploy.yml b/docker-deploy.yml index 5396eab..7ba9f66 100644 --- a/docker-deploy.yml +++ b/docker-deploy.yml @@ -13,7 +13,7 @@ jobs: uses: actions/checkout@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: Debug Secrets (Check if they are available) run: echo "Docker Username: ${{ secrets.DOCKER_USERNAME }}" From 9d2a92330f3668a000de9ef7a79b5b6acc899d21 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:12:54 +0530 Subject: [PATCH 30/60] Update docker-compose.yml From f38f874ec853770d79db05b5ed725e1860f7d0f5 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:25:33 +0530 Subject: [PATCH 31/60] docker-deploy.yml --- .github/workflows/main.yml | 62 ++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cc9f70e..7ba9f66 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,46 +1,50 @@ -name: Docker Build and Deploy +name: Build and Deploy Python App on: push: branches: - - main # Runs on push to main branch + - main # Adjust if using a different branch jobs: - build-and-deploy: + build: runs-on: ubuntu-latest - steps: - # Step 1: Checkout the code - - name: Checkout Repository + - name: Checkout repository uses: actions/checkout@v3 - # Step 2: Set up Docker Buildx (for multi-platform builds) - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - # Step 3: Log in to Docker Hub - - name: Log in to Docker Hub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKER_HUB_USERNAME }} - password: ${{ secrets.DOCKER_HUB_PASSWORD }} + - name: Debug Secrets (Check if they are available) + run: echo "Docker Username: ${{ secrets.DOCKER_USERNAME }}" - # Step 4: Build and push the Docker image - - name: Build and Push Docker Image - uses: docker/build-push-action@v4 + - name: Enable Docker CLI authentication + run: echo "{ \"credsStore\": \"desktop\" }" > ~/.docker/config.json + + - name: Login to Docker Hub + uses: docker/login-action@v3 with: - context: . - file: ./Dockerfile - push: true - tags: ${{ secrets.DOCKER_HUB_USERNAME }}/python-app:test + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + run: | + docker build -t ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test . + docker push ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test - # Step 5: Install Docker Compose (Fix for "docker-compose: command not found") - - name: Install Docker Compose + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Set up SSH Key run: | - sudo apt-get update - sudo apt-get install -y docker-compose + echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key + chmod 600 private_key - # Step 6: Deploy using Docker Compose - - name: Deploy with Docker Compose - run: - docker-compose up -d + - name: Deploy using Docker Compose + run: | + ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' + docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" + docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test + docker-compose up -d --force-recreate + EOF From b11894b34223cf81dac079666f948eb5661b87c9 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:26:16 +0530 Subject: [PATCH 32/60] Delete docker-deploy.yml --- docker-deploy.yml | 50 ----------------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 docker-deploy.yml diff --git a/docker-deploy.yml b/docker-deploy.yml deleted file mode 100644 index 7ba9f66..0000000 --- a/docker-deploy.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: Build and Deploy Python App - -on: - push: - branches: - - main # Adjust if using a different branch - -jobs: - build: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Debug Secrets (Check if they are available) - run: echo "Docker Username: ${{ secrets.DOCKER_USERNAME }}" - - - name: Enable Docker CLI authentication - run: echo "{ \"credsStore\": \"desktop\" }" > ~/.docker/config.json - - - 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 build -t ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test . - docker push ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test - - deploy: - needs: build - runs-on: ubuntu-latest - steps: - - name: Set up SSH Key - run: | - echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key - chmod 600 private_key - - - name: Deploy using Docker Compose - run: | - ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' - docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" - docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test - docker-compose up -d --force-recreate - EOF From 5c46ac849584f0f9ed3a0df889794418b9e6dd68 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:27:40 +0530 Subject: [PATCH 33/60] Create docker-deploy.yml --- .github/workflows/docker-deploy.yml | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 .github/workflows/docker-deploy.yml diff --git a/.github/workflows/docker-deploy.yml b/.github/workflows/docker-deploy.yml new file mode 100644 index 0000000..7ba9f66 --- /dev/null +++ b/.github/workflows/docker-deploy.yml @@ -0,0 +1,50 @@ +name: Build and Deploy Python App + +on: + push: + branches: + - main # Adjust if using a different branch + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Debug Secrets (Check if they are available) + run: echo "Docker Username: ${{ secrets.DOCKER_USERNAME }}" + + - name: Enable Docker CLI authentication + run: echo "{ \"credsStore\": \"desktop\" }" > ~/.docker/config.json + + - 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 build -t ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test . + docker push ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test + + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Set up SSH Key + run: | + echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key + chmod 600 private_key + + - name: Deploy using Docker Compose + run: | + ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' + docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" + docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test + docker-compose up -d --force-recreate + EOF From 3e72a64466585813dcd5407523679683c94d2954 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:27:55 +0530 Subject: [PATCH 34/60] Delete .github/workflows/main.yml --- .github/workflows/main.yml | 50 -------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 7ba9f66..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: Build and Deploy Python App - -on: - push: - branches: - - main # Adjust if using a different branch - -jobs: - build: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Debug Secrets (Check if they are available) - run: echo "Docker Username: ${{ secrets.DOCKER_USERNAME }}" - - - name: Enable Docker CLI authentication - run: echo "{ \"credsStore\": \"desktop\" }" > ~/.docker/config.json - - - 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 build -t ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test . - docker push ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test - - deploy: - needs: build - runs-on: ubuntu-latest - steps: - - name: Set up SSH Key - run: | - echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key - chmod 600 private_key - - - name: Deploy using Docker Compose - run: | - ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' - docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" - docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test - docker-compose up -d --force-recreate - EOF From f642055ba017f17c553889b895bd990a91bb12c4 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:29:08 +0530 Subject: [PATCH 35/60] Update docker-compose.yml From 62212f012eaba5864258d6bbe5d0d4a213a93480 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:31:28 +0530 Subject: [PATCH 36/60] Update docker-deploy.yml --- .github/workflows/docker-deploy.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/docker-deploy.yml b/.github/workflows/docker-deploy.yml index 7ba9f66..9247bd4 100644 --- a/.github/workflows/docker-deploy.yml +++ b/.github/workflows/docker-deploy.yml @@ -16,10 +16,7 @@ jobs: uses: docker/setup-buildx-action@v3 - name: Debug Secrets (Check if they are available) - run: echo "Docker Username: ${{ secrets.DOCKER_USERNAME }}" - - - name: Enable Docker CLI authentication - run: echo "{ \"credsStore\": \"desktop\" }" > ~/.docker/config.json + run: echo "Secrets are set" - name: Login to Docker Hub uses: docker/login-action@v3 From cabdfc2e86330c930198efa2d33888dba63743e3 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:40:48 +0530 Subject: [PATCH 37/60] Update docker-compose.yml From c3203d51d0c246ee624cacce784908f4e59bd408 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:46:20 +0530 Subject: [PATCH 38/60] Update docker-compose.yml From 3187ff209ac721f397a9136f2600ef9ac79341ed Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Mon, 3 Mar 2025 23:52:24 +0530 Subject: [PATCH 39/60] Update docker-deploy.yml --- .github/workflows/docker-deploy.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/docker-deploy.yml b/.github/workflows/docker-deploy.yml index 9247bd4..783865c 100644 --- a/.github/workflows/docker-deploy.yml +++ b/.github/workflows/docker-deploy.yml @@ -38,6 +38,16 @@ jobs: echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key chmod 600 private_key + - name: Install Docker & Docker Compose on Remote Server + run: | + ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' + sudo apt update + sudo apt install -y docker.io docker-compose + sudo systemctl enable --now docker + sudo usermod -aG docker $USER + newgrp docker + EOF + - name: Deploy using Docker Compose run: | ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' From 1547f7b2a8eddbc0fd897457c390b8a0cefe6cbe Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 00:00:23 +0530 Subject: [PATCH 40/60] Update docker-deploy.yml --- .github/workflows/docker-deploy.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker-deploy.yml b/.github/workflows/docker-deploy.yml index 783865c..8237496 100644 --- a/.github/workflows/docker-deploy.yml +++ b/.github/workflows/docker-deploy.yml @@ -44,14 +44,14 @@ jobs: sudo apt update sudo apt install -y docker.io docker-compose sudo systemctl enable --now docker - sudo usermod -aG docker $USER - newgrp docker EOF - name: Deploy using Docker Compose run: | ssh -o StrictHostKeyChecking=no -i private_key ubuntu@${{ secrets.SERVER_IP }} << 'EOF' + cd /home/ubuntu/python_projects # Navigate to the correct directory docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" docker pull ${{ secrets.DOCKER_USERNAME }}/python-snake-game:test - docker-compose up -d --force-recreate + docker-compose down # Stop any running containers + docker-compose up -d --force-recreate # Start fresh EOF From 0d9d32784b92e76c7841870e1f734eb8f0245e19 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 00:05:57 +0530 Subject: [PATCH 41/60] Update dockerfile --- dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockerfile b/dockerfile index 4ace6bf..bbbb389 100644 --- a/dockerfile +++ b/dockerfile @@ -2,4 +2,4 @@ FROM python:3.9-slim WORKDIR /app COPY . . RUN pip install pygame -CMD ["python", "8_final_code_background_music.py"] +CMD ["python", "7_final_code_background_music.py"] From 9d5bd00cdafe993153048b0e3391fdb34231a028 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 00:15:32 +0530 Subject: [PATCH 42/60] Update dockerfile --- dockerfile | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/dockerfile b/dockerfile index bbbb389..499ea2e 100644 --- a/dockerfile +++ b/dockerfile @@ -1,5 +1,16 @@ FROM python:3.9-slim + WORKDIR /app -COPY . . -RUN pip install pygame -CMD ["python", "7_final_code_background_music.py"] + +# Copy only necessary files +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the `1_snake_game` folder properly +COPY 1_snake_game /app/1_snake_game + +# Set the correct working directory +WORKDIR /app/1_snake_game + +# Run the correct script +CMD ["python", "7_final_code_background_music_image.py"] From 67adaa5307094bf8408f9de9bbaa4f3ba35915fa Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 00:20:15 +0530 Subject: [PATCH 43/60] Create requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0cb7ff1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pygame From c3124584b922cd919191f85021336f99037492b3 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 00:20:36 +0530 Subject: [PATCH 44/60] Delete 1_snake_game/requirements.txt --- 1_snake_game/requirements.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 1_snake_game/requirements.txt diff --git a/1_snake_game/requirements.txt b/1_snake_game/requirements.txt deleted file mode 100644 index 0cb7ff1..0000000 --- a/1_snake_game/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -pygame From 7e28c9418fbcbb92dbc072841cc24c58856a61b4 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 00:21:32 +0530 Subject: [PATCH 45/60] Update docker-compose.yml From 1b721eaa7264dce1ab9eb3ed154b70b3f2f99651 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 10:25:18 +0530 Subject: [PATCH 46/60] Update dockerfile --- dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dockerfile b/dockerfile index 499ea2e..64c8b68 100644 --- a/dockerfile +++ b/dockerfile @@ -6,11 +6,11 @@ WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -# Copy the `1_snake_game` folder properly -COPY 1_snake_game /app/1_snake_game - # Set the correct working directory WORKDIR /app/1_snake_game +# Copy the `1_snake_game` folder properly +COPY 1_snake_game /app/1_snake_game + # Run the correct script CMD ["python", "7_final_code_background_music_image.py"] From e251345baf18b10058406c82f7b2350c056e7f05 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 10:36:08 +0530 Subject: [PATCH 47/60] Update dockerfile --- dockerfile | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dockerfile b/dockerfile index 64c8b68..2d024f0 100644 --- a/dockerfile +++ b/dockerfile @@ -1,7 +1,16 @@ +# Use a slim Python image FROM python:3.9-slim +# Set working directory WORKDIR /app +# Install necessary system libraries (including libgthread) +RUN apt-get update && apt-get install -y \ + libglib2.0-0 \ + libglib2.0-dev \ + libsdl2-mixer-2.0-0 \ + && rm -rf /var/lib/apt/lists/* + # Copy only necessary files COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt From 07b247fcaf72daddc7b51b944a688b0a46d8e972 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 10:39:31 +0530 Subject: [PATCH 48/60] Update dockerfile From 0da36953f498800f331363399fb7bb50c2322364 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 10:47:15 +0530 Subject: [PATCH 49/60] Update dockerfile --- dockerfile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dockerfile b/dockerfile index 2d024f0..e650911 100644 --- a/dockerfile +++ b/dockerfile @@ -4,11 +4,13 @@ FROM python:3.9-slim # Set working directory WORKDIR /app -# Install necessary system libraries (including libgthread) +# Install necessary system libraries (including ALSA and PulseAudio for audio support) RUN apt-get update && apt-get install -y \ libglib2.0-0 \ libglib2.0-dev \ libsdl2-mixer-2.0-0 \ + alsa-utils \ + pulseaudio \ && rm -rf /var/lib/apt/lists/* # Copy only necessary files @@ -21,5 +23,9 @@ WORKDIR /app/1_snake_game # Copy the `1_snake_game` folder properly COPY 1_snake_game /app/1_snake_game +# Set environment variables to fix XDG_RUNTIME_DIR issues +ENV SDL_AUDIODRIVER=pulseaudio +ENV XDG_RUNTIME_DIR=/tmp + # Run the correct script CMD ["python", "7_final_code_background_music_image.py"] From 1845a0bb5cb56e321478de1c0b3eb323c46f6dc2 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 10:49:07 +0530 Subject: [PATCH 50/60] Update dockerfile From 08ff4436d4c7f2d0e8f02012d2c030bda60310c7 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:14:25 +0530 Subject: [PATCH 51/60] Update dockerfile --- dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dockerfile b/dockerfile index e650911..adbd79d 100644 --- a/dockerfile +++ b/dockerfile @@ -14,8 +14,7 @@ RUN apt-get update && apt-get install -y \ && rm -rf /var/lib/apt/lists/* # Copy only necessary files -COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt +run pip install py # Set the correct working directory WORKDIR /app/1_snake_game From f7908011664ba6e9ab0e0b70292eaa6c59f4b8fa Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:17:49 +0530 Subject: [PATCH 52/60] Update dockerfile --- dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dockerfile b/dockerfile index adbd79d..e650911 100644 --- a/dockerfile +++ b/dockerfile @@ -14,7 +14,8 @@ RUN apt-get update && apt-get install -y \ && rm -rf /var/lib/apt/lists/* # Copy only necessary files -run pip install py +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt # Set the correct working directory WORKDIR /app/1_snake_game From 5ffbac7fde61c8c93bca5873b1e5807fe2ff7e26 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:27:18 +0530 Subject: [PATCH 53/60] Update 7_final_code_background_music_image.py --- .../7_final_code_background_music_image.py | 69 +++++++++---------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/1_snake_game/7_final_code_background_music_image.py b/1_snake_game/7_final_code_background_music_image.py index cb4e329..832d570 100644 --- a/1_snake_game/7_final_code_background_music_image.py +++ b/1_snake_game/7_final_code_background_music_image.py @@ -1,4 +1,4 @@ -# Add background image and music +# Add background image and music (Sound Disabled) import pygame from pygame.locals import * @@ -7,6 +7,7 @@ SIZE = 40 BACKGROUND_COLOR = (110, 110, 5) +ENABLE_SOUND = False # Set to True if you want sound class Apple: def __init__(self, parent_screen): @@ -20,8 +21,8 @@ def draw(self): pygame.display.flip() def move(self): - self.x = random.randint(1,24)*SIZE - self.y = random.randint(1,19)*SIZE + self.x = random.randint(1, 24) * SIZE + self.y = random.randint(1, 19) * SIZE class Snake: def __init__(self, parent_screen): @@ -46,12 +47,10 @@ def move_down(self): self.direction = 'down' def walk(self): - # update body - for i in range(self.length-1,0,-1): - self.x[i] = self.x[i-1] - self.y[i] = self.y[i-1] + for i in range(self.length - 1, 0, -1): + self.x[i] = self.x[i - 1] + self.y[i] = self.y[i - 1] - # update head if self.direction == 'left': self.x[0] -= SIZE if self.direction == 'right': @@ -79,8 +78,9 @@ def __init__(self): pygame.init() pygame.display.set_caption("Codebasics Snake And Apple Game") - pygame.mixer.init() - self.play_background_music() + if ENABLE_SOUND: + pygame.mixer.init() + self.play_background_music() self.surface = pygame.display.set_mode((1000, 800)) self.snake = Snake(self.surface) @@ -89,30 +89,28 @@ def __init__(self): self.apple.draw() def play_background_music(self): - pygame.mixer.music.load('resources/bg_music_1.mp3') - pygame.mixer.music.play(-1, 0) + if ENABLE_SOUND: + pygame.mixer.music.load('resources/bg_music_1.mp3') + pygame.mixer.music.play(-1, 0) def play_sound(self, sound_name): - if sound_name == "crash": - sound = pygame.mixer.Sound("resources/crash.mp3") - elif sound_name == 'ding': - sound = pygame.mixer.Sound("resources/ding.mp3") - - pygame.mixer.Sound.play(sound) + if ENABLE_SOUND: + if sound_name == "crash": + sound = pygame.mixer.Sound("resources/crash.mp3") + elif sound_name == 'ding': + sound = pygame.mixer.Sound("resources/ding.mp3") + pygame.mixer.Sound.play(sound) def reset(self): self.snake = Snake(self.surface) self.apple = Apple(self.surface) def is_collision(self, x1, y1, x2, y2): - if x1 >= x2 and x1 < x2 + SIZE: - if y1 >= y2 and y1 < y2 + SIZE: - return True - return False + return x1 >= x2 and x1 < x2 + SIZE and y1 >= y2 and y1 < y2 + SIZE def render_background(self): bg = pygame.image.load("resources/background.jpg") - self.surface.blit(bg, (0,0)) + self.surface.blit(bg, (0, 0)) def play(self): self.render_background() @@ -121,22 +119,20 @@ def play(self): self.display_score() pygame.display.flip() - # snake eating apple scenario if self.is_collision(self.snake.x[0], self.snake.y[0], self.apple.x, self.apple.y): self.play_sound("ding") self.snake.increase_length() self.apple.move() - # snake colliding with itself for i in range(3, self.snake.length): if self.is_collision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i]): self.play_sound('crash') raise "Collision Occurred" def display_score(self): - font = pygame.font.SysFont('arial',30) - score = font.render(f"Score: {self.snake.length}",True,(200,200,200)) - self.surface.blit(score,(850,10)) + font = pygame.font.SysFont('arial', 30) + score = font.render(f"Score: {self.snake.length}", True, (200, 200, 200)) + self.surface.blit(score, (850, 10)) def show_game_over(self): self.render_background() @@ -145,7 +141,8 @@ def show_game_over(self): self.surface.blit(line1, (200, 300)) line2 = font.render("To play again press Enter. To exit press Escape!", True, (255, 255, 255)) self.surface.blit(line2, (200, 350)) - pygame.mixer.music.pause() + if ENABLE_SOUND: + pygame.mixer.music.pause() pygame.display.flip() def run(self): @@ -157,32 +154,28 @@ def run(self): if event.type == KEYDOWN: if event.key == K_ESCAPE: running = False - if event.key == K_RETURN: - pygame.mixer.music.unpause() + if ENABLE_SOUND: + pygame.mixer.music.unpause() pause = False if not pause: if event.key == K_LEFT: self.snake.move_left() - if event.key == K_RIGHT: self.snake.move_right() - if event.key == K_UP: self.snake.move_up() - if event.key == K_DOWN: self.snake.move_down() elif event.type == QUIT: running = False - try: + try: if not pause: self.play() - - except Exception as e: + except Exception: self.show_game_over() pause = True self.reset() @@ -191,4 +184,4 @@ def run(self): if __name__ == '__main__': game = Game() - game.run() \ No newline at end of file + game.run() From 3787aa5c535fe49df4410bcfbeb63eed6f16e0db Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:42:40 +0530 Subject: [PATCH 54/60] Update 7_final_code_background_music_image.py --- .../7_final_code_background_music_image.py | 109 ++++-------------- 1 file changed, 24 insertions(+), 85 deletions(-) diff --git a/1_snake_game/7_final_code_background_music_image.py b/1_snake_game/7_final_code_background_music_image.py index 832d570..428a179 100644 --- a/1_snake_game/7_final_code_background_music_image.py +++ b/1_snake_game/7_final_code_background_music_image.py @@ -1,13 +1,14 @@ -# Add background image and music (Sound Disabled) - import pygame from pygame.locals import * import time import random +from flask import Flask, render_template + +app = Flask(__name__) SIZE = 40 BACKGROUND_COLOR = (110, 110, 5) -ENABLE_SOUND = False # Set to True if you want sound +ENABLE_SOUND = False class Apple: def __init__(self, parent_screen): @@ -29,7 +30,6 @@ def __init__(self, parent_screen): self.parent_screen = parent_screen self.image = pygame.image.load("resources/block.jpg").convert() self.direction = 'down' - self.length = 1 self.x = [40] self.y = [40] @@ -76,8 +76,7 @@ def increase_length(self): class Game: def __init__(self): pygame.init() - pygame.display.set_caption("Codebasics Snake And Apple Game") - + pygame.display.set_caption("Snake Game") if ENABLE_SOUND: pygame.mixer.init() self.play_background_music() @@ -88,100 +87,40 @@ def __init__(self): self.apple = Apple(self.surface) self.apple.draw() - def play_background_music(self): - if ENABLE_SOUND: - pygame.mixer.music.load('resources/bg_music_1.mp3') - pygame.mixer.music.play(-1, 0) - - def play_sound(self, sound_name): - if ENABLE_SOUND: - if sound_name == "crash": - sound = pygame.mixer.Sound("resources/crash.mp3") - elif sound_name == 'ding': - sound = pygame.mixer.Sound("resources/ding.mp3") - pygame.mixer.Sound.play(sound) - - def reset(self): - self.snake = Snake(self.surface) - self.apple = Apple(self.surface) - - def is_collision(self, x1, y1, x2, y2): - return x1 >= x2 and x1 < x2 + SIZE and y1 >= y2 and y1 < y2 + SIZE - - def render_background(self): - bg = pygame.image.load("resources/background.jpg") - self.surface.blit(bg, (0, 0)) - def play(self): - self.render_background() self.snake.walk() self.apple.draw() - self.display_score() - pygame.display.flip() - - if self.is_collision(self.snake.x[0], self.snake.y[0], self.apple.x, self.apple.y): - self.play_sound("ding") - self.snake.increase_length() - self.apple.move() - - for i in range(3, self.snake.length): - if self.is_collision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i]): - self.play_sound('crash') - raise "Collision Occurred" - - def display_score(self): - font = pygame.font.SysFont('arial', 30) - score = font.render(f"Score: {self.snake.length}", True, (200, 200, 200)) - self.surface.blit(score, (850, 10)) - - def show_game_over(self): - self.render_background() - font = pygame.font.SysFont('arial', 30) - line1 = font.render(f"Game is over! Your score is {self.snake.length}", True, (255, 255, 255)) - self.surface.blit(line1, (200, 300)) - line2 = font.render("To play again press Enter. To exit press Escape!", True, (255, 255, 255)) - self.surface.blit(line2, (200, 350)) - if ENABLE_SOUND: - pygame.mixer.music.pause() pygame.display.flip() def run(self): running = True - pause = False - while running: for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_ESCAPE: running = False - if event.key == K_RETURN: - if ENABLE_SOUND: - pygame.mixer.music.unpause() - pause = False - - if not pause: - if event.key == K_LEFT: - self.snake.move_left() - if event.key == K_RIGHT: - self.snake.move_right() - if event.key == K_UP: - self.snake.move_up() - if event.key == K_DOWN: - self.snake.move_down() - + if event.key == K_LEFT: + self.snake.move_left() + if event.key == K_RIGHT: + self.snake.move_right() + if event.key == K_UP: + self.snake.move_up() + if event.key == K_DOWN: + self.snake.move_down() elif event.type == QUIT: running = False + self.play() + time.sleep(.25) - try: - if not pause: - self.play() - except Exception: - self.show_game_over() - pause = True - self.reset() +game = None - time.sleep(.25) +@app.route('/') +def index(): + global game + if game is None: + game = Game() + game.run() + return "Snake Game is Running!" if __name__ == '__main__': - game = Game() - game.run() + app.run(host='0.0.0.0', port=5000) From 2a02d0a149aca1618e1df0abae29b8966ddb8207 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:44:35 +0530 Subject: [PATCH 55/60] Update dockerfile --- dockerfile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dockerfile b/dockerfile index e650911..9605972 100644 --- a/dockerfile +++ b/dockerfile @@ -4,13 +4,14 @@ FROM python:3.9-slim # Set working directory WORKDIR /app -# Install necessary system libraries (including ALSA and PulseAudio for audio support) +# Install necessary system libraries for audio and GUI support RUN apt-get update && apt-get install -y \ libglib2.0-0 \ libglib2.0-dev \ libsdl2-mixer-2.0-0 \ alsa-utils \ pulseaudio \ + xvfb # Add virtual display support && rm -rf /var/lib/apt/lists/* # Copy only necessary files @@ -26,6 +27,7 @@ COPY 1_snake_game /app/1_snake_game # Set environment variables to fix XDG_RUNTIME_DIR issues ENV SDL_AUDIODRIVER=pulseaudio ENV XDG_RUNTIME_DIR=/tmp +ENV DISPLAY=:99 # Use xvfb virtual display -# Run the correct script -CMD ["python", "7_final_code_background_music_image.py"] +# Start the virtual display and run the game +CMD ["bash", "-c", "Xvfb :99 -screen 0 1024x768x16 & python 7_final_code_background_music_image.py"] From fc2f4722f4c05089418ea2136682ac86bc7cddc2 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:46:23 +0530 Subject: [PATCH 56/60] Update dockerfile From da7e060075d5897d6877553744c90b71fcd0575c Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:48:41 +0530 Subject: [PATCH 57/60] Update dockerfile From 0c5101a21d58452786b0ec15c8864a773b94e373 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:50:37 +0530 Subject: [PATCH 58/60] Update dockerfile --- dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dockerfile b/dockerfile index 9605972..7e12e66 100644 --- a/dockerfile +++ b/dockerfile @@ -24,10 +24,10 @@ WORKDIR /app/1_snake_game # Copy the `1_snake_game` folder properly COPY 1_snake_game /app/1_snake_game -# Set environment variables to fix XDG_RUNTIME_DIR issues +# Set environment variables ENV SDL_AUDIODRIVER=pulseaudio ENV XDG_RUNTIME_DIR=/tmp -ENV DISPLAY=:99 # Use xvfb virtual display +ENV DISPLAY=:99 # Start the virtual display and run the game CMD ["bash", "-c", "Xvfb :99 -screen 0 1024x768x16 & python 7_final_code_background_music_image.py"] From b75b612a23493773374d5940a68a80e1c111917f Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:52:32 +0530 Subject: [PATCH 59/60] Update dockerfile --- dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dockerfile b/dockerfile index 7e12e66..8b20a22 100644 --- a/dockerfile +++ b/dockerfile @@ -11,8 +11,8 @@ RUN apt-get update && apt-get install -y \ libsdl2-mixer-2.0-0 \ alsa-utils \ pulseaudio \ - xvfb # Add virtual display support - && rm -rf /var/lib/apt/lists/* + xvfb && \ + rm -rf /var/lib/apt/lists/* # Copy only necessary files COPY requirements.txt . From 497e849878de6a26a657254d90be3d7561576bf2 Mon Sep 17 00:00:00 2001 From: Jobin Aj Date: Tue, 4 Mar 2025 11:57:04 +0530 Subject: [PATCH 60/60] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 0cb7ff1..3785397 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ +flask pygame