-
Notifications
You must be signed in to change notification settings - Fork 29
279 lines (238 loc) · 8.55 KB
/
Copy pathdeploy.yml
File metadata and controls
279 lines (238 loc) · 8.55 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
name: Deploy to Kubernetes
on:
push:
branches: [main, develop, kubernetes-deployment]
paths: ['src/**', 'package.json', 'k8s/**', 'Dockerfile']
pull_request:
branches: [main]
paths: ['src/**', 'package.json', 'Dockerfile']
workflow_dispatch:
inputs:
environment:
description: 'Deployment environment'
required: true
default: 'staging'
type: choice
options:
- staging
- production
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
KUBECTL_VERSION: 'v1.28.0'
HELM_VERSION: 'v3.13.0'
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: |
npm ci --production=false
- name: Run linting
run: npm run lint
- name: Run tests
run: npm run test:cov
- name: Upload coverage reports
uses: codecov/codecov-action@v3
with:
file: ./coverage/lcov.info
flags: unittests
name: codecov-umbrella
security-scan:
name: Security Scan
runs-on: ubuntu-latest
timeout-minutes: 10
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
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'
build:
name: Build and Push Docker Image
runs-on: ubuntu-latest
needs: [test, security-scan]
outputs:
image-digest: ${{ steps.build.outputs.digest }}
image-tag: ${{ steps.meta.outputs.tags }}
timeout-minutes: 20
steps:
- name: Checkout code
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 }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
provenance: false
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/develop' || github.event.inputs.environment == 'staging'
environment: staging
timeout-minutes: 10
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup kubectl
uses: azure/setup-kubectl@v3
with:
version: ${{ env.KUBECTL_VERSION }}
- name: Setup Helm
uses: azure/setup-helm@v3
with:
version: ${{ env.HELM_VERSION }}
- name: Configure kubectl
run: |
echo "${{ secrets.KUBE_CONFIG_STAGING }}" | base64 -d > kubeconfig
export KUBECONFIG=kubeconfig
- name: Deploy to Kubernetes
run: |
export KUBECONFIG=kubeconfig
# Create namespace if it doesn't exist
kubectl create namespace currentdao-staging --dry-run=client -o yaml | kubectl apply -f -
# Apply all manifests
kubectl apply -f k8s/ -n currentdao-staging || true
# Update image
kubectl set image deployment/currentdao-backend currentdao-backend=${{ needs.build.outputs.image-tag }} -n currentdao-staging || kubectl create -f k8s/deployment.yaml -n currentdao-staging
kubectl rollout status deployment/currentdao-backend -n currentdao-staging --timeout=300s
- name: Run smoke tests
run: |
export KUBECONFIG=kubeconfig
kubectl wait --for=condition=ready pod -l app=currentdao-backend -n currentdao-staging --timeout=300s || true
kubectl port-forward svc/currentdao-backend-service 8080:80 -n currentdao-staging &
sleep 10
curl -f http://localhost:8080/health || curl -f http://localhost:8080/api/health || echo 'Health check failed but continuing'
deploy-production:
name: Deploy to Production
runs-on: ubuntu-latest
needs: [build, deploy-staging]
if: github.ref == 'refs/heads/main' || github.event.inputs.environment == 'production'
environment: production
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup kubectl
uses: azure/setup-kubectl@v3
with:
version: ${{ env.KUBECTL_VERSION }}
- name: Setup Helm
uses: azure/setup-helm@v3
with:
version: ${{ env.HELM_VERSION }}
- name: Configure kubectl
run: |
echo "${{ secrets.KUBE_CONFIG_PRODUCTION }}" | base64 -d > kubeconfig
export KUBECONFIG=kubeconfig
- name: Deploy to Kubernetes
run: |
export KUBECONFIG=kubeconfig
# Create namespace if it doesn't exist
kubectl create namespace currentdao-prod --dry-run=client -o yaml | kubectl apply -f -
# Apply all manifests
kubectl apply -f k8s/ -n currentdao-prod || true
# Update image
kubectl set image deployment/currentdao-backend currentdao-backend=${{ needs.build.outputs.image-tag }} -n currentdao-prod || kubectl create -f k8s/deployment.yaml -n currentdao-prod
kubectl rollout status deployment/currentdao-backend -n currentdao-prod --timeout=300s
- name: Verify deployment
run: |
export KUBECONFIG=kubeconfig
kubectl wait --for=condition=ready pod -l app=currentdao-backend -n currentdao-prod --timeout=300s
kubectl get pods -l app=currentdao-backend -n currentdao-prod
kubectl get hpa currentdao-backend-hpa -n currentdao-prod
- name: Run production smoke tests
run: |
export KUBECONFIG=kubeconfig
kubectl wait --for=condition=ready pod -l app=currentdao-backend -n currentdao-prod --timeout=300s || true
kubectl port-forward svc/currentdao-backend-service 8080:80 -n currentdao-prod &
sleep 10
curl -f http://localhost:8080/health || curl -f http://localhost:8080/api/health || echo 'Health check failed but continuing'
# External health check
curl -f https://api.currentdao.org/health || curl -f https://api.currentdao.org/api/health || echo 'External health check failed but continuing'
rollback:
name: Rollback on Failure
runs-on: ubuntu-latest
needs: [deploy-production]
if: failure() && needs.deploy-production.result == 'failure'
environment: production
timeout-minutes: 10
steps:
- name: Setup kubectl
uses: azure/setup-kubectl@v3
with:
version: ${{ env.KUBECTL_VERSION }}
- name: Configure kubectl
run: |
echo "${{ secrets.KUBE_CONFIG_PRODUCTION }}" | base64 -d > kubeconfig
export KUBECONFIG=kubeconfig
- name: Rollback deployment
run: |
export KUBECONFIG=kubeconfig
kubectl rollout undo deployment/currentdao-backend -n currentdao-prod
kubectl rollout status deployment/currentdao-backend -n currentdao-prod --timeout=300s
- name: Notify team
uses: 8398a7/action-slack@v3
with:
status: failure
channel: '#devops'
text: 'Production deployment failed and was rolled back'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
cleanup:
name: Cleanup
runs-on: ubuntu-latest
needs: [deploy-staging, deploy-production]
if: always()
timeout-minutes: 5
steps:
- name: Cleanup old images
uses: actions/delete-package-versions@v4
with:
package-name: 'currentdao-backend'
package-type: 'container'
min-versions-to-keep: 10
delete-only-untagged-versions: true