-
Notifications
You must be signed in to change notification settings - Fork 3
397 lines (343 loc) · 12.6 KB
/
deploy.yml
File metadata and controls
397 lines (343 loc) · 12.6 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
name: Deploy
on:
workflow_call:
inputs:
environment:
description: "Deployment environment"
required: true
type: string
default: "staging"
version:
description: "Version to deploy (tag or commit)"
required: false
type: string
skip-health-check:
description: "Skip post-deployment health check"
required: false
default: false
type: boolean
workflow_dispatch:
inputs:
environment:
description: "Deployment environment"
required: true
type: string
default: "staging"
version:
description: "Version to deploy (tag or commit)"
required: false
type: string
skip-health-check:
description: "Skip post-deployment health check"
required: false
default: false
type: boolean
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
CARGO_INCREMENTAL: 0
CARGO_NET_RETRY: 10
RUSTUP_MAX_RETRIES: 10
REGISTRY: ghcr.io
IMAGE_NAME: terraphim/terraphim-ai
jobs:
# Validate deployment parameters
validate:
name: Validate Deployment
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 3
outputs:
environment: ${{ steps.env.outputs.environment }}
version: ${{ steps.version.outputs.version }}
is-production: ${{ steps.env.outputs.is-production }}
deployment-url: ${{ steps.env.outputs.deployment-url }}
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Validate environment
id: env
run: |
ENVIRONMENT="${{ github.event.inputs.environment || inputs.environment }}"
case "$ENVIRONMENT" in
staging|dev|development)
echo "environment=staging" >> $GITHUB_OUTPUT
echo "is-production=false" >> $GITHUB_OUTPUT
echo "deployment-url=https://staging.terraphim.ai" >> $GITHUB_OUTPUT
;;
production|prod)
echo "environment=production" >> $GITHUB_OUTPUT
echo "is-production=true" >> $GITHUB_OUTPUT
echo "deployment-url=https://app.terraphim.ai" >> $GITHUB_OUTPUT
;;
*)
echo "Invalid environment: $ENVIRONMENT"
echo "Valid environments: staging, production"
exit 1
;;
esac
- name: Resolve version
id: version
run: |
VERSION="${{ github.event.inputs.version || inputs.version || github.sha }}"
# If it's a commit, get the short hash
if [[ "$VERSION" =~ ^[0-9a-f]{7,40}$ ]]; then
VERSION=$(git rev-parse --short "$VERSION")
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Deploying version: $VERSION to ${{ steps.env.outputs.environment }}"
# Prepare deployment artifacts
prepare:
name: Prepare Artifacts
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 15
needs: validate
steps:
- name: Checkout
uses: actions/checkout@v6
with:
ref: ${{ needs.validate.outputs.version }}
- name: Download CI artifacts
uses: actions/download-artifact@v4
continue-on-error: true # Artifacts may not exist for custom versions
with:
pattern: rust-binaries-*
path: artifacts/binaries
merge-multiple: true
- name: Download Docker image
if: needs.validate.outputs.version != github.sha
run: |
# Pull existing Docker image for the version
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.validate.outputs.version }} || \
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest || \
echo "Docker image not found, will build from source"
- name: Install Rust toolchain
if: steps.download.outcome == 'failure'
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache Cargo (self-hosted)
if: steps.download.outcome == 'failure'
uses: actions/cache@v4
with:
path: |
/opt/cargo-cache/registry
/opt/cargo-cache/git
~/.cargo/registry
~/.cargo/git
target
key: deploy-build-${{ needs.validate.outputs.version }}-${{ hashFiles('**/Cargo.lock') }}
env:
CARGO_HOME: /opt/cargo-cache
- name: Build binaries
if: steps.download.outcome == 'failure'
run: |
cargo build --release --package terraphim_server --package terraphim_mcp_server
# Create binaries directory
mkdir -p artifacts/binaries
cp target/release/terraphim* artifacts/binaries/
- name: Upload prepared artifacts
uses: actions/upload-artifact@v4
with:
name: deployment-binaries
path: artifacts/binaries/
retention-days: 7
# Deploy to staging
deploy-staging:
name: Deploy to Staging
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 10
needs: [validate, prepare]
if: needs.validate.outputs.environment == 'staging'
environment:
name: staging
url: ${{ needs.validate.outputs.deployment-url }}
steps:
- name: Download deployment artifacts
uses: actions/download-artifact@v4
with:
name: deployment-binaries
path: ./binaries
- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.STAGING_SSH_KEY }}
- name: Deploy to staging server
run: |
# Configuration
STAGING_HOST="${{ secrets.STAGING_HOST }}"
STAGING_USER="${{ secrets.STAGING_USER }}"
STAGING_PATH="${{ secrets.STAGING_PATH || '/opt/terraphim' }}"
# Create deployment package
tar -czf deployment.tar.gz -C binaries .
# Copy to staging server
scp -o StrictHostKeyChecking=no deployment.tar.gz $STAGING_USER@$STAGING_HOST:/tmp/
# Deploy
ssh -o StrictHostKeyChecking=no $STAGING_USER@$STAGING_HOST << 'EOF'
set -e
# Create backup
if [[ -d "/opt/terraphim" ]]; then
sudo cp -r /opt/terraphim /opt/terraphim.backup.$(date +%s)
fi
# Extract deployment
cd /tmp
tar -xzf deployment.tar.gz
# Stop service
sudo systemctl stop terraphim-server || true
# Deploy files
sudo mkdir -p /opt/terraphim/bin
sudo cp -r /tmp/* /opt/terraphim/bin/
sudo chown -R terraphim:terraphim /opt/terraphim/
sudo chmod +x /opt/terraphim/bin/terraphim*
# Start service
sudo systemctl start terraphim-server
sudo systemctl enable terraphim-server
# Cleanup
rm -f /tmp/deployment.tar.gz /tmp/terraphim*
EOF
- name: Health check
if: github.event.inputs.skip-health-check != 'true' && inputs.skip-health-check != 'true'
run: |
echo "Performing health check..."
# Wait for service to start
for i in {1..30}; do
if curl -f "${{ needs.validate.outputs.deployment-url }}/health" 2>/dev/null; then
echo "✅ Health check passed"
break
fi
echo "Waiting for service... ($i/30)"
sleep 5
done
# Final health check
curl -f "${{ needs.validate.outputs.deployment-url }}/health" || {
echo "❌ Health check failed"
exit 1
}
# Deploy to production
deploy-production:
name: Deploy to Production
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 15
needs: [validate, prepare]
if: needs.validate.outputs.environment == 'production'
environment:
name: production
url: ${{ needs.validate.outputs.deployment-url }}
steps:
- name: Download deployment artifacts
uses: actions/download-artifact@v4
with:
name: deployment-binaries
path: ./binaries
- name: Deploy via Docker Compose (Production)
run: |
echo "Deploying to production using Docker Compose"
# Create docker-compose override for production
cat > docker-compose.override.yml << EOF
version: '3.8'
services:
terraphim:
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.validate.outputs.version }}
restart: always
environment:
- NODE_ENV=production
- LOG_LEVEL=info
ports:
- "80:8080"
deploy:
replicas: 2
resources:
limits:
memory: 2G
cpus: '1.0'
EOF
- name: Production health check
if: github.event.inputs.skip-health-check != 'true' && inputs.skip-health-check != 'true'
run: |
echo "Performing production health check..."
# Extended health check for production
for i in {1..60}; do
if curl -f "${{ needs.validate.outputs.deployment-url }}/health" 2>/dev/null; then
echo "✅ Production health check passed"
break
fi
echo "Waiting for production service... ($i/60)"
sleep 5
done
# Additional production checks
curl -f "${{ needs.validate.outputs.deployment-url }}/health" && \
curl -f "${{ needs.validate.outputs.deployment-url }}/config" || {
echo "❌ Production health check failed"
exit 1
}
# Deploy Docker image
deploy-docker:
name: Deploy Docker Image
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 10
needs: [validate]
if: needs.validate.outputs.environment == 'production'
steps:
- 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: Tag and push production image
run: |
# Pull source image
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.validate.outputs.version }}
# Tag for production
docker tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.validate.outputs.version }} \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:production
# Push production tag
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:production
# Post-deployment notifications
notify:
name: Deployment Notifications
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 5
needs: [validate, deploy-staging, deploy-production]
if: always()
steps:
- name: Notify on success
if: needs.deploy-staging.result == 'success' || needs.deploy-production.result == 'success'
run: |
echo "🚀 Deployment successful!"
echo "Environment: ${{ needs.validate.outputs.environment }}"
echo "Version: ${{ needs.validate.outputs.version }}"
echo "URL: ${{ needs.validate.outputs.deployment-url }}"
- name: Notify on failure
if: needs.deploy-staging.result == 'failure' || needs.deploy-production.result == 'failure'
run: |
echo "❌ Deployment failed!"
echo "Environment: ${{ needs.validate.outputs.environment }}"
echo "Version: ${{ needs.validate.outputs.version }}"
exit 1
- name: Update deployment status
if: github.event_name == 'workflow_call'
run: |
# Update any external deployment tracking systems
echo "Updating deployment status for ${{ needs.validate.outputs.environment }}"
# Rollback on failure
rollback:
name: Rollback on Failure
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 8
needs: [validate, deploy-staging, deploy-production]
if: always() && (needs.deploy-staging.result == 'failure' || needs.deploy-production.result == 'failure') && needs.validate.outputs.is-production == 'true'
steps:
- name: Rollback deployment
run: |
echo "🔄 Rolling back deployment..."
echo "This would typically involve:"
echo "- Restoring previous Docker image"
echo "- Reverting database migrations if needed"
echo "- Restoring configuration files"
echo "- Restarting services"
# Placeholder for actual rollback logic
echo "Rollback completed"