-
Notifications
You must be signed in to change notification settings - Fork 0
172 lines (147 loc) · 5.22 KB
/
Copy pathci.yml
File metadata and controls
172 lines (147 loc) · 5.22 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
# ============================================================
# 🚀 CI/CD Pipeline - Cloud-Native API
# ============================================================
# Stages:
# 1. Lint → Code quality (ruff)
# 2. Test → Unit tests + coverage (pytest)
# 3. Security → Container vulnerability scan (Trivy)
# 4. Build → Multi-stage Docker image
# 5. Push → Push to AWS ECR (only on main, requires secrets)
# ============================================================
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
env:
PYTHON_VERSION: "3.12"
DOCKER_IMAGE: cloud-native-api
permissions:
contents: read
security-events: write
jobs:
# ---- Stage 1: Lint ----
lint:
name: 🔍 Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install ruff
run: pip install ruff
- name: Run linter
run: ruff check app/ --output-format=github
# ---- Stage 2: Test ----
test:
name: 🧪 Test
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: pip
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests with coverage
run: |
pytest app/tests/ \
-v \
--cov=app \
--cov-report=term-missing \
--cov-report=xml:coverage.xml \
--cov-fail-under=80
- name: Upload coverage report
uses: actions/upload-artifact@v4
if: always()
with:
name: coverage-report
path: coverage.xml
# ---- Stage 3: Security Scan ----
security:
name: 🛡️ Security Scan
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: Build image for scanning
run: docker build -t ${{ env.DOCKER_IMAGE }}:scan .
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@0.28.0
with:
image-ref: ${{ env.DOCKER_IMAGE }}:scan
format: table
exit-code: 1
ignore-unfixed: true
severity: CRITICAL,HIGH
- name: Run Trivy (SARIF output for GitHub Security)
uses: aquasecurity/trivy-action@0.28.0
if: always()
with:
image-ref: ${{ env.DOCKER_IMAGE }}:scan
format: sarif
output: trivy-results.sarif
severity: CRITICAL,HIGH
- name: Upload Trivy SARIF to GitHub Security
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: trivy-results.sarif
# ---- Stage 4: Build & Push ----
build-and-push:
name: 🐳 Build & Push
runs-on: ubuntu-latest
needs: security
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build production image
run: |
docker build \
--tag ${{ env.DOCKER_IMAGE }}:${{ github.sha }} \
--tag ${{ env.DOCKER_IMAGE }}:latest \
--label "org.opencontainers.image.revision=${{ github.sha }}" \
--label "org.opencontainers.image.created=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
.
# ---- Push to ECR (requires AWS secrets) ----
- name: Configure AWS credentials
if: env.AWS_ACCESS_KEY_ID != ''
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION || 'us-east-1' }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
- name: Login to Amazon ECR
if: env.AWS_ACCESS_KEY_ID != ''
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
- name: Push to ECR
if: steps.login-ecr.outcome == 'success'
run: |
ECR_URI=${{ steps.login-ecr.outputs.registry }}/cloud-native-api-dev
docker tag ${{ env.DOCKER_IMAGE }}:${{ github.sha }} $ECR_URI:${{ github.sha }}
docker tag ${{ env.DOCKER_IMAGE }}:latest $ECR_URI:latest
docker push $ECR_URI:${{ github.sha }}
docker push $ECR_URI:latest
echo "### 🐳 Image pushed to ECR" >> $GITHUB_STEP_SUMMARY
echo "- \`$ECR_URI:${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY
- name: Summary (no AWS configured)
if: env.AWS_ACCESS_KEY_ID == ''
run: |
echo "### ✅ Docker image built successfully" >> $GITHUB_STEP_SUMMARY
echo "ECR push skipped — no AWS credentials configured." >> $GITHUB_STEP_SUMMARY
echo "To enable, add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to repository secrets." >> $GITHUB_STEP_SUMMARY
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}