diff --git a/.gitea/workflows/ai.yml b/.gitea/workflows/ai.yml new file mode 100644 index 0000000..aa4efd3 --- /dev/null +++ b/.gitea/workflows/ai.yml @@ -0,0 +1,78 @@ +name: AI Service CI/CD + +on: + push: + branches: [production] + paths: + - '**' + - '.gitea/workflows/ai.yml' + + pull_request: + branches: [production] + paths: + - '**' + - '.gitea/workflows/ai.yml' + +jobs: + test: + name: Lint & Test + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + python-version: ['3.11'] + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Ubuntu apt mirrors + run: | + sudo tee /etc/apt/sources.list > /dev/null <<'EOF' + deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy main restricted universe multiverse + deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy-updates main restricted universe multiverse + deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy-security main restricted universe multiverse + EOF + sudo apt-get update + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: 3.11 + + - name: Setup Python pip mirrors + run: | + pip config --user set global.index-url https://package-mirror.liara.ir/repository/pypi/simple + pip config --user set global.extra-index-url https://mirror.cdn.ir/repository/pypi/simple + pip config --user set global.trusted-host "package-mirror.liara.ir mirror.cdn.ir mirror2.chabokan.net" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install pytest flake8 + + - name: Run lint + run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + + - name: Run tests + run: pytest -q + + deploy: + name: Deploy AI Service + needs: test + if: gitea.event_name == 'push' && gitea.ref == 'refs/heads/production' + runs-on: ubuntu-latest + + steps: + - name: Deploy via SSH + uses: appleboy/ssh-action@v1 + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USER }} + port: ${{ secrets.SSH_PORT }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + script: | + git pull origin production + docker compose up -d --build diff --git a/.github/workflows/ai.yml b/.github/workflows/ai.yml new file mode 100644 index 0000000..249555d --- /dev/null +++ b/.github/workflows/ai.yml @@ -0,0 +1,120 @@ +name: AI Service CI/CD + +on: + push: + branches: [main] + paths: + - 'ai/**' + - 'ai/.github/workflows/ai.yml' + pull_request: + branches: [main] + paths: + - 'ai/**' + - 'ai/.github/workflows/ai.yml' + +defaults: + run: + working-directory: ai + +jobs: + test: + name: Lint & Test + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + python-version: ['3.11'] + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Ubuntu apt mirrors + run: | + sudo tee /etc/apt/sources.list > /dev/null <<'EOF' + deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy main restricted universe multiverse + deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy-updates main restricted universe multiverse + deb [trusted=yes] https://mirror2.chabokan.net/ubuntu jammy-security main restricted universe multiverse + EOF + sudo apt-get update + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Setup Python pip mirrors + run: | + pip config --user set global.index-url https://package-mirror.liara.ir/repository/pypi/simple + pip config --user set global.extra-index-url https://mirror.cdn.ir/repository/pypi/simple + pip config --user set global.trusted-host "package-mirror.liara.ir mirror.cdn.ir mirror2.chabokan.net" + + - name: Cache pip dependencies + uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-ai-${{ hashFiles('ai/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip-ai- + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install pytest flake8 + + - name: Run lint + run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + + - name: Run tests + run: pytest --tb=short -q + + docker: + name: Build & Push Docker Image + needs: test + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Registry + uses: docker/login-action@v3 + with: + registry: ${{ secrets.DOCKER_REGISTRY }} + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: ./ai + push: true + tags: | + ${{ secrets.DOCKER_REGISTRY }}/ai:latest + ${{ secrets.DOCKER_REGISTRY }}/ai:${{ github.sha }} + build-args: | + APT_MIRROR=mirror2.chabokan.net + cache-from: type=gha + cache-to: type=gha,mode=max + + deploy: + name: Deploy AI Service + needs: docker + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + steps: + - name: Deploy via SSH + uses: appleboy/ssh-action@v1 + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USER }} + port: ${{ secrets.SSH_PORT }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + script: | + cd /opt/myproject/ai + git pull origin main + docker compose pull + docker compose up -d --remove-orphans diff --git a/docker-compose-prod.yaml b/docker-compose-prod.yaml index faa7465..99452e2 100644 --- a/docker-compose-prod.yaml +++ b/docker-compose-prod.yaml @@ -49,7 +49,13 @@ services: restart: unless-stopped web: - build: . + build: + context: . + args: + APT_MIRROR: mirror2.chabokan.net + PIP_INDEX_URL: https://package-mirror.liara.ir/repository/pypi/simple + PIP_EXTRA_INDEX_URL: https://mirror.cdn.ir/repository/pypi/simple + PYTHON_MIRROR: mirror2.chabokan.net container_name: ai-web env_file: - .env @@ -73,7 +79,13 @@ services: - ./logs:/app/logs celery: - build: . + build: + context: . + args: + APT_MIRROR: mirror2.chabokan.net + PIP_INDEX_URL: https://package-mirror.liara.ir/repository/pypi/simple + PIP_EXTRA_INDEX_URL: https://mirror.cdn.ir/repository/pypi/simple + PYTHON_MIRROR: mirror2.chabokan.net container_name: ai-celery command: celery -A config worker -l info env_file: diff --git a/docker-compose.yaml b/docker-compose.yaml index c876e84..889b273 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -52,6 +52,9 @@ services: context: . args: APT_MIRROR: mirror2.chabokan.net + PIP_INDEX_URL: https://package-mirror.liara.ir/repository/pypi/simple + PIP_EXTRA_INDEX_URL: https://mirror.cdn.ir/repository/pypi/simple + PYTHON_MIRROR: mirror2.chabokan.net container_name: ai-web command: ["python", "manage.py", "runserver", "0.0.0.0:8000"] volumes: @@ -79,7 +82,10 @@ services: build: context: . args: - APT_MIRROR: mirror.cdn.ir + APT_MIRROR: mirror2.chabokan.net + PIP_INDEX_URL: https://package-mirror.liara.ir/repository/pypi/simple + PIP_EXTRA_INDEX_URL: https://mirror.cdn.ir/repository/pypi/simple + PYTHON_MIRROR: mirror2.chabokan.net container_name: ai-celery command: celery -A config worker -l info volumes: