-
Notifications
You must be signed in to change notification settings - Fork 4
293 lines (252 loc) · 8.26 KB
/
deploy.yml
File metadata and controls
293 lines (252 loc) · 8.26 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
name: Deploy to Production
on:
push:
branches:
- main
pull_request:
types:
- closed
branches:
- main
env:
DOCKER_REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
DEPLOY_TIMEOUT: 600
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16-alpine
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: memecoingen_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
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
- name: Run linter
run: npm run lint
- name: Run type check
run: npm run type-check
- name: Run unit tests
run: npm run test:unit
env:
DATABASE_URL: postgresql://test:test@localhost:5432/memecoingen_test
- name: Run integration tests
run: npm run test:integration
env:
DATABASE_URL: postgresql://test:test@localhost:5432/memecoingen_test
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
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'
severity: 'CRITICAL,HIGH'
- name: Upload Trivy scan results
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: 'trivy-results.sarif'
- name: Run npm audit
run: npm audit --production --audit-level=moderate
build:
name: Build Docker Image
needs: [test, security-scan]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image-tag: ${{ steps.meta.outputs.tags }}
image-digest: ${{ steps.build.outputs.digest }}
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.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile.prod
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_DATE=${{ github.event.head_commit.timestamp }}
VCS_REF=${{ github.sha }}
VERSION=${{ steps.meta.outputs.version }}
deploy-staging:
name: Deploy to Staging
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
environment:
name: staging
url: https://staging.memecoingen.com
steps:
- name: Deploy to staging
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.STAGING_HOST }}
username: ${{ secrets.STAGING_USER }}
key: ${{ secrets.STAGING_SSH_KEY }}
script: |
cd /opt/memecoingen
docker compose -f docker-compose.staging.yml pull
docker compose -f docker-compose.staging.yml up -d --no-deps --scale app=3
docker system prune -f
- name: Run smoke tests
run: |
sleep 30
curl -f https://staging.memecoingen.com/health || exit 1
deploy-production:
name: Deploy to Production
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment:
name: production
url: https://memecoingen.com
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Create deployment
uses: chrnorm/deployment-action@v2
id: deployment
with:
token: ${{ github.token }}
environment: production
description: 'Deploy ${{ needs.build.outputs.image-tag }}'
- name: Deploy to production (Blue-Green)
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.PRODUCTION_HOST }}
username: ${{ secrets.PRODUCTION_USER }}
key: ${{ secrets.PRODUCTION_SSH_KEY }}
timeout: ${{ env.DEPLOY_TIMEOUT }}s
script: |
cd /opt/memecoingen
# Pull new image
docker compose -f docker-compose.prod.yml pull app
# Start new containers (blue)
docker compose -f docker-compose.prod.yml up -d --no-deps --scale app=6 app
# Wait for health checks
sleep 30
# Check new containers are healthy
NEW_CONTAINERS=$(docker ps --filter "label=com.docker.compose.service=app" --filter "health=healthy" -q | head -3)
if [ -z "$NEW_CONTAINERS" ]; then
echo "New containers failed health check"
exit 1
fi
# Update load balancer to point to new containers
docker exec nginx nginx -s reload
# Remove old containers
OLD_CONTAINERS=$(docker ps --filter "label=com.docker.compose.service=app" -q | tail -n +4)
if [ ! -z "$OLD_CONTAINERS" ]; then
docker stop $OLD_CONTAINERS
docker rm $OLD_CONTAINERS
fi
# Clean up
docker system prune -f
- name: Run production tests
run: |
sleep 30
# Health check
curl -f https://memecoingen.com/health || exit 1
# API check
curl -f https://memecoingen.com/api/status || exit 1
- name: Update deployment status
if: always()
uses: chrnorm/deployment-status@v2
with:
token: ${{ github.token }}
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
state: ${{ job.status }}
environment-url: https://memecoingen.com
- name: Notify Slack
if: always()
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
text: 'Production deployment ${{ job.status }}'
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
channel: '#deployments'
username: 'GitHub Actions'
rollback:
name: Rollback Production
needs: deploy-production
runs-on: ubuntu-latest
if: failure()
environment:
name: production
steps:
- name: Rollback deployment
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.PRODUCTION_HOST }}
username: ${{ secrets.PRODUCTION_USER }}
key: ${{ secrets.PRODUCTION_SSH_KEY }}
script: |
cd /opt/memecoingen
# Rollback to previous version
docker compose -f docker-compose.prod.yml up -d --no-deps --scale app=3 app:previous
docker system prune -f
- name: Notify Slack about rollback
uses: 8398a7/action-slack@v3
with:
status: custom
custom_payload: |
{
text: ':warning: Production deployment rolled back',
channel: '#deployments',
username: 'GitHub Actions'
}
webhook_url: ${{ secrets.SLACK_WEBHOOK }}