-
Notifications
You must be signed in to change notification settings - Fork 0
247 lines (206 loc) Β· 7.37 KB
/
deploy.yml
File metadata and controls
247 lines (206 loc) Β· 7.37 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
name: Deploy to DigitalOcean
on:
push:
branches: [ main ]
workflow_dispatch:
env:
RUBY_VERSION: '3.3.6'
NODE_VERSION: '20'
POSTGRES_VERSION: '17'
REDIS_VERSION: '7'
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
services:
postgres:
image: postgres:17
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:7
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@v4
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@v5
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://${{ secrets.DROPLET_HOST }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy to DigitalOcean
id: deploy
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.DROPLET_HOST }}
username: deploy
key: ${{ secrets.DEPLOY_SSH_KEY }}
timeout: 900s
script: |
set -e
cd /var/www/streamsource
# Set environment variables for deployment
export GITHUB_REPOSITORY="${{ github.repository }}"
export GITHUB_SHA="${{ github.sha }}"
export GITHUB_REF="${{ github.ref }}"
# Run deployment with error handling
echo "π Starting deployment..."
./deploy/github-deploy.sh
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.0.0
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"