Slight Changes in Dockerfile #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI/CD Pipeline | |
| on: | |
| push: | |
| branches: [ main, develop ] | |
| pull_request: | |
| branches: [ main ] | |
| env: | |
| PYTHON_VERSION: '3.11' | |
| NODE_VERSION: '18' | |
| jobs: | |
| test-backend: | |
| name: Test Backend | |
| runs-on: ubuntu-latest | |
| services: | |
| postgres: | |
| image: postgres:15 | |
| env: | |
| POSTGRES_PASSWORD: postgres | |
| POSTGRES_USER: postgres | |
| POSTGRES_DB: test_db | |
| options: >- | |
| --health-cmd pg_isready | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| ports: | |
| - 5432:5432 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: ${{ env.PYTHON_VERSION }} | |
| - name: Install UV (fast Python package installer) | |
| run: | | |
| curl -LsSf https://astral.sh/uv/install.sh | sh | |
| echo "$HOME/.cargo/bin" >> $GITHUB_PATH | |
| - name: Install dependencies | |
| working-directory: ./backend | |
| run: | | |
| uv venv | |
| source .venv/bin/activate | |
| uv pip install -r requirements.txt | |
| uv pip install pytest pytest-asyncio httpx | |
| - name: Run backend tests | |
| working-directory: ./backend | |
| env: | |
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db | |
| run: | | |
| source .venv/bin/activate | |
| pytest tests/ -v --tb=short | |
| - name: Run API integration tests | |
| working-directory: ./backend | |
| env: | |
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db | |
| run: | | |
| source .venv/bin/activate | |
| # Start the server in background | |
| uvicorn main:app --host 0.0.0.0 --port 8000 & | |
| sleep 10 | |
| # Run curl tests | |
| python test_curl_examples.py | |
| test-frontend: | |
| name: Test Frontend | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ env.NODE_VERSION }} | |
| cache: 'npm' | |
| cache-dependency-path: frontend/package-lock.json | |
| - name: Install frontend dependencies | |
| working-directory: ./frontend | |
| run: npm ci | |
| - name: Run frontend linter | |
| working-directory: ./frontend | |
| run: npm run lint | |
| - name: Build frontend | |
| working-directory: ./frontend | |
| run: npm run build | |
| - name: Run frontend tests (if any) | |
| working-directory: ./frontend | |
| run: | | |
| if [ -f "package.json" ] && grep -q '"test"' package.json; then | |
| npm test | |
| else | |
| echo "No frontend tests found, skipping..." | |
| fi | |
| security-scan: | |
| name: Security Scan | |
| runs-on: ubuntu-latest | |
| needs: [test-backend, test-frontend] | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Run Trivy vulnerability scanner | |
| uses: aquasecurity/trivy-action@master | |
| with: | |
| scan-type: 'fs' | |
| scan-ref: '.' | |
| format: 'sarif' | |
| output: 'trivy-results.sarif' | |
| - name: Upload Trivy scan results to GitHub Security tab | |
| uses: github/codeql-action/upload-sarif@v2 | |
| if: always() | |
| with: | |
| sarif_file: 'trivy-results.sarif' | |
| code-quality: | |
| name: Code Quality | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: ${{ env.PYTHON_VERSION }} | |
| - name: Install Python quality tools | |
| run: | | |
| pip install black flake8 mypy bandit safety | |
| - name: Run Black (code formatter check) | |
| working-directory: ./backend | |
| run: black --check --diff . | |
| - name: Run Flake8 (linting) | |
| working-directory: ./backend | |
| run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | |
| - name: Run MyPy (type checking) | |
| working-directory: ./backend | |
| run: mypy . --ignore-missing-imports | |
| - name: Run Bandit (security linting) | |
| working-directory: ./backend | |
| run: bandit -r . -x tests/ | |
| - name: Run Safety (dependency security check) | |
| working-directory: ./backend | |
| run: safety check -r requirements.txt | |
| build-and-push: | |
| name: Build and Push Docker Images | |
| runs-on: ubuntu-latest | |
| needs: [test-backend, test-frontend, security-scan, code-quality] | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build and push backend image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./backend | |
| push: true | |
| tags: | | |
| ${{ secrets.DOCKER_USERNAME }}/crud-api-backend:latest | |
| ${{ secrets.DOCKER_USERNAME }}/crud-api-backend:${{ github.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build and push frontend image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./frontend | |
| push: true | |
| tags: | | |
| ${{ secrets.DOCKER_USERNAME }}/crud-api-frontend:latest | |
| ${{ secrets.DOCKER_USERNAME }}/crud-api-frontend:${{ github.sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| generate-docs: | |
| name: Generate and Deploy Documentation | |
| runs-on: ubuntu-latest | |
| needs: [test-backend] | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: ${{ env.PYTHON_VERSION }} | |
| - name: Install dependencies | |
| working-directory: ./backend | |
| run: | | |
| pip install -r requirements.txt | |
| - name: Generate OpenAPI schema | |
| working-directory: ./backend | |
| run: | | |
| python export_openapi.py | |
| python generate_curl_snippets.py | |
| - name: Upload OpenAPI artifacts | |
| uses: actions/upload-artifact@v3 | |
| with: | |
| name: openapi-docs | |
| path: | | |
| backend/openapi_schema.json | |
| backend/curl_examples.md | |
| backend/curl_examples.json | |
| - name: Deploy to GitHub Pages (if enabled) | |
| if: github.ref == 'refs/heads/main' | |
| uses: peaceiris/actions-gh-pages@v3 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| publish_dir: ./backend | |
| destination_dir: api-docs | |
| keep_files: true | |
| publish_branch: gh-pages |