Align auto-deploy workflow with Cloud Run private IP settings #74
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: Deploy and Keep Active | |
| on: | |
| push: | |
| branches: [ main ] | |
| # Runs automatically every day at 0:00 UTC (8:00 AM Beijing Time) | |
| schedule: | |
| - cron: '0 0 * * *' | |
| workflow_dispatch: | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| env: | |
| GCE_INSTANCE_NAME: ${{ secrets.GCE_INSTANCE_NAME || 'interactive-brokers-quant-instance' }} | |
| GCE_ZONE: ${{ secrets.GCE_ZONE || 'us-east1-c' }} | |
| DEPLOY_PATH: ${{ secrets.DEPLOY_PATH || '/home/zwlddx0815/ib-docker' }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Authenticate to Google Cloud | |
| uses: google-github-actions/auth@v2 | |
| with: | |
| credentials_json: ${{ secrets.GCP_SA_KEY }} | |
| - name: Deploy and Log Setup | |
| uses: google-github-actions/ssh-compute@v0 | |
| with: | |
| instance_name: ${{ env.GCE_INSTANCE_NAME }} | |
| zone: ${{ env.GCE_ZONE }} | |
| ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| command: | | |
| cd ${{ env.DEPLOY_PATH }} | |
| sudo git fetch --all | |
| sudo git reset --hard origin/main | |
| # Write environment configurations | |
| echo "TWS_USERID='${{ secrets.TWS_USERID }}'" > .env | |
| echo "TWS_PASSWORD='${{ secrets.TWS_PASSWORD }}'" >> .env | |
| echo "TOTP_SECRET='${{ secrets.TOTP_SECRET }}'" >> .env | |
| echo "VNC_SERVER_PASSWORD='${{ secrets.VNC_SERVER_PASSWORD }}'" >> .env | |
| echo "TRADING_MODE='${{ secrets.TRADING_MODE }}'" >> .env | |
| echo "ACCEPT_API_FROM_IP='${{ secrets.ACCEPT_API_FROM_IP || '10.8.0.0/28' }}'" >> .env | |
| echo "ALLOW_CONNECTIONS_FROM_LOCALHOST_ONLY='${{ secrets.ALLOW_CONNECTIONS_FROM_LOCALHOST_ONLY || 'no' }}'" >> .env | |
| # Build image (cached after first build, fast on subsequent runs) | |
| sudo docker compose build | |
| # Restart containers | |
| sudo docker compose down | |
| sudo docker compose up -d | |
| # Wait for the container to stabilize | |
| echo "Waiting for container to stabilize..." | |
| sleep 30 | |
| # --- Start Bot and ensure it runs as a daemon --- | |
| # 1. Kill any existing bot processes to avoid conflicts | |
| sudo docker exec ib-gateway pkill -f 2fa_bot.py || true | |
| # 2. Start the bot using nohup for background persistence | |
| sudo docker exec -d ib-gateway bash -c "nohup python3 /home/ibgateway/2fa_bot.py > /home/ibgateway/2fa.log 2>&1 &" | |
| # 3. Verify bot started successfully | |
| sleep 5 | |
| sudo docker exec ib-gateway pgrep -f 2fa_bot.py && echo "✅ Bot started successfully." || echo "⚠️ Warning: Bot may not have started, check logs." | |
| echo "✅ Deployment complete. Bot is monitoring the login window." | |
| echo "Run 'docker exec ib-gateway tail -f /home/ibgateway/2fa.log' for real-time status." |