-
Notifications
You must be signed in to change notification settings - Fork 0
283 lines (237 loc) Β· 8.44 KB
/
deploy.yml
File metadata and controls
283 lines (237 loc) Β· 8.44 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
name: Deploy to DigitalOcean
run-name: Deploy to DigitalOcean (${{ github.ref_name }})
on:
push:
branches: [ main ]
workflow_dispatch:
permissions:
contents: read
packages: write
env:
RUBY_VERSION: '4.0.1'
NODE_VERSION: '24'
POSTGRES_VERSION: '18'
REDIS_VERSION: '8'
REGISTRY: ghcr.io
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
services:
postgres:
image: postgres:18
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: streamsource_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:8
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
steps:
- name: Checkout code
uses: actions/checkout@v5
with:
fetch-depth: 0 # Full history for better caching
- name: Cache Ruby dependencies
uses: actions/cache@v4
with:
path: |
~/.bundle
vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true
- name: Cache Node dependencies
uses: actions/cache@v4
with:
path: |
~/.cache/yarn
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'yarn'
- name: Install dependencies
run: |
yarn install --frozen-lockfile --prefer-offline
- name: Setup test database
env:
RAILS_ENV: test
DATABASE_URL: postgres://postgres:postgres@localhost:5432/streamsource_test
REDIS_URL: redis://localhost:6379/1
run: |
bundle exec rails db:create
bundle exec rails db:schema:load
- name: Run tests
env:
RAILS_ENV: test
DATABASE_URL: postgres://postgres:postgres@localhost:5432/streamsource_test
REDIS_URL: redis://localhost:6379/1
run: bundle exec rspec
- name: Run security checks
run: |
echo "π Running security analysis..."
# Static Application Security Testing (SAST)
bundle exec brakeman -q -w2 --format json --output brakeman-report.json
# Dependency vulnerability scanning
bundle exec bundler-audit check --update --format json --output bundler-audit-report.json
# Display results
echo "Brakeman security scan completed"
if [ -f brakeman-report.json ]; then
echo "Brakeman found $(jq '.warnings | length' brakeman-report.json) potential issues"
fi
echo "Bundler audit completed"
if [ -f bundler-audit-report.json ]; then
echo "Bundler audit found $(jq '.vulnerabilities | length' bundler-audit-report.json) vulnerabilities"
fi
- name: Upload security reports
uses: actions/upload-artifact@v4
if: always()
with:
name: security-reports
path: |
brakeman-report.json
bundler-audit-report.json
retention-days: 30
deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
timeout-minutes: 20
environment:
name: production
url: https://${{ vars.DROPLET_HOST }}
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Set image name
id: image
run: echo "name=${GITHUB_REPOSITORY,,}" >> $GITHUB_OUTPUT
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }}
tags: |
type=sha,format=long
type=raw,value=latest
- name: Build and push image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Deploy to DigitalOcean
id: deploy
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.DROPLET_HOST }}
username: deploy
key: ${{ secrets.DEPLOY_SSH_KEY }}
timeout: 900s
script: |
set -e
cd /var/www/streamsource
export STREAMSOURCE_IMAGE="${{ env.REGISTRY }}/${{ steps.image.outputs.name }}:sha-${{ github.sha }}"
export STREAMSOURCE_ENV_FILE="/var/www/streamsource/.env.production"
echo "π Pulling image ${STREAMSOURCE_IMAGE}..."
docker compose -f docker-compose.yml -f docker-compose.prod.yml pull web
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d web
echo "β
Deployment completed successfully"
- name: Comprehensive health check
run: |
echo "π₯ Running comprehensive health checks..."
# Wait for application to stabilize
sleep 30
# Test multiple endpoints
echo "Testing basic health endpoint..."
curl -f -s https://${{ secrets.DROPLET_HOST }}/health || exit 1
echo "Testing database connectivity..."
curl -f -s https://${{ secrets.DROPLET_HOST }}/health/db || exit 1
echo "Testing Redis connectivity..."
curl -f -s https://${{ secrets.DROPLET_HOST }}/health/redis || exit 1
echo "Testing application responsiveness..."
RESPONSE_TIME=$(curl -w "%{time_total}" -s -o /dev/null https://${{ secrets.DROPLET_HOST }}/health)
echo "Response time: ${RESPONSE_TIME}s"
# Fail if response time is too slow (> 5 seconds)
if (( $(echo "$RESPONSE_TIME > 5.0" | bc -l) )); then
echo "β Application is responding too slowly ($RESPONSE_TIME seconds)"
exit 1
fi
echo "β
All health checks passed"
- name: Rollback on failure
if: failure() && steps.deploy.conclusion == 'failure'
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.DROPLET_HOST }}
username: deploy
key: ${{ secrets.DEPLOY_SSH_KEY }}
script: |
cd /var/www/streamsource
echo "π Initiating automatic rollback..."
./deploy/rollback.sh
echo "β
Rollback completed"
continue-on-error: true
- name: Notify deployment status
if: always()
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
text: |
StreamSource deployment ${{ job.status }}
π *Deployment Details:*
β’ Commit: `${{ github.sha }}`
β’ Branch: `${{ github.ref_name }}`
β’ Author: ${{ github.actor }}
β’ Workflow: ${{ github.workflow }}
${{ job.status == 'success' && 'β
Deployment successful!' || 'β Deployment failed - automatic rollback initiated' }}
π [View Workflow Run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
continue-on-error: true
- name: Record deployment metrics
if: always()
run: |
echo "π Recording deployment metrics..."
# Create deployment record
curl -X POST "https://api.github.com/repos/${{ github.repository }}/deployments" \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{
"ref": "${{ github.sha }}",
"environment": "production",
"description": "Deployment via GitHub Actions",
"auto_merge": false
}' || echo "Failed to record deployment"
echo "β
Deployment metrics recorded"