Auto Stop (if empty) #7
This file contains hidden or 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 Stop (if empty) | |
| on: | |
| schedule: | |
| - cron: "*/30 * * * *" # every 30 minutes | |
| jobs: | |
| check-and-stop: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: "1.7.0" | |
| - name: Terraform Init | |
| working-directory: terraform | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.DO_SPACES_KEY }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.DO_SPACES_SECRET }} | |
| run: terraform init | |
| - name: Check if server is running | |
| id: check | |
| working-directory: terraform | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.DO_SPACES_KEY }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.DO_SPACES_SECRET }} | |
| TF_VAR_do_token: ${{ secrets.DO_TOKEN }} | |
| TF_VAR_ssh_public_key: ${{ secrets.SSH_PUBLIC_KEY }} | |
| TF_VAR_repo_url: ${{ github.server_url }}/${{ github.repository }} | |
| run: | | |
| IP=$(terraform output -raw server_ip 2>/dev/null || echo "") | |
| echo "ip=$IP" >> $GITHUB_OUTPUT | |
| [ -n "$IP" ] && echo "running=true" >> $GITHUB_OUTPUT || echo "running=false" >> $GITHUB_OUTPUT | |
| - name: Check player activity via SSH | |
| if: steps.check.outputs.running == 'true' | |
| id: activity | |
| env: | |
| SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| LAST=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 \ | |
| root@${{ steps.check.outputs.ip }} \ | |
| "grep -E 'RemoteRole|joined|left|PlayerController' \ | |
| /home/astroneer/astro-server/Astro/Saved/Logs/AstroServer.log 2>/dev/null \ | |
| | tail -1 | grep -oP '\[\d{4}\.\d{2}\.\d{2}-\d{2}\.\d{2}\.\d{2}' | tr -d '[' \ | |
| || echo ''" 2>/dev/null || echo "") | |
| UPTIME=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 \ | |
| root@${{ steps.check.outputs.ip }} \ | |
| "awk '{print int(\$1)}' /proc/uptime" 2>/dev/null || echo "0") | |
| echo "last_activity=$LAST" >> $GITHUB_OUTPUT | |
| echo "uptime=$UPTIME" >> $GITHUB_OUTPUT | |
| - name: Decide whether to stop | |
| if: steps.check.outputs.running == 'true' | |
| id: decide | |
| run: | | |
| LAST="${{ steps.activity.outputs.last_activity }}" | |
| UPTIME="${{ steps.activity.outputs.uptime }}" | |
| NOW=$(date +%s) | |
| if [ -z "$LAST" ]; then | |
| # No activity in logs — stop if up more than 1 hour | |
| if [ "$UPTIME" -gt 3600 ]; then | |
| echo "stop=true" >> $GITHUB_OUTPUT | |
| echo "reason=No player activity and server up > 1 hour" >> $GITHUB_OUTPUT | |
| else | |
| echo "stop=false" >> $GITHUB_OUTPUT | |
| fi | |
| else | |
| LAST_TS=$(date -d "$(echo $LAST | sed 's/\./\-/g' | sed 's/-/:/5' | sed 's/-/:/6')" +%s 2>/dev/null || echo "0") | |
| IDLE=$((NOW - LAST_TS)) | |
| if [ "$IDLE" -gt 3600 ]; then | |
| echo "stop=true" >> $GITHUB_OUTPUT | |
| echo "reason=No player activity for over 60 minutes" >> $GITHUB_OUTPUT | |
| else | |
| echo "stop=false" >> $GITHUB_OUTPUT | |
| fi | |
| fi | |
| - name: Destroy idle server | |
| if: steps.check.outputs.running == 'true' && steps.decide.outputs.stop == 'true' | |
| working-directory: terraform | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.DO_SPACES_KEY }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.DO_SPACES_SECRET }} | |
| TF_VAR_do_token: ${{ secrets.DO_TOKEN }} | |
| TF_VAR_ssh_public_key: ${{ secrets.SSH_PUBLIC_KEY }} | |
| TF_VAR_repo_url: ${{ github.server_url }}/${{ github.repository }} | |
| SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| ssh -o StrictHostKeyChecking=no root@${{ steps.check.outputs.ip }} \ | |
| "systemctl stop astroneer" || true | |
| terraform destroy -auto-approve \ | |
| -target=digitalocean_droplet.astro \ | |
| -target=digitalocean_volume_attachment.saves \ | |
| -target=digitalocean_firewall.astro \ | |
| -target=digitalocean_ssh_key.astro | |
| echo "Auto-stopped: ${{ steps.decide.outputs.reason }}" | |
| - name: No action needed | |
| if: steps.check.outputs.running == 'false' || steps.decide.outputs.stop == 'false' | |
| run: echo "No action needed." |