Skip to content

Latest commit

 

History

History
128 lines (92 loc) · 3.72 KB

File metadata and controls

128 lines (92 loc) · 3.72 KB

GitHub Actions EC2 Deploy

This repo now includes an EC2 deployment workflow at .github/workflows/deploy-ec2.yml.

What it does:

  • triggers automatically on pushes to master
  • can also be run manually from the GitHub Actions tab
  • connects to your EC2 instance over SSH
  • syncs the repo to the server
  • uploads the runtime .env
  • builds the Docker image on EC2
  • runs python -m bot.profile_runner <paper|live> validate spy on EC2 (or btc if DEPLOY_MARKET=btc)
  • installs a cron job that runs the chosen profile once an hour in America/New_York

Required GitHub Secrets

Create these repository or environment secrets:

  • EC2_HOST: public DNS name or public IP of the instance
  • EC2_USER: SSH user, usually ubuntu
  • EC2_SSH_KEY: private key used by GitHub Actions to SSH into EC2
  • EC2_ENV_FILE: full contents of the server-side .env

Example EC2_ENV_FILE source:

  • start from .env.example
  • fill in your Alpaca keys
  • keep ALPACA_PAPER_* and ALPACA_LIVE_* values there
  • keep only real secrets in this GitHub secret, not in the repo

Optional repository variables:

  • EC2_APP_DIR: defaults to /home/ubuntu/trading-bot
  • EC2_PORT: defaults to 22

One-Time EC2 Setup

Use Ubuntu 22.04 or 24.04 on EC2.

  1. SSH into the instance.
  2. Install Docker, the Compose plugin, and cron:
sudo apt-get update
sudo apt-get install -y docker.io docker-compose-plugin cron
sudo systemctl enable --now docker
sudo systemctl enable --now cron
sudo usermod -aG docker "$USER"
newgrp docker
  1. Create the app directory:
mkdir -p /home/ubuntu/trading-bot
  1. Make sure the SSH user from EC2_USER can log in non-interactively from GitHub Actions.

Recommended path:

  • generate a dedicated SSH key pair for deployment
  • add the public key to ~/.ssh/authorized_keys on EC2
  • store the private key in the EC2_SSH_KEY GitHub secret
  1. Confirm the instance user can run Docker without sudo:
docker version
docker compose version

First Deployment

  1. Push this repo to GitHub with the new workflow files.
  2. Add the secrets listed above.
  3. Open Actions in GitHub.
  4. Run Deploy To EC2.
  5. Choose live or paper.
  6. Leave install_cron enabled unless you want a code-only deploy.

On every later push to master, the workflow will auto-deploy the live profile.

Verify on EC2

Check the installed cron entries:

crontab -l

Check the last deploy logs from GitHub Actions in the Actions tab.

Check runtime output on the server:

ls -la /home/ubuntu/trading-bot/logs
tail -n 50 /home/ubuntu/trading-bot/logs/live_cron.log
tail -n 50 /home/ubuntu/trading-bot/logs/paper_cron.log

If you want to run one profile manually on the server after a deploy:

cd /home/ubuntu/trading-bot
docker compose run --rm trade
docker compose run --rm paper

If you want a different schedule, set CRON_SCHEDULE before running deploy/ec2/install_cron.sh, or edit the script default.

Current default schedule:

  • once an hour, 5 minutes past the hour (5 * * * *)
  • every day
  • America/New_York timezone

The default deploy market is spy (SYMBOL=TSLA, see config/live_spy.env), which trades hourly bars during the equity session — the bot itself checks market hours and holds outside them, so running the cron job around the clock is harmless, just a no-op most of the day. The btc market (config/live_btc.env) also trades hourly bars now, so the same schedule applies; set DEPLOY_MARKET=btc on the workflow dispatch to deploy it instead. If you change a profile's TIMEFRAME_MINUTES, update CRON_SCHEDULE to match — running the bot much more often than its bar interval just wastes API calls and log lines, since cooldown and pending-order checks will no-op the extra invocations.