-
Notifications
You must be signed in to change notification settings - Fork 0
195 lines (163 loc) · 5.45 KB
/
Copy pathci-cd.yml
File metadata and controls
195 lines (163 loc) · 5.45 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
name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# Code Quality & Testing
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: tnp_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
# Backend tests
- name: Backend - Install dependencies
run: cd backend && npm ci
- name: Backend - Run linter
run: cd backend && npm run lint 2>/dev/null || true
- name: Backend - Run tests
run: cd backend && npm test 2>/dev/null || true
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/tnp_test
# Frontend tests
- name: Frontend - Install dependencies
run: cd frontend && npm ci
- name: Frontend - Run linter
run: cd frontend && npm run lint 2>/dev/null || true
- name: Frontend - Build
run: cd frontend && npm run build
env:
VITE_API_BASE_URL: http://localhost:3001
# Docker Build & Push
docker-build:
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'push'
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Build Backend
- name: Extract metadata (Backend)
id: meta-backend
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-backend
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
- name: Build and push Backend image
uses: docker/build-push-action@v5
with:
context: ./backend
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-backend.outputs.tags }}
labels: ${{ steps.meta-backend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
# Build Frontend
- name: Extract metadata (Frontend)
id: meta-frontend
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-frontend
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
- name: Build and push Frontend image
uses: docker/build-push-action@v5
with:
context: ./frontend
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta-frontend.outputs.tags }}
labels: ${{ steps.meta-frontend.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
VITE_API_BASE_URL=http://localhost:3001
# Security Scanning
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Trivy vulnerability scanner (Backend)
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: 'backend'
format: 'sarif'
output: 'trivy-backend-results.sarif'
- name: Run Trivy vulnerability scanner (Frontend)
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: 'frontend'
format: 'sarif'
output: 'trivy-frontend-results.sarif'
- name: Upload Trivy results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-*.sarif'
# Deploy to staging (optional - requires configuration)
deploy-staging:
needs: [test, docker-build]
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop' && github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- name: Deploy to Staging
run: |
echo "🚀 Deploying to staging environment..."
# Add your deployment script or commands here
# Example: kubectl apply -f k8s/staging/
# Or: docker-compose -f docker-compose.yml up -d
# Deploy to production (optional - requires configuration)
deploy-production:
needs: [test, docker-build]
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- name: Deploy to Production
run: |
echo "🚀 Deploying to production environment..."
# Add your deployment script or commands here
# Example: kubectl apply -f k8s/production/
# Or: docker-compose -f docker-compose.prod.yml up -d