Refactor project structure and update configurations for State Corp backend

- Updated project description in __init__.py
- Enhanced .gitignore to exclude additional data files
- Modified User model to remove first_name and last_name fields
- Improved instance save method in services.py to include updated_at field
- Added API tokens to .env.example for external services
- Cleaned up test files for better readability
- Updated Dockerfile and docker-compose.yml for improved setup
- Revised README.md to reflect project changes and added changelog
This commit is contained in:
2026-02-17 09:24:42 +01:00
parent e9d7f24aaa
commit fd2adf9ab4
31 changed files with 1419 additions and 933 deletions

View File

@@ -2,9 +2,12 @@ name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
branches: [ main, develop, dev ]
pull_request:
branches: [ main, develop ]
branches: [ main, develop, dev ]
env:
PYTHON_VERSION: "3.11"
jobs:
lint:
@@ -13,110 +16,72 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install uv
run: |
REPO_URL=$(echo ${GITHUB_SERVER_URL} | sed "s|://|://oauth2:${{ gitea.token }}@|")
git clone --depth=1 --branch=${GITHUB_REF_NAME} ${REPO_URL}/${GITHUB_REPOSITORY}.git .
git checkout ${GITHUB_SHA}
- name: Install Python and uv
run: |
apt-get update && apt-get install -y software-properties-common
add-apt-repository -y ppa:deadsnakes/ppa
apt-get update && apt-get install -y python3.11 python3.11-venv
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.local/bin" >> $GITHUB_PATH
export PATH="$HOME/.local/bin:$PATH"
- name: Create virtual environment
run: uv venv
- name: Activate virtual environment and install dependencies
- name: Create virtual environment and install dependencies
run: |
export PATH="$HOME/.local/bin:$PATH"
uv venv --python python3.11
source .venv/bin/activate
uv sync --dev
- name: Run Ruff linting
run: |
export PATH="$HOME/.local/bin:$PATH"
source .venv/bin/activate
ruff check .
ruff check src/
- name: Run Ruff formatting check
run: |
export PATH="$HOME/.local/bin:$PATH"
source .venv/bin/activate
ruff format . --check
ruff format src/ --check
test:
name: Run Tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15.10
env:
POSTGRES_DB: test_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:7-alpine
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install uv
run: |
REPO_URL=$(echo ${GITHUB_SERVER_URL} | sed "s|://|://oauth2:${{ gitea.token }}@|")
git clone --depth=1 --branch=${GITHUB_REF_NAME} ${REPO_URL}/${GITHUB_REPOSITORY}.git .
git checkout ${GITHUB_SHA}
- name: Install Python and uv
run: |
apt-get update && apt-get install -y software-properties-common
add-apt-repository -y ppa:deadsnakes/ppa
apt-get update && apt-get install -y python3.11 python3.11-venv
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.local/bin" >> $GITHUB_PATH
export PATH="$HOME/.local/bin:$PATH"
- name: Create virtual environment
run: uv venv
- name: Activate virtual environment and install dependencies
- name: Create virtual environment and install dependencies
run: |
export PATH="$HOME/.local/bin:$PATH"
uv venv --python python3.11
source .venv/bin/activate
uv sync --dev
- name: Wait for services to be ready
run: |
# Wait for PostgreSQL
until pg_isready -h localhost -p 5432 -U postgres; do
echo "Waiting for PostgreSQL..."
sleep 2
done
# Wait for Redis
until redis-cli -h localhost -p 6379 ping; do
echo "Waiting for Redis..."
sleep 2
done
- name: Run Django tests
run: |
export PATH="$HOME/.local/bin:$PATH"
source .venv/bin/activate
cd src
python manage.py test --verbosity=2
export PYTHONPATH="${PWD}/src:${PYTHONPATH}"
python src/manage.py test tests --verbosity=2
env:
DJANGO_SETTINGS_MODULE: config.settings.development
DATABASE_URL: postgres://postgres:postgres@localhost:5432/test_db
REDIS_URL: redis://localhost:6379/0
CELERY_BROKER_URL: redis://localhost:6379/0
DJANGO_SETTINGS_MODULE: config.settings.test
SECRET_KEY: test-secret-key-for-ci
build:
@@ -126,122 +91,135 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata for web image
id: meta-web
uses: docker/metadata-action@v5
with:
images: |
${{ github.repository_owner }}/mostovik-web
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
- name: Extract metadata for celery image
id: meta-celery
uses: docker/metadata-action@v5
with:
images: |
${{ github.repository_owner }}/mostovik-celery
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
run: |
REPO_URL=$(echo ${GITHUB_SERVER_URL} | sed "s|://|://oauth2:${{ gitea.token }}@|")
git clone --depth=1 --branch=${GITHUB_REF_NAME} ${REPO_URL}/${GITHUB_REPOSITORY}.git .
git checkout ${GITHUB_SHA}
- name: Build web image
uses: docker/build-push-action@v5
with:
context: .
file: ./docker/Dockerfile.web
push: false
load: true
tags: ${{ steps.meta-web.outputs.tags }}
labels: ${{ steps.meta-web.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
run: |
BRANCH_TAG=$(echo ${GITHUB_REF_NAME} | sed 's/\//-/g')
SHA_SHORT=$(echo ${GITHUB_SHA} | cut -c1-7)
docker build -f ./docker/Dockerfile.web -t state_corp-web:${BRANCH_TAG} -t state_corp-web:${BRANCH_TAG}-${SHA_SHORT} .
- name: Build celery image
uses: docker/build-push-action@v5
with:
context: .
file: ./docker/Dockerfile.celery
push: false
load: true
tags: ${{ steps.meta-celery.outputs.tags }}
labels: ${{ steps.meta-celery.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
run: |
BRANCH_TAG=$(echo ${GITHUB_REF_NAME} | sed 's/\//-/g')
SHA_SHORT=$(echo ${GITHUB_SHA} | cut -c1-7)
docker build -f ./docker/Dockerfile.celery -t state_corp-celery:${BRANCH_TAG} -t state_corp-celery:${BRANCH_TAG}-${SHA_SHORT} .
push:
name: Push to Gitea Registry
runs-on: ubuntu-latest
needs: [build]
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/dev'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Login to Gitea Container Registry
uses: docker/login-action@v3
with:
registry: ${{ vars.GITEA_REGISTRY_URL }}
username: ${{ secrets.GITEA_USERNAME }}
password: ${{ secrets.GITEA_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata for web image
id: meta-web
uses: docker/metadata-action@v5
with:
images: |
${{ vars.GITEA_REGISTRY_URL }}/${{ github.repository_owner }}/mostovik-web
tags: |
type=ref,event=branch
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Extract metadata for celery image
id: meta-celery
uses: docker/metadata-action@v5
with:
images: |
${{ vars.GITEA_REGISTRY_URL }}/${{ github.repository_owner }}/mostovik-celery
tags: |
type=ref,event=branch
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push web image
uses: docker/build-push-action@v5
with:
context: .
file: ./docker/Dockerfile.web
push: true
tags: ${{ steps.meta-web.outputs.tags }}
labels: ${{ steps.meta-web.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push celery image
uses: docker/build-push-action@v5
with:
context: .
file: ./docker/Dockerfile.celery
push: true
tags: ${{ steps.meta-celery.outputs.tags }}
labels: ${{ steps.meta-celery.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Image digest
run: |
echo "Web image digest: ${{ steps.docker_build_web.outputs.digest }}"
echo "Celery image digest: ${{ steps.docker_build_celery.outputs.digest }}"
REPO_URL=$(echo ${GITHUB_SERVER_URL} | sed "s|://|://oauth2:${{ gitea.token }}@|")
git clone --depth=1 --branch=${GITHUB_REF_NAME} ${REPO_URL}/${GITHUB_REPOSITORY}.git .
git checkout ${GITHUB_SHA}
- name: Build and push images
run: |
# Install crane for pushing to Gitea container registry
curl -sL https://github.com/google/go-containerregistry/releases/download/v0.19.0/go-containerregistry_Linux_x86_64.tar.gz | tar xz crane
chmod +x crane
BRANCH_TAG=$(echo ${GITHUB_REF_NAME} | sed 's/\//-/g')
SHA_SHORT=$(echo ${GITHUB_SHA} | cut -c1-7)
REGISTRY_HOST="10.10.0.10:3000"
REGISTRY="${REGISTRY_HOST}/${{ github.repository_owner }}"
# Debug
echo "Registry: ${REGISTRY_HOST}"
echo "Actor: ${GITHUB_ACTOR}"
# Login to Gitea container registry (HTTP, requires --insecure)
echo "${REGISTRY_PASSWORD}" | ./crane auth login --insecure ${REGISTRY_HOST} -u "${REGISTRY_USER}" --password-stdin
# Build and push web image
docker build -f ./docker/Dockerfile.web -t state_corp-web:local .
docker save state_corp-web:local -o /tmp/web.tar
./crane push --insecure /tmp/web.tar ${REGISTRY}/state_corp-web:${BRANCH_TAG}
./crane push --insecure /tmp/web.tar ${REGISTRY}/state_corp-web:${BRANCH_TAG}-${SHA_SHORT}
if [ "${GITHUB_REF_NAME}" = "main" ]; then
./crane push --insecure /tmp/web.tar ${REGISTRY}/state_corp-web:latest
fi
# Build and push celery image
docker build -f ./docker/Dockerfile.celery -t state_corp-celery:local .
docker save state_corp-celery:local -o /tmp/celery.tar
./crane push --insecure /tmp/celery.tar ${REGISTRY}/state_corp-celery:${BRANCH_TAG}
./crane push --insecure /tmp/celery.tar ${REGISTRY}/state_corp-celery:${BRANCH_TAG}-${SHA_SHORT}
if [ "${GITHUB_REF_NAME}" = "main" ]; then
./crane push --insecure /tmp/celery.tar ${REGISTRY}/state_corp-celery:latest
fi
env:
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_TOKEN }}
- name: Image summary
run: |
echo "Images pushed to 10.10.0.10:3000/${{ github.repository_owner }}/"
deploy:
name: Deploy to Server
runs-on: ubuntu-latest
needs: [push]
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/dev'
steps:
- name: Checkout code
run: |
REPO_URL=$(echo ${GITHUB_SERVER_URL} | sed "s|://|://oauth2:${{ gitea.token }}@|")
git clone --depth=1 --branch=${GITHUB_REF_NAME} ${REPO_URL}/${GITHUB_REPOSITORY}.git .
git checkout ${GITHUB_SHA}
- name: Deploy via SSH
run: |
BRANCH_TAG=$(echo ${GITHUB_REF_NAME} | sed 's/\//-/g')
# Setup SSH (decode base64 key)
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
# Copy docker-compose.prod.yml to server
scp -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no docker-compose.prod.yml ${DEPLOY_USER}@${DEPLOY_HOST}:/opt/state-corp-backend/
# Deploy commands
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_HOST} "
cd /opt/state-corp-backend
# Login to registry (HTTP on internal IP)
echo '${REGISTRY_PASSWORD}' | docker login --username '${REGISTRY_USER}' --password-stdin 10.10.0.10:3000
# Pull new images
export IMAGE_TAG=${BRANCH_TAG}
docker compose -f docker-compose.prod.yml pull web celery_worker celery_beat
# Stop and remove all project containers
docker compose -f docker-compose.prod.yml down --remove-orphans || true
docker rm -f state_corp_db state_corp_redis state_corp_web state_corp_celery_worker state_corp_celery_beat 2>/dev/null || true
# Start services
docker compose -f docker-compose.prod.yml up -d
# Cleanup old images
docker image prune -f
"
echo "Deployed ${BRANCH_TAG} to ${DEPLOY_HOST}"
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 }}