-
Notifications
You must be signed in to change notification settings - Fork 2
235 lines (199 loc) · 6.98 KB
/
Copy pathquantum-ci-cd.yml
File metadata and controls
235 lines (199 loc) · 6.98 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
name: Quantum Infrastructure CI/CD
on:
push:
branches: [ main, develop, 'feature/*' ]
paths:
- 'src/**'
- 'deploy/**'
- 'tests/**'
- 'pyproject.toml'
- 'requirements*.txt'
pull_request:
branches: [ main, develop ]
env:
DOCKER_IMAGE: ghcr.io/${{ github.repository }}/quantum-node
HELM_VERSION: 3.14.0
KUBE_VERSION: 1.28.0
PYTHON_VERSION: '3.10'
DOCKER_BUILDKIT: 1
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
services:
# Add any required services like Redis, PostgreSQL, etc.
redis:
image: redis:7
ports:
- 6379:6379
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install pytest pytest-cov pytest-asyncio
- name: Run unit tests
run: |
pytest tests/unit/ --cov=src --cov-report=xml --cov-report=term
- name: Run integration tests
run: |
pytest tests/integration/ -v
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
fail_ci_if_error: false
build:
name: Build and Push Docker Image
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.DOCKER_IMAGE }}
tags: |
type=sha,format=long
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=sha-
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
PYTHON_VERSION=${{ env.PYTHON_VERSION }}
deploy-staging:
name: Deploy to Staging
needs: build
if: github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Helm
uses: azure/setup-helm@v3
with:
version: ${{ env.HELM_VERSION }}
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: ${{ env.KUBE_VERSION }}
- name: Configure AWS Credentials
if: env.AWS_ACCESS_KEY_ID == ''
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Configure kubeconfig
run: |
aws eks --region ${{ secrets.AWS_REGION }} update-kubeconfig \
--name ${{ secrets.EKS_CLUSTER_NAME }}
- name: Deploy to Kubernetes
run: |
helm upgrade --install quantum-node ./deploy/helm/quantum-infra-zero \
--namespace staging \
--create-namespace \
--values ./deploy/helm/quantum-infra-zero/values-staging.yaml \
--set image.tag=${{ github.sha }} \
--atomic \
--timeout 5m \
--wait
- name: Run smoke tests
run: |
# Add smoke tests here
echo "Running smoke tests..."
# Example: curl -sSf http://quantum-node.staging.svc.cluster.local:8000/health
deploy-production:
name: Deploy to Production
needs: deploy-staging
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Helm
uses: azure/setup-helm@v3
with:
version: ${{ env.HELM_VERSION }}
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: ${{ env.KUBE_VERSION }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Configure kubeconfig
run: |
aws eks --region ${{ secrets.AWS_REGION }} update-kubeconfig \
--name ${{ secrets.EKS_CLUSTER_NAME }}
- name: Deploy to Kubernetes
run: |
# Get the current production image tag
CURRENT_TAG=$(helm get values -n production quantum-node -o json | jq -r '.image.tag // empty')
# Deploy with canary strategy
helm upgrade --install quantum-node ./deploy/helm/quantum-infra-zero \
--namespace production \
--create-namespace \
--values ./deploy/helm/quantum-infra-zero/values-production.yaml \
--set image.tag=${{ github.sha }} \
--set replicaCount=1 \
--atomic \
--timeout 5m \
--wait
# Run canary tests
echo "Running canary tests..."
# Example: run tests against the new deployment
# If tests pass, scale up
kubectl scale deployment quantum-node -n production --replicas=3
# Optionally, rollback if tests fail
# if [ $? -ne 0 ]; then
# echo "Canary tests failed, rolling back..."
# helm rollback quantum-node -n production
# exit 1
# fi
- name: Notify Slack
if: always()
uses: rtCamp/action-slack-notify@v2.2.0
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_COLOR: ${{ job.status == 'success' && '#36a64f' || '#ff0000' }}
SLACK_TITLE: 'Deployment to Production ${{ job.status == 'success' && '✅ Succeeded' || '❌ Failed' }}'
SLACK_MESSAGE: "Quantum Infrastructure deployment to production ${{ job.status == 'success' && 'completed successfully' || 'failed' }}.\nCommit: ${{ github.sha }}\nRun: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
SLACK_USERNAME: GitHub Actions
MSG_MINIMAL: true