From c185b62441d483af9f3988cb968793c9a74345d5 Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 17:10:21 +0300 Subject: [PATCH 01/10] test build and deploy --- .gitea/workflows/ci.yml | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 13875f8..c0fc6f3 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -53,6 +53,68 @@ jobs: run: | cd web npm run build + + build-and-deploy: + needs: test-backend + runs-on: ubuntu-latest + if: success() # Запускается только если тесты прошли успешно + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to container registry + uses: docker/login-action@v3 + with: + registry: gitea.example.com # Замените на ваш Gitea registry + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_TOKEN }} + + - name: Build Docker image + id: build + uses: docker/build-push-action@v5 + with: + context: . + push: false + load: true + tags: fastapi-app:latest + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Stop and remove old container + run: | + docker stop fastapi-container || true + docker rm fastapi-container || true + + - name: Run new container + run: | + docker run -d \ + --name fastapi-container \ + -p 8080:8000 \ + --restart unless-stopped \ + fastapi-app:latest + + - name: Wait for container to be ready + run: | + for i in {1..30}; do + if curl -s http://localhost:8080/health > /dev/null; then + echo "Container is ready!" + exit 0 + fi + echo "Waiting for container... ($i/30)" + sleep 2 + done + echo "Container failed to start properly" + docker logs fastapi-container + exit 1 + + - name: Verify deployment + run: | + curl -f http://localhost:8080/ || exit 1 + echo "Deployment successful!" # Явный статус для PR pr-status: -- 2.43.0 From 8874cea21d3f7cfd990c2ae37dcb8fdf74c0a19c Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 17:14:48 +0300 Subject: [PATCH 02/10] fix registry login --- .gitea/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index c0fc6f3..e24da71 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -69,7 +69,7 @@ jobs: - name: Log in to container registry uses: docker/login-action@v3 with: - registry: gitea.example.com # Замените на ваш Gitea registry + registry: git.rlkdev.ru # Замените на ваш Gitea registry username: ${{ secrets.REGISTRY_USERNAME }} password: ${{ secrets.REGISTRY_TOKEN }} -- 2.43.0 From 3359d088e92f747da583f9f54defb9fd59f5d5d2 Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 17:28:32 +0300 Subject: [PATCH 03/10] add dockerfile --- Dockerfile | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fb4dd9d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM python:3.11-slim + +WORKDIR /app + +# Установка системных зависимостей +# RUN apt-get update && apt-get install -y \ +# gcc \ +# && rm -rf /var/lib/apt/lists/* + +# Копирование зависимостей +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Копирование кода приложения +COPY api/ . + +# Создание непривилегированного пользователя +# RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app +# USER appuser + +# Открытие порта +EXPOSE 8000 + +# Команда для запуска приложения +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file -- 2.43.0 From 34640858d84ee5eba058e4dbdd76837131d00fdc Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 17:30:26 +0300 Subject: [PATCH 04/10] fix dockerfile --- Dockerfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index fb4dd9d..6d555c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,13 +7,13 @@ WORKDIR /app # gcc \ # && rm -rf /var/lib/apt/lists/* -# Копирование зависимостей -COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt - # Копирование кода приложения COPY api/ . +# Копирование зависимостей +# COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + # Создание непривилегированного пользователя # RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app # USER appuser -- 2.43.0 From 1ccdd8c66ab876d15f49187ba2da5a4a4f9dde38 Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 17:43:45 +0300 Subject: [PATCH 05/10] fix cd --- .gitea/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index e24da71..49a820c 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -81,8 +81,6 @@ jobs: push: false load: true tags: fastapi-app:latest - cache-from: type=gha - cache-to: type=gha,mode=max - name: Stop and remove old container run: | -- 2.43.0 From edb83e0fadda4e40ca0f29e7525c7118ff94bb5f Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 18:15:01 +0300 Subject: [PATCH 06/10] cache test --- .gitea/workflows/ci.yml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 49a820c..cd1c7dd 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -19,6 +19,14 @@ jobs: uses: actions/setup-python@v5 with: python-version: '3.11' + + - name: Кэширование pip + uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('api/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- - name: Установка зависимостей run: | @@ -73,14 +81,23 @@ jobs: username: ${{ secrets.REGISTRY_USERNAME }} password: ${{ secrets.REGISTRY_TOKEN }} + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ hashFiles('api/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Build Docker image - id: build uses: docker/build-push-action@v5 with: context: . push: false load: true tags: fastapi-app:latest + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache,mode=max - name: Stop and remove old container run: | -- 2.43.0 From 8616f59c956f44a244f1381ffa4bdb71ec5baeff Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 18:18:25 +0300 Subject: [PATCH 07/10] docker fix --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6d555c9..71003ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ WORKDIR /app # && rm -rf /var/lib/apt/lists/* # Копирование кода приложения -COPY api/ . +COPY api/ api/ # Копирование зависимостей # COPY requirements.txt . @@ -22,4 +22,4 @@ RUN pip install --no-cache-dir -r requirements.txt EXPOSE 8000 # Команда для запуска приложения -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file +CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file -- 2.43.0 From b64d49ddaac151fb71ca6635df35640129d0755e Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 18:22:42 +0300 Subject: [PATCH 08/10] Update Dockerfile to reference requirements from the api directory --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 71003ce..c5c1515 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ COPY api/ api/ # Копирование зависимостей # COPY requirements.txt . -RUN pip install --no-cache-dir -r requirements.txt +RUN pip install --no-cache-dir -r api/requirements.txt # Создание непривилегированного пользователя # RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app -- 2.43.0 From 4e8300f6f0faa145cc0d4fcc52894c73207328c7 Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 18:35:27 +0300 Subject: [PATCH 09/10] Refactor CI workflow to improve container readiness check and remove pip caching step - Replaced the pip caching step with a direct installation of dependencies. - Enhanced the container readiness check by implementing a timeout mechanism that waits for application startup logs instead of a fixed number of attempts. --- .gitea/workflows/ci.yml | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index cd1c7dd..fd77fa1 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -19,14 +19,6 @@ jobs: uses: actions/setup-python@v5 with: python-version: '3.11' - - - name: Кэширование pip - uses: actions/cache@v4 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('api/requirements.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - name: Установка зависимостей run: | @@ -114,22 +106,22 @@ jobs: - name: Wait for container to be ready run: | - for i in {1..30}; do - if curl -s http://localhost:8080/health > /dev/null; then - echo "Container is ready!" + timeout=60 + interval=2 + elapsed=0 + while [ $elapsed -lt $timeout ]; do + if docker logs fastapi-container 2>&1 | grep -q "Application startup complete"; then + echo "Application startup detected in logs" exit 0 fi - echo "Waiting for container... ($i/30)" - sleep 2 + echo "Waiting for startup log... ($elapsed/$timeout sec)" + sleep $interval + elapsed=$((elapsed + interval)) done - echo "Container failed to start properly" + echo "Timeout: Application startup not detected in logs" docker logs fastapi-container - exit 1 - - - name: Verify deployment - run: | - curl -f http://localhost:8080/ || exit 1 echo "Deployment successful!" + exit 1 # Явный статус для PR pr-status: -- 2.43.0 From cc14322c12b710741a0da7b3a4c95744aebc0777 Mon Sep 17 00:00:00 2001 From: EremeevRA Date: Thu, 2 Apr 2026 18:50:50 +0300 Subject: [PATCH 10/10] Refactor Dockerfile and CI workflow for improved dependency management and cleanup - Updated Dockerfile to optimize layer caching by copying requirements.txt before application code. - Added caching for pip packages in CI workflow to speed up dependency installation. - Implemented cleanup of dangling Docker images post-deployment. --- .gitea/workflows/ci.yml | 15 ++++++++++++++- Dockerfile | 21 ++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index fd77fa1..84bd4cd 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -20,6 +20,12 @@ jobs: with: python-version: '3.11' + - name: Cache pip packages + uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('api/requirements.txt') }} + - name: Установка зависимостей run: | cd api @@ -112,6 +118,7 @@ jobs: while [ $elapsed -lt $timeout ]; do if docker logs fastapi-container 2>&1 | grep -q "Application startup complete"; then echo "Application startup detected in logs" + echo "Deployment successful!" exit 0 fi echo "Waiting for startup log... ($elapsed/$timeout sec)" @@ -120,8 +127,14 @@ jobs: done echo "Timeout: Application startup not detected in logs" docker logs fastapi-container - echo "Deployment successful!" exit 1 + + - name: Clean up dangling images + run: | + echo "Removing old dangling images..." + docker image prune -f + echo "Current images after cleanup:" + docker images # Явный статус для PR pr-status: diff --git a/Dockerfile b/Dockerfile index c5c1515..c42dd9e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,24 +2,15 @@ FROM python:3.11-slim WORKDIR /app -# Установка системных зависимостей -# RUN apt-get update && apt-get install -y \ -# gcc \ -# && rm -rf /var/lib/apt/lists/* +# 1. Копируем только файл с зависимостями (меняется редко) +COPY api/requirements.txt requirements.txt -# Копирование кода приложения +# 2. Устанавливаем зависимости (слой кэшируется, пока не изменился requirements.txt) +RUN pip install --no-cache-dir -r requirements.txt + +# 3. Копируем весь остальной код (меняется часто) COPY api/ api/ -# Копирование зависимостей -# COPY requirements.txt . -RUN pip install --no-cache-dir -r api/requirements.txt - -# Создание непривилегированного пользователя -# RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app -# USER appuser - -# Открытие порта EXPOSE 8000 -# Команда для запуска приложения CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file -- 2.43.0