fix: keep ibkr 2fa bot running after restarts #75
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 | |
| # Install host-level watcher so the bot is restored after VM/container restarts. | |
| sudo bash ./scripts/install_2fa_bot_watcher.sh | |
| sleep 5 | |
| sudo systemctl status ibkr-2fa-bot.timer --no-pager | |
| 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." |