-
Notifications
You must be signed in to change notification settings - Fork 0
100 lines (91 loc) · 3.34 KB
/
codespace-manage.yml
File metadata and controls
100 lines (91 loc) · 3.34 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
name: "Manage: Codespace"
on:
workflow_call:
inputs:
action:
type: string
required: true
description: "Action: start, stop, rebuild, delete"
secrets:
CODESPACE_TOKEN:
required: true
permissions:
contents: read
jobs:
manage:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
GH_TOKEN: ${{ secrets.CODESPACE_TOKEN }}
steps:
- name: Find or create Codespace
id: cs
run: |
NAME=$(gh codespace list --repo "${{ github.repository }}" --json name -q '.[0].name')
ACTION="${{ inputs.action }}"
if [ -z "$NAME" ] && [ "$ACTION" = "start" ]; then
echo "Creating new Codespace..."
NAME=$(gh codespace create -R "${{ github.repository }}" -b master --machine premiumLinux)
fi
if [ -z "$NAME" ]; then
echo "No Codespace found for ${{ github.repository }}"
exit 0
fi
echo "name=$NAME" >> "$GITHUB_OUTPUT"
STATE=$(gh codespace list --json name,state -q ".[] | select(.name==\"$NAME\") | .state")
echo "state=$STATE" >> "$GITHUB_OUTPUT"
echo "Found Codespace: $NAME (state: $STATE)"
- name: Execute action
if: steps.cs.outputs.name
run: |
CS="${{ steps.cs.outputs.name }}"
ACTION="${{ inputs.action }}"
case "$ACTION" in
start)
STATE="${{ steps.cs.outputs.state }}"
if [ "$STATE" = "Available" ]; then
echo "Codespace is already running."
else
echo "Starting Codespace $CS..."
gh codespace ssh -c "$CS" -- true 2>/dev/null || true
for i in $(seq 1 30); do
STATE=$(gh codespace list --json name,state -q ".[] | select(.name==\"$CS\") | .state")
if [ "$STATE" = "Available" ]; then
echo "Codespace is ready."
break
fi
echo " Waiting... ($STATE)"
sleep 10
done
fi
# Output preview URL
URLS=$(gh codespace ssh -c "$CS" -- "/usr/local/share/codespace-network/cs-network-urls.sh" 2>/dev/null) || true
if [ -n "$URLS" ]; then
echo "Preview URLs: $URLS"
fi
;;
stop)
echo "Stopping Codespace $CS..."
gh codespace stop -c "$CS"
echo "Stopped."
;;
delete)
echo "Deleting Codespace $CS..."
gh codespace delete -c "$CS" -f
echo "Deleted."
;;
rebuild)
echo "Deleting Codespace $CS (rebuild = delete + next trigger recreates)..."
gh codespace delete -c "$CS" -f
echo "Deleted. Next trigger will create a fresh Codespace."
;;
import)
echo "Importing production data into Codespace $CS..."
gh codespace ssh -c "$CS" -- "bash -c 'cd /workspaces/* && /usr/local/share/codespace-network/cs-import-production.sh'" 2>&1
echo "Import complete."
;;
*)
echo "::error::Unknown action: $ACTION. Use: start, stop, rebuild, delete, import"
exit 1
;;
esac