1+ name : CI/CD Pipeline
2+
3+ on :
4+ push :
5+ branches : [ main, develop ]
6+ pull_request :
7+ branches : [ main ]
8+
9+ env :
10+ PYTHON_VERSION : ' 3.11'
11+ NODE_VERSION : ' 18'
12+
13+ jobs :
14+ # Code Quality and Testing
15+ test :
16+ name : Test and Quality Checks
17+ runs-on : ubuntu-latest
18+ strategy :
19+ matrix :
20+ python-version : ['3.8', '3.9', '3.10', '3.11']
21+
22+ steps :
23+ - name : Checkout code
24+ uses : actions/checkout@v4
25+
26+ - name : Set up Python ${{ matrix.python-version }}
27+ uses : actions/setup-python@v4
28+ with :
29+ python-version : ${{ matrix.python-version }}
30+
31+ - name : Cache pip dependencies
32+ uses : actions/cache@v3
33+ with :
34+ path : ~/.cache/pip
35+ key : ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
36+ restore-keys : |
37+ ${{ runner.os }}-pip-
38+
39+ - name : Install dependencies
40+ run : |
41+ python -m pip install --upgrade pip
42+ pip install -r requirements.txt
43+ pip install pytest pytest-cov pytest-asyncio flake8 black isort mypy
44+
45+ - name : Code formatting check (Black)
46+ run : black --check --diff src/ frontend/
47+
48+ - name : Import sorting check (isort)
49+ run : isort --check-only --diff src/ frontend/
50+
51+ - name : Lint with flake8
52+ run : |
53+ # Stop the build if there are Python syntax errors or undefined names
54+ flake8 src/ --count --select=E9,F63,F7,F82 --show-source --statistics
55+ # Exit-zero treats all errors as warnings
56+ flake8 src/ --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
57+
58+ - name : Type checking with mypy
59+ run : mypy src/ --ignore-missing-imports
60+
61+ - name : Run unit tests with pytest
62+ run : |
63+ pytest tests/ --cov=src/ --cov-report=xml --cov-report=html -v
64+
65+ - name : Upload coverage to Codecov
66+ if : matrix.python-version == '3.11'
67+ uses : codecov/codecov-action@v3
68+ with :
69+ file : ./coverage.xml
70+ flags : unittests
71+ name : codecov-umbrella
72+
73+ # Security Scanning
74+ security :
75+ name : Security Scan
76+ runs-on : ubuntu-latest
77+
78+ steps :
79+ - name : Checkout code
80+ uses : actions/checkout@v4
81+
82+ - name : Set up Python
83+ uses : actions/setup-python@v4
84+ with :
85+ python-version : ${{ env.PYTHON_VERSION }}
86+
87+ - name : Install dependencies
88+ run : |
89+ python -m pip install --upgrade pip
90+ pip install -r requirements.txt
91+
92+ - name : Run security scan with bandit
93+ run : |
94+ pip install bandit
95+ bandit -r src/ -f json -o bandit-report.json
96+
97+ - name : Run dependency vulnerability scan
98+ uses : pypa/gh-action-pip-audit@v1.0.8
99+ with :
100+ inputs : requirements.txt
101+
102+ # Docker Build and Test
103+ docker-build :
104+ name : Docker Build and Test
105+ runs-on : ubuntu-latest
106+ needs : [test, security]
107+
108+ steps :
109+ - name : Checkout code
110+ uses : actions/checkout@v4
111+
112+ - name : Set up Docker Buildx
113+ uses : docker/setup-buildx-action@v3
114+
115+ - name : Build backend Docker image
116+ run : |
117+ docker build --target backend -t invoice-ai-backend:test .
118+
119+ - name : Build frontend Docker image
120+ run : |
121+ docker build --target frontend -t invoice-ai-frontend:test .
122+
123+ - name : Test Docker images
124+ run : |
125+ # Test backend health endpoint
126+ docker run --rm -d --name backend-test -p 8000:8000 invoice-ai-backend:test
127+ sleep 10
128+ curl -f http://localhost:8000/ || exit 1
129+ docker stop backend-test
130+
131+ - name : Run container security scan
132+ uses : aquasecurity/trivy-action@master
133+ with :
134+ image-ref : ' invoice-ai-backend:test'
135+ format : ' sarif'
136+ output : ' trivy-results.sarif'
137+
138+ # Performance Testing
139+ performance :
140+ name : Performance Testing
141+ runs-on : ubuntu-latest
142+ needs : docker-build
143+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
144+
145+ steps :
146+ - name : Checkout code
147+ uses : actions/checkout@v4
148+
149+ - name : Run performance tests
150+ run : |
151+ # Add performance testing with locust or similar
152+ echo "Performance testing would run here"
153+
154+ # Deploy to Staging
155+ deploy-staging :
156+ name : Deploy to Staging
157+ runs-on : ubuntu-latest
158+ needs : [test, security, docker-build]
159+ if : github.ref == 'refs/heads/develop'
160+ environment : staging
161+
162+ steps :
163+ - name : Checkout code
164+ uses : actions/checkout@v4
165+
166+ - name : Deploy to staging
167+ run : |
168+ echo "Deploy to staging environment"
169+ # Add deployment scripts here
170+
171+ # Deploy to Production
172+ deploy-production :
173+ name : Deploy to Production
174+ runs-on : ubuntu-latest
175+ needs : [test, security, docker-build, performance]
176+ if : github.ref == 'refs/heads/main'
177+ environment : production
178+
179+ steps :
180+ - name : Checkout code
181+ uses : actions/checkout@v4
182+
183+ - name : Login to DockerHub
184+ uses : docker/login-action@v3
185+ with :
186+ username : ${{ secrets.DOCKERHUB_USERNAME }}
187+ password : ${{ secrets.DOCKERHUB_TOKEN }}
188+
189+ - name : Build and push Docker images
190+ run : |
191+ # Backend
192+ docker build --target backend -t ${{ secrets.DOCKERHUB_USERNAME }}/invoice-ai-backend:latest .
193+ docker push ${{ secrets.DOCKERHUB_USERNAME }}/invoice-ai-backend:latest
194+
195+ # Frontend
196+ docker build --target frontend -t ${{ secrets.DOCKERHUB_USERNAME }}/invoice-ai-frontend:latest .
197+ docker push ${{ secrets.DOCKERHUB_USERNAME }}/invoice-ai-frontend:latest
198+
199+ - name : Deploy to production
200+ run : |
201+ echo "Deploy to production environment"
202+ # Add production deployment scripts here
203+
204+ # Cleanup
205+ cleanup :
206+ name : Cleanup
207+ runs-on : ubuntu-latest
208+ needs : [deploy-production]
209+ if : always()
210+
211+ steps :
212+ - name : Clean up Docker images
213+ run : |
214+ docker system prune -f
0 commit comments