Рефакторинг инфраструктуры и конфигурации проекта
Some checks failed
CI/CD Pipeline / Code Quality Checks (push) Successful in 1m52s
CI/CD Pipeline / Run Tests (push) Failing after 2m2s
CI/CD Pipeline / Build & Push Images (push) Has been skipped

- Перенесена структура Django-конфига в src/core и src/settings

- Унифицирована Docker-сборка и docker-compose для dev/prod

- Добавлены startup-checks (DB/Redis) и обновлены env-шаблоны

- Расширена OpenAPI-документация и ответы API

- Удалены устаревшие deploy/requirements/служебные скрипты

- Обновлены CI/CD, README и тесты
This commit is contained in:
2026-02-18 13:25:01 +01:00
parent 0f4af561de
commit d5d184537f
71 changed files with 1253 additions and 2318 deletions

View File

@@ -61,6 +61,24 @@ jobs:
. .venv/bin/activate
ruff format src --check
- name: Telegram notify (lint failed)
if: failure()
run: |
set -euo pipefail
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
MSG="❌ [mostovik-backend] lint failed
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}"
curl -fsS -X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}"
test:
name: Run Tests
runs-on: ubuntu-latest
@@ -94,7 +112,7 @@ jobs:
- name: Run Django tests
env:
DJANGO_SETTINGS_MODULE: config.settings.test
DJANGO_SETTINGS_MODULE: settings.test
SECRET_KEY: test-secret-key-for-ci
run: |
set -euo pipefail
@@ -102,18 +120,33 @@ jobs:
export PYTHONPATH="${PWD}/src:${PYTHONPATH:-}"
python src/manage.py test tests --verbosity=2
- name: Telegram notify (test failed)
if: failure()
run: |
set -euo pipefail
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
MSG="❌ [mostovik-backend] test failed
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}"
curl -fsS -X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}"
build_push:
name: Build & Push Images
runs-on: ubuntu-latest
needs: [lint, test]
if: |
always() &&
github.event_name == 'push' &&
(github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') &&
(github.ref == 'refs/heads/dev' || (
(needs.lint.result == 'success' || needs.lint.result == 'skipped') &&
(needs.test.result == 'success' || needs.test.result == 'skipped')
))
(needs.lint.result == 'success' || needs.lint.result == 'skipped') &&
(needs.test.result == 'success' || needs.test.result == 'skipped')
steps:
- name: Checkout code
@@ -142,7 +175,11 @@ jobs:
echo "${REGISTRY_PASSWORD}" | ./crane auth login --insecure "${REGISTRY_HOST}" -u "${REGISTRY_USER}" --password-stdin
docker build -f ./docker/Dockerfile.web -t "${WEB_IMAGE}:local" .
docker build \
-f ./docker/Dockerfile \
--target runtime-web \
--build-arg INSTALL_DEV=false \
-t "${WEB_IMAGE}:local" .
docker save "${WEB_IMAGE}:local" -o /tmp/web.tar
./crane push --insecure /tmp/web.tar "${REGISTRY}/${WEB_IMAGE}:${BRANCH_TAG}"
@@ -151,7 +188,11 @@ jobs:
./crane push --insecure /tmp/web.tar "${REGISTRY}/${WEB_IMAGE}:latest"
fi
docker build -f ./docker/Dockerfile.celery -t "${CELERY_IMAGE}:local" .
docker build \
-f ./docker/Dockerfile \
--target runtime-celery \
--build-arg INSTALL_DEV=false \
-t "${CELERY_IMAGE}:local" .
docker save "${CELERY_IMAGE}:local" -o /tmp/celery.tar
./crane push --insecure /tmp/celery.tar "${REGISTRY}/${CELERY_IMAGE}:${BRANCH_TAG}"
@@ -165,110 +206,43 @@ jobs:
REPO_OWNER="${GITHUB_REPOSITORY%%/*}"
echo "Images pushed to ${REGISTRY_HOST}/${REPO_OWNER}/"
deploy_dev:
name: Deploy (dev)
runs-on: ubuntu-latest
needs: [build_push]
if: github.event_name == 'push' && github.ref == 'refs/heads/dev'
concurrency: deploy-dev
environment: dev
steps:
- name: Checkout code
run: |
REPO_URL=$(echo "${GITHUB_SERVER_URL}" | sed "s|://|://oauth2:${{ gitea.token }}@|")
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}"
git clone --depth=1 --branch="${BRANCH}" "${REPO_URL}/${GITHUB_REPOSITORY}.git" .
git checkout "${GITHUB_SHA}"
- name: Deploy via SSH
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_TOKEN }}
- name: Telegram notify (build_push failed)
if: failure()
run: |
set -euo pipefail
BRANCH_TAG=$(echo "${GITHUB_REF_NAME}" | sed 's/\//-/g')
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
mkdir -p ~/.ssh
echo "${DEPLOY_SSH_KEY}" | base64 -d > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H "${DEPLOY_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
MSG="❌ [mostovik-backend] build_push failed
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}
registry=${REGISTRY_HOST}"
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "mkdir -p /opt/mostovik-backend"
scp -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no docker-compose.prod.yml "${DEPLOY_USER}@${DEPLOY_HOST}:/opt/mostovik-backend/"
curl -fsS -X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}"
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "
set -e
# Configure insecure registry if not already configured
if ! grep -q '${REGISTRY_HOST}' /etc/docker/daemon.json 2>/dev/null; then
echo '{\"insecure-registries\": [\"${REGISTRY_HOST}\"]}' > /etc/docker/daemon.json
systemctl restart docker
fi
cd /opt/mostovik-backend
echo '${REGISTRY_PASSWORD}' | docker login --username '${REGISTRY_USER}' --password-stdin ${REGISTRY_HOST}
export IMAGE_TAG=${BRANCH_TAG}
docker compose -f docker-compose.prod.yml pull web celery_worker celery_beat
docker compose -f docker-compose.prod.yml down --remove-orphans || true
docker rm -f mostovik_web mostovik_celery_worker mostovik_celery_beat 2>/dev/null || true
docker compose -f docker-compose.prod.yml up -d
docker image prune -f
"
echo "Deployed ${BRANCH_TAG} to ${DEPLOY_HOST}"
deploy_prod:
name: Deploy (prod)
runs-on: ubuntu-latest
needs: [build_push]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
concurrency: deploy-prod
environment: prod
steps:
- name: Checkout code
run: |
REPO_URL=$(echo "${GITHUB_SERVER_URL}" | sed "s|://|://oauth2:${{ gitea.token }}@|")
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}"
git clone --depth=1 --branch="${BRANCH}" "${REPO_URL}/${GITHUB_REPOSITORY}.git" .
git checkout "${GITHUB_SHA}"
- name: Deploy via SSH
- name: Telegram notify (build_push success)
if: success()
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_TOKEN }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
set -euo pipefail
BRANCH_TAG=$(echo "${GITHUB_REF_NAME}" | sed 's/\//-/g')
if [ -z "${TG_BOT_KEY:-}" ] || [ -z "${TG_CHANNEL:-}" ]; then
echo "TG_BOT_KEY or TG_CHANNEL is not set; skip telegram notification"
exit 0
fi
mkdir -p ~/.ssh
echo "${DEPLOY_SSH_KEY}" | base64 -d > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H "${DEPLOY_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
MSG="✅ [mostovik-backend] image build & push success
branch=${GITHUB_REF_NAME}
sha=${GITHUB_SHA}
actor=${GITHUB_ACTOR}
registry=${REGISTRY_HOST}
commit=${COMMIT_MESSAGE}"
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "mkdir -p /opt/mostovik-backend"
scp -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no docker-compose.prod.yml "${DEPLOY_USER}@${DEPLOY_HOST}:/opt/mostovik-backend/"
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no "${DEPLOY_USER}@${DEPLOY_HOST}" "
set -e
# Configure insecure registry if not already configured
if ! grep -q '${REGISTRY_HOST}' /etc/docker/daemon.json 2>/dev/null; then
echo '{\"insecure-registries\": [\"${REGISTRY_HOST}\"]}' > /etc/docker/daemon.json
systemctl restart docker
fi
cd /opt/mostovik-backend
echo '${REGISTRY_PASSWORD}' | docker login --username '${REGISTRY_USER}' --password-stdin ${REGISTRY_HOST}
export IMAGE_TAG=${BRANCH_TAG}
docker compose -f docker-compose.prod.yml pull web celery_worker celery_beat
docker compose -f docker-compose.prod.yml down --remove-orphans || true
docker rm -f mostovik_web mostovik_celery_worker mostovik_celery_beat 2>/dev/null || true
docker compose -f docker-compose.prod.yml up -d
docker image prune -f
"
echo "Deployed ${BRANCH_TAG} to ${DEPLOY_HOST}"
curl -fsS -X POST "https://api.telegram.org/bot${TG_BOT_KEY}/sendMessage" \
-d "chat_id=${TG_CHANNEL}" \
--data-urlencode "text=${MSG}"