-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
484 lines (433 loc) · 14.6 KB
/
action.yml
File metadata and controls
484 lines (433 loc) · 14.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
name: 'fleetd Deploy'
description: 'Deploy software to your fleet of devices using fleetd'
author: 'fleetd'
branding:
icon: 'upload-cloud'
color: 'blue'
inputs:
api_url:
description: 'fleetd API URL'
required: true
api_key:
description: 'fleetd API key for authentication'
required: true
# Deployment identification
name:
description: 'Deployment name'
required: true
description:
description: 'Deployment description'
required: false
default: 'Deployment from GitHub Actions'
# Deployment type
type:
description: 'Deployment type (binary, script, container, config, firmware, package, bundle)'
required: true
default: 'binary'
# Payload configuration (depends on type)
artifact_url:
description: 'URL to download the artifact'
required: false
artifact_path:
description: 'Local path to the artifact'
required: false
version:
description: 'Version of the deployment'
required: false
default: '${{ github.sha }}'
checksum:
description: 'Checksum of the artifact'
required: false
# Script specific
script_content:
description: 'Inline script content (for script deployments)'
required: false
interpreter:
description: 'Script interpreter (e.g., /bin/bash, /usr/bin/python3)'
required: false
default: '/bin/bash'
# Container specific
container_image:
description: 'Container image (for container deployments)'
required: false
container_name:
description: 'Container name to update'
required: false
# Target configuration
target_devices:
description: 'Comma-separated device IDs'
required: false
target_groups:
description: 'Comma-separated group IDs'
required: false
target_labels:
description: 'Label selector (JSON format)'
required: false
target_query:
description: 'Custom query selector'
required: false
target_filters:
description: 'Target filters (JSON format)'
required: false
max_devices:
description: 'Maximum number of devices to target'
required: false
# Strategy
strategy:
description: 'Deployment strategy (immediate, rolling, canary, blue-green)'
required: false
default: 'rolling'
# Rollout configuration
batch_size:
description: 'Number of devices per batch'
required: false
default: '10'
pause_seconds:
description: 'Pause between batches in seconds'
required: false
default: '60'
canary_size:
description: 'Size of canary deployment'
required: false
default: '1'
# Health check
health_check_enabled:
description: 'Enable health checks'
required: false
default: 'true'
health_check_timeout:
description: 'Health check timeout in seconds'
required: false
default: '60'
# Rollback
auto_rollback:
description: 'Enable automatic rollback on failure'
required: false
default: 'true'
failure_threshold:
description: 'Failure percentage to trigger rollback'
required: false
default: '0.1'
# Notifications
webhook_urls:
description: 'Comma-separated webhook URLs for notifications'
required: false
# Behavior
auto_start:
description: 'Start deployment immediately'
required: false
default: 'true'
wait_for_completion:
description: 'Wait for deployment to complete'
required: false
default: 'true'
timeout_minutes:
description: 'Deployment timeout in minutes'
required: false
default: '60'
# Environment
environment:
description: 'Target environment (dev, staging, production)'
required: false
default: 'production'
outputs:
deployment_id:
description: 'The ID of the created deployment'
value: ${{ steps.deploy.outputs.deployment_id }}
status:
description: 'Final deployment status'
value: ${{ steps.status.outputs.status }}
message:
description: 'Deployment message'
value: ${{ steps.status.outputs.message }}
devices_deployed:
description: 'Number of devices successfully deployed'
value: ${{ steps.status.outputs.devices_deployed }}
runs:
using: 'composite'
steps:
- name: Setup fleetctl
shell: bash
run: |
echo "Installing fleetctl..."
curl -sSL https://get.fleetd.sh | sh
echo "$HOME/.fleetd/bin" >> $GITHUB_PATH
- name: Prepare deployment configuration
id: prepare
shell: bash
run: |
cat > deployment.json <<'EOF'
{
"name": "${{ inputs.name }}",
"description": "${{ inputs.description }}",
"type": "DEPLOYMENT_TYPE_${{ inputs.type }}",
"payload": $(cat <<'PAYLOAD'
EOF
# Build payload based on type
case "${{ inputs.type }}" in
binary)
cat >> deployment.json <<EOF
{
"binary": {
"download_url": "${{ inputs.artifact_url }}",
"checksum": "${{ inputs.checksum }}",
"version": "${{ inputs.version }}",
"restart_on_update": true
}
}
EOF
;;
script)
if [ -n "${{ inputs.script_content }}" ]; then
SCRIPT_CONTENT=$(echo "${{ inputs.script_content }}" | jq -Rs .)
else
SCRIPT_CONTENT='""'
fi
cat >> deployment.json <<EOF
{
"script": {
"script_content": ${SCRIPT_CONTENT},
"script_url": "${{ inputs.artifact_url }}",
"interpreter": "${{ inputs.interpreter }}",
"timeout_seconds": 300
}
}
EOF
;;
container)
cat >> deployment.json <<EOF
{
"container": {
"image": "${{ inputs.container_image }}",
"container_name": "${{ inputs.container_name }}",
"restart_policy": "RESTART_POLICY_UNLESS_STOPPED"
}
}
EOF
;;
*)
echo "Unsupported deployment type: ${{ inputs.type }}"
exit 1
;;
esac
cat >> deployment.json <<'EOF'
PAYLOAD
),
"target": $(cat <<'TARGET'
EOF
# Build target selector
if [ -n "${{ inputs.target_devices }}" ]; then
cat >> deployment.json <<EOF
{
"devices": {
"device_ids": [$(echo "${{ inputs.target_devices }}" | sed 's/,/","/g' | sed 's/^/"/;s/$/"/')]
}
}
EOF
elif [ -n "${{ inputs.target_groups }}" ]; then
cat >> deployment.json <<EOF
{
"groups": {
"group_ids": [$(echo "${{ inputs.target_groups }}" | sed 's/,/","/g' | sed 's/^/"/;s/$/"/')]
}
}
EOF
elif [ -n "${{ inputs.target_labels }}" ]; then
cat >> deployment.json <<EOF
{
"labels": ${{ inputs.target_labels }}
}
EOF
elif [ -n "${{ inputs.target_query }}" ]; then
cat >> deployment.json <<EOF
{
"query": {
"expression": "${{ inputs.target_query }}"
}
}
EOF
else
echo "No target specified"
exit 1
fi
# Add filters if provided
if [ -n "${{ inputs.target_filters }}" ]; then
cat >> deployment.json <<EOF
,
"filters": ${{ inputs.target_filters }}
EOF
fi
if [ -n "${{ inputs.max_devices }}" ]; then
cat >> deployment.json <<EOF
,
"max_devices": ${{ inputs.max_devices }}
EOF
fi
cat >> deployment.json <<'EOF'
TARGET
),
"strategy": "DEPLOYMENT_STRATEGY_${{ inputs.strategy }}",
"config": {
"rollout": {
"batch_size": ${{ inputs.batch_size }},
"pause_seconds": ${{ inputs.pause_seconds }},
"canary_size": ${{ inputs.canary_size }}
},
"health_check": {
"enabled": ${{ inputs.health_check_enabled }},
"timeout_seconds": ${{ inputs.health_check_timeout }}
},
"rollback": {
"auto_rollback": ${{ inputs.auto_rollback }},
"failure_threshold": ${{ inputs.failure_threshold }}
},
"timeout_minutes": ${{ inputs.timeout_minutes }}
EOF
# Add notifications if provided
if [ -n "${{ inputs.webhook_urls }}" ]; then
cat >> deployment.json <<EOF
,
"notifications": {
"webhook_urls": [$(echo "${{ inputs.webhook_urls }}" | sed 's/,/","/g' | sed 's/^/"/;s/$/"/')]
}
EOF
fi
cat >> deployment.json <<EOF
},
"metadata": {
"source": "github-action",
"source_ref": "${{ github.run_id }}",
"commit_sha": "${{ github.sha }}",
"branch": "${{ github.ref_name }}",
"repository": "${{ github.repository }}",
"triggered_by": "${{ github.actor }}",
"environment": "${{ inputs.environment }}",
"tags": {
"workflow": "${{ github.workflow }}",
"run_number": "${{ github.run_number }}",
"event": "${{ github.event_name }}"
}
},
"auto_start": ${{ inputs.auto_start }}
}
EOF
# Validate JSON
jq . deployment.json > /dev/null || (echo "Invalid deployment configuration" && exit 1)
echo "Deployment configuration prepared"
- name: Create deployment
id: deploy
shell: bash
env:
FLEETD_API_URL: ${{ inputs.api_url }}
FLEETD_API_KEY: ${{ inputs.api_key }}
run: |
echo "Creating deployment..."
RESPONSE=$(curl -X POST \
-H "Authorization: Bearer $FLEETD_API_KEY" \
-H "Content-Type: application/json" \
-d @deployment.json \
"$FLEETD_API_URL/api/v1/deployments")
DEPLOYMENT_ID=$(echo "$RESPONSE" | jq -r '.deployment.id')
if [ -z "$DEPLOYMENT_ID" ] || [ "$DEPLOYMENT_ID" = "null" ]; then
echo "Failed to create deployment"
echo "$RESPONSE" | jq .
exit 1
fi
echo "deployment_id=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT
echo "Deployment created: $DEPLOYMENT_ID"
# Show warnings if any
WARNINGS=$(echo "$RESPONSE" | jq -r '.warnings[]?.message' 2>/dev/null)
if [ -n "$WARNINGS" ]; then
echo "⚠️ Warnings:"
echo "$WARNINGS"
fi
- name: Monitor deployment
id: monitor
if: inputs.wait_for_completion == 'true'
shell: bash
env:
FLEETD_API_URL: ${{ inputs.api_url }}
FLEETD_API_KEY: ${{ inputs.api_key }}
run: |
DEPLOYMENT_ID="${{ steps.deploy.outputs.deployment_id }}"
TIMEOUT_SECONDS=$(( ${{ inputs.timeout_minutes }} * 60 ))
START_TIME=$(date +%s)
echo "Monitoring deployment $DEPLOYMENT_ID..."
while true; do
# Get deployment status
RESPONSE=$(curl -s \
-H "Authorization: Bearer $FLEETD_API_KEY" \
"$FLEETD_API_URL/api/v1/deployments/$DEPLOYMENT_ID/status")
STATE=$(echo "$RESPONSE" | jq -r '.state')
PROGRESS=$(echo "$RESPONSE" | jq -r '.progress')
if [ -n "$PROGRESS" ] && [ "$PROGRESS" != "null" ]; then
TOTAL=$(echo "$PROGRESS" | jq -r '.total_devices')
SUCCEEDED=$(echo "$PROGRESS" | jq -r '.succeeded_devices')
FAILED=$(echo "$PROGRESS" | jq -r '.failed_devices')
PERCENTAGE=$(echo "$PROGRESS" | jq -r '.percentage_complete')
echo "Progress: ${PERCENTAGE}% - Success: $SUCCEEDED/$TOTAL, Failed: $FAILED"
fi
# Check terminal states
case "$STATE" in
DEPLOYMENT_STATE_COMPLETED)
echo "✅ Deployment completed successfully!"
echo "status=completed" >> $GITHUB_OUTPUT
echo "devices_deployed=$SUCCEEDED" >> $GITHUB_OUTPUT
exit 0
;;
DEPLOYMENT_STATE_FAILED|DEPLOYMENT_STATE_CANCELLED|DEPLOYMENT_STATE_ROLLED_BACK)
echo "❌ Deployment $STATE"
echo "status=$STATE" >> $GITHUB_OUTPUT
echo "devices_deployed=$SUCCEEDED" >> $GITHUB_OUTPUT
# Get failure details
EVENTS=$(curl -s \
-H "Authorization: Bearer $FLEETD_API_KEY" \
"$FLEETD_API_URL/api/v1/deployments/$DEPLOYMENT_ID?include_events=true" \
| jq -r '.events[] | select(.severity == "error") | .message')
if [ -n "$EVENTS" ]; then
echo "Errors:"
echo "$EVENTS"
fi
exit 1
;;
esac
# Check timeout
CURRENT_TIME=$(date +%s)
ELAPSED=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED -gt $TIMEOUT_SECONDS ]; then
echo "⏱️ Deployment timed out after ${{ inputs.timeout_minutes }} minutes"
echo "status=timeout" >> $GITHUB_OUTPUT
exit 1
fi
# Wait before next check
sleep 10
done
- name: Get final status
id: status
if: always()
shell: bash
env:
FLEETD_API_URL: ${{ inputs.api_url }}
FLEETD_API_KEY: ${{ inputs.api_key }}
run: |
if [ -n "${{ steps.deploy.outputs.deployment_id }}" ]; then
DEPLOYMENT_ID="${{ steps.deploy.outputs.deployment_id }}"
RESPONSE=$(curl -s \
-H "Authorization: Bearer $FLEETD_API_KEY" \
"$FLEETD_API_URL/api/v1/deployments/$DEPLOYMENT_ID")
STATE=$(echo "$RESPONSE" | jq -r '.deployment.state')
echo "Final deployment state: $STATE"
echo "status=$STATE" >> $GITHUB_OUTPUT
# Create summary
echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- **Deployment ID:** $DEPLOYMENT_ID" >> $GITHUB_STEP_SUMMARY
echo "- **Name:** ${{ inputs.name }}" >> $GITHUB_STEP_SUMMARY
echo "- **Type:** ${{ inputs.type }}" >> $GITHUB_STEP_SUMMARY
echo "- **Strategy:** ${{ inputs.strategy }}" >> $GITHUB_STEP_SUMMARY
echo "- **Environment:** ${{ inputs.environment }}" >> $GITHUB_STEP_SUMMARY
echo "- **Status:** $STATE" >> $GITHUB_STEP_SUMMARY
if [ -n "${{ steps.monitor.outputs.devices_deployed }}" ]; then
echo "- **Devices Deployed:** ${{ steps.monitor.outputs.devices_deployed }}" >> $GITHUB_STEP_SUMMARY
fi
fi