-
Notifications
You must be signed in to change notification settings - Fork 0
224 lines (194 loc) · 6.4 KB
/
Copy pathdeploy.yml
File metadata and controls
224 lines (194 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
env:
PYTHON_VERSION: '3.12'
jobs:
# Job 1: Linting and code quality
lint:
name: Code Quality Checks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 black isort
pip install -r requirements.txt
- name: Run Black (code formatting check)
run: black --check .
continue-on-error: true
- name: Run isort (import sorting check)
run: isort --check-only .
continue-on-error: true
- name: Run Flake8 (linting)
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
# Job 2: Security scanning
security:
name: Security Audit
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install safety
run: pip install safety
- name: Check for security vulnerabilities
run: safety check --json || true
# Job 3: Testing with coverage
test:
name: Run Tests
runs-on: ubuntu-latest
needs: [lint]
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov pytest-flask pytest-mock requests
- name: Run pytest with coverage
run: |
export PYTHONPATH="${PYTHONPATH}:${PWD}"
pytest tests/ -v --cov=. --cov-report=xml --cov-report=html --cov-report=term-missing
env:
FLASK_ENV: testing
SECRET_KEY: test-secret-key
PYTHONPATH: ${{ github.workspace }}
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: false
- name: Archive coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: htmlcov/
# Job 4: Build Docker image (optional)
build:
name: Build Application
runs-on: ubuntu-latest
needs: [test]
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
name: production-files
path: |
.
!venv/
!.git/
!__pycache__/
!*.pyc
# Job 5: Deploy to production
deploy:
name: Deploy to Production
runs-on: ubuntu-latest
needs: [build, security]
if: false # Disabled until production server is configured
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment:
name: production
url: https://your-domain.com
steps:
- name: Download production artifacts
uses: actions/download-artifact@v4
with:
name: production-files
- name: Deploy via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PRODUCTION_HOST }}
username: ${{ secrets.PRODUCTION_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT || 22 }}
script: |
cd /var/www/portfolio
git pull origin main
source venv/bin/activate
pip install -r requirements.txt
flask db upgrade || echo "No migrations to run"
sudo systemctl restart portfolio
sudo systemctl reload nginx
- name: Health check
run: |
sleep 10
curl --fail https://your-domain.com/health || exit 1
- name: Notify deployment success
if: success()
run: |
echo "✅ Deployment successful!"
# Add notification service here (Slack, Discord, email, etc.)
- name: Notify deployment failure
if: failure()
run: |
echo "❌ Deployment failed!"
# Add notification service here
# Job 6: Database backup (runs after deploy)
backup:
name: Backup Database
runs-on: ubuntu-latest
needs: [deploy]
if: false # Disabled until production server is configured
# if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Backup database via SSH
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PRODUCTION_HOST }}
username: ${{ secrets.PRODUCTION_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/portfolio
mkdir -p backups
if [ -f instance/portfolio.db ]; then
cp instance/portfolio.db backups/portfolio_$(date +%Y%m%d_%H%M%S).db
# Keep only last 7 backups
ls -t backups/portfolio_*.db | tail -n +8 | xargs -r rm
fi