Auto Restart and Keep Codespace Active #3926
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto Restart and Keep Codespace Active | |
on: | |
schedule: | |
- cron: '* * * * *' # Runs every minute | |
workflow_dispatch: | |
jobs: | |
restart_codespace: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Install GitHub CLI if not present | |
run: | | |
if ! command -v gh &> /dev/null; then | |
sudo apt-get install gh jq | |
fi | |
- name: Authenticate GitHub CLI with Personal Access Token | |
run: | | |
echo "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" | gh auth login --with-token | |
- name: Check Codespace Status and Keep Alive | |
run: | | |
CODESPACE_NAME="fuzzy-parakeet-jjrgxrjppxw5hqvp6" | |
STATUS=$(gh codespace list --json name,state | jq -r --arg name "$CODESPACE_NAME" '.[] | select(.name == $name) | .state') | |
if [ -z "$STATUS" ]; then | |
echo "Error fetching codespace status or codespace does not exist." | |
exit 1 | |
fi | |
echo "Codespace status: $STATUS" | |
if [ "$STATUS" == "Shutdown" ]; then | |
echo "Codespace is offline. Attempting to start it by SSH..." | |
gh codespace ssh --codespace $CODESPACE_NAME -- echo 'Codespace started!' | |
else | |
echo "Codespace is active. Sending keep-alive signal..." | |
gh codespace ssh --codespace $CODESPACE_NAME -- echo 'Keep-alive signal' | |
fi | |