-
Notifications
You must be signed in to change notification settings - Fork 0
344 lines (286 loc) · 9.73 KB
/
ci.yml
File metadata and controls
344 lines (286 loc) · 9.73 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
name: Red Hat Learning Paths CI
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME_BACKEND: ${{ github.repository }}-backend
IMAGE_NAME_FRONTEND: ${{ github.repository }}-frontend
jobs:
test-backend:
name: Test Backend
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_PASSWORD: password
POSTGRES_USER: postgres
POSTGRES_DB: test_db
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip'
- name: Install dependencies
working-directory: ./backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest-cov pytest-mock
- name: Run tests
working-directory: ./backend
env:
DATABASE_URL: postgresql://postgres:password@localhost:5432/test_db
OLLAMA_BASE_URL: http://localhost:11434
run: |
python -m pytest test_main.py -v --cov=. --cov-report=xml --cov-report=term-missing
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./backend/coverage.xml
flags: backend
name: backend-coverage
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@v3
with:
node-version: '18'
cache: 'npm'
cache-dependency-path: ./frontend/package-lock.json
- name: Install dependencies
working-directory: ./frontend
run: npm ci
- name: Run unit tests
working-directory: ./frontend
run: npm test -- --coverage --watchAll=false
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./frontend/coverage/lcov.info
flags: frontend
name: frontend-coverage
lint-and-format:
name: Lint and Format Check
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
cache-dependency-path: ./frontend/package-lock.json
- name: Install Python linting tools
run: |
python -m pip install --upgrade pip
pip install black flake8 isort mypy
- name: Lint Python code
working-directory: ./backend
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
black --check .
isort --check-only .
- name: Install frontend dependencies
working-directory: ./frontend
run: npm ci
- name: Lint frontend code
working-directory: ./frontend
run: npm run lint
security-scan:
name: Security Scan
runs-on: ubuntu-latest
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'
build-images:
name: Build Container Images
runs-on: ubuntu-latest
needs: [test-backend, test-frontend, lint-and-format]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Podman
run: |
sudo apt-get update
sudo apt-get install -y podman
- name: Log in to Container Registry
uses: redhat-actions/podman-login@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for backend
id: meta-backend
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Extract metadata for frontend
id: meta-frontend
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build backend image
working-directory: ./backend
run: |
podman build \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}:latest \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}:${{ github.sha }} \
--file Containerfile .
- name: Build frontend image
working-directory: ./frontend
run: |
podman build \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}:latest \
--tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}:${{ github.sha }} \
--file Containerfile .
- name: Push backend image
run: |
podman push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}:latest
podman push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}:${{ github.sha }}
- name: Push frontend image
run: |
podman push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}:latest
podman push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}:${{ github.sha }}
e2e-tests:
name: E2E Tests
runs-on: ubuntu-latest
needs: [build-images]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Podman
run: |
sudo apt-get update
sudo apt-get install -y podman
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
cache-dependency-path: ./frontend/package-lock.json
- name: Install frontend dependencies
working-directory: ./frontend
run: npm ci
- name: Install Playwright
working-directory: ./frontend
run: npx playwright install --with-deps
- name: Pull container images
run: |
podman pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}:${{ github.sha }}
podman pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}:${{ github.sha }}
podman pull docker.io/postgres:15-alpine
podman pull docker.io/ollama/ollama:latest
- name: Tag images for local use
run: |
podman tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_BACKEND }}:${{ github.sha }} localhost/redhat-learning-paths-backend:latest
podman tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_FRONTEND }}:${{ github.sha }} localhost/redhat-learning-paths-frontend:latest
- name: Start application with Podman
run: |
# Create volumes
podman volume create postgres-data || true
podman volume create ollama-data || true
# Start the pod
podman play kube podman-pod.yaml
# Wait for services to be ready
sleep 30
# Check health
timeout 60 bash -c 'until curl -f http://localhost:30000/health; do sleep 5; done'
timeout 60 bash -c 'until curl -f http://localhost:30001/health; do sleep 5; done'
- name: Run E2E tests
working-directory: ./frontend
env:
PLAYWRIGHT_BASE_URL: http://localhost:30000
run: npx playwright test
- name: Upload Playwright report
uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: frontend/playwright-report/
retention-days: 30
- name: Stop application
if: always()
run: |
podman pod stop redhat-learning-paths || true
podman pod rm redhat-learning-paths || true
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: [e2e-tests]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
environment:
name: staging
url: https://staging.redhat-learning-paths.example.com
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy to staging
run: |
echo "Deploying to staging environment..."
echo "Image tags: ${{ github.sha }}"
# Add your staging deployment steps here
# This could include updating Kubernetes manifests,
# triggering deployment pipelines, etc.
notification:
name: Notification
runs-on: ubuntu-latest
needs: [test-backend, test-frontend, lint-and-format, security-scan]
if: always()
steps:
- name: Notify on success
if: ${{ needs.test-backend.result == 'success' && needs.test-frontend.result == 'success' && needs.lint-and-format.result == 'success' }}
run: |
echo "✅ All checks passed successfully!"
- name: Notify on failure
if: ${{ needs.test-backend.result == 'failure' || needs.test-frontend.result == 'failure' || needs.lint-and-format.result == 'failure' || needs.security-scan.result == 'failure' }}
run: |
echo "❌ Some checks failed. Please review the logs."
exit 1