# Set Up GitHub Actions CI for a SQLAlchemy App

Yes. An agent writes a workflow that starts MySQL and Redis as service containers, runs Alembic and the pytest suite against them, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-github-actions-ci/sqlalchemy/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent works out what the tests need from the outside world, since that is the whole difficulty of CI for a database-backed app. In a SQLAlchemy project like teamhide/fastapi-boilerplate there is no `.github` directory at all. `pyproject.toml` is a Poetry project that pins `python = "3.11.7"` exactly and has a `poetry.lock`. The `Makefile` defines `test` as `ENV=test pytest tests -s`, and `core/config.py` turns `ENV=test` into a `TestConfig` whose writer and reader URLs are `mysql+aiomysql://fastapi:fastapi@localhost:3306/fastapi_test`. The `session` fixture in `tests/conftest.py` calls `TestDbCoordinator.apply_alembic()`, which runs `alembic upgrade head` from `alembic.ini`, and truncates every table afterwards over `pymysql`. The cache tests import a client built from `redis://localhost` and set real keys. Locally all of this comes from `docker/docker-compose.yml`, with `mysql:8.0.33`, `redis:6.2.6` and a `docker/init.sql` that creates `fastapi_test` and grants it to the `fastapi` user.

It writes `.github/workflows/test.yml` to reproduce that environment with service containers. The MySQL service uses the same `mysql:8.0.33` image, user and password as the compose file, publishes port 3306 and gets a health check with `mysqladmin ping` so the job waits for it. One detail does not carry over: compose mounts `init.sql` into the image's init directory, and a service container starts before the checkout step, so the file is not there to mount. The agent sets `MYSQL_DATABASE: fastapi_test` on the service instead, which makes the image create that database and grant it to `MYSQL_USER`, the same end state `init.sql` produces. Redis is `redis:6.2.6` on 6379 with a `redis-cli ping` health check. The steps check out the code, install Poetry, set up Python `3.11.7` with Poetry caching keyed on the lockfile, run `poetry install` and then `poetry run make test`, so CI and a contributor's laptop run the same target.

A second job covers the checks the repository already defines but nothing enforces. `.pre-commit-config.yaml` lists the standard file hooks, `ruff` with `--fix` and `mypy` with its extra type stubs, both excluding `migrations/`, plus `ruff-format` and `isort`. The job runs `pre-commit run --all-files --show-diff-on-failure`. The agent runs that command in the sandbox first and reads what it reports on the current tree. If the hooks want to rewrite files, it puts those rewrites in a separate commit so a reviewer can keep or drop them without touching the workflow. The workflow triggers on pull requests and on pushes to the default branch, sets `permissions: contents: read`, cancels superseded runs with a `concurrency` group and gives each job a timeout.

A workflow file cannot be executed in a sandbox, but everything it does can. The agent installs Docker with `sudo apt-get install -y docker.io`, starts `dockerd`, and runs the two images with exactly the environment and ports the workflow's `services` block declares, without `init.sql`, to confirm that `MYSQL_DATABASE` alone is enough for `alembic upgrade head` and the truncation to work as the `fastapi` user. It installs Python 3.11.7 and Poetry, runs `poetry install` and `make test`, and records the result. Then it pushes the branch and opens the pull request, where the new workflow runs for the first time as that pull request's own check. A badge and a short CI section go into `README.md` under the existing test instructions.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Set up GitHub Actions CI for this SQLAlchemy project. Read pyproject.toml, poetry.lock (versions only), the Makefile, pytest.ini, core/config.py, core/helpers/redis.py, tests/conftest.py, tests/support/test_db_coordinator.py, migrations/env.py, docker/docker-compose.yml, docker/init.sql, .pre-commit-config.yaml and README.md first. Write .github/workflows/test.yml with two jobs. The test job uses service containers for mysql:8.0.33 and redis:6.2.6 with the same credentials and ports as the compose file and health checks on both; service containers start before checkout and cannot mount docker/init.sql, so create the fastapi_test database with MYSQL_DATABASE instead. It checks out, installs Poetry, sets up Python 3.11.7 with Poetry caching, runs poetry install and poetry run make test. The lint job runs pre-commit run --all-files --show-diff-on-failure. Trigger on pull_request and on push to the default branch, set permissions to contents: read, add a concurrency group that cancels superseded runs and a timeout per job. Before pushing, prove it in the sandbox: install Docker, start both images with the workflow's exact environment and no init.sql, install Python 3.11.7 and Poetry, run make test and the pre-commit command, and report the results. If pre-commit rewrites files, commit those separately. Add a badge and a CI section to the README and open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that adds `.github/workflows/test.yml` and the README lines, plus a separate commit of hook rewrites if the hooks produced any. Its description explains each service's settings against the line of `docker/docker-compose.yml` it came from, states why `MYSQL_DATABASE` replaces `init.sql`, and includes the sandbox output of `make test` and of the pre-commit run on the tree as it was. The workflow's two jobs appear as checks on the pull request itself, so the reviewer sees its first real result without merging anything.

A reviewer checks the things CI gets wrong quietly. That the health checks are present, because without them the first test can reach MySQL before it accepts connections and the failure looks like a flaky test. That the Python version in the workflow is the one `pyproject.toml` pins. That the lint job fails on a real violation and does not merely report it. And they decide whether an existing pre-commit.ci integration, which the `ci:` block at the bottom of `.pre-commit-config.yaml` configures, should stay alongside the new lint job or be replaced by it; the pull request points the block out and leaves it in place.

## Run it on every terminal hand-off

This is setup work that sits undone because it needs an hour of waiting on containers. A developer sends this body from the terminal with the repository in `resources` and returns to the feature branch they were on. For a group of services built on the same boilerplate, a short script sends one request per repository; each run reads that repository's own compose file and test config, so a service on Postgres or with a RabbitMQ dependency gets the services it needs and not a copy of this file.

Put a `request_id`, a UUID, in each body so that re-running the script returns the runs already started. When a turn ends, `GET /o/{org}/sessions/{id}` names the pull request. If the workflow's first run on GitHub fails where the sandbox passed, a follow-up message to the same session with the failing job's log wakes the parked sandbox, which still has both images pulled and the dependencies installed, to find the difference.

## Why run it on Rush?

CI for a database app is usually debugged by pushing commits and waiting, one typo per round trip. The agent does the debugging before the first push: in its own sandbox it starts the same images with the same environment the workflow declares and runs the real suite against them, and each command and its output is kept in the run's record. The assumption most likely to be wrong, that the image's own database variable can stand in for the init script, is tested directly instead of discovered on the third red run.

It also keeps the experiment off a developer's machine. Nobody has to stop their local MySQL to free port 3306 or wonder whether a passing run depended on data already in their `fastapi_test` database. The sandbox starts from nothing, as a GitHub runner does, so what passes there is a fair prediction of the check on the pull request.

## Frequently asked questions

### Why service containers and not docker compose in the job?

Service containers give each job a fresh MySQL and Redis, wait on their health checks before the steps start, and are torn down afterwards with no cleanup step. Running the compose file inside the job also works, but then the workflow has to wait for readiness itself. The pull request notes the trade-off.

### How does the test database get created without init.sql?

The MySQL image creates the database named in `MYSQL_DATABASE` on first start and grants `MYSQL_USER` full access to it. Setting it to `fastapi_test` gives the tests what `docker/init.sql` gives them locally. The agent confirms this in the sandbox by starting the image that way and running the suite.

### Do the tests really need Redis in CI?

In this project, yes. The cache backend tests under `tests/core/helpers/cache/` use a client built from `redis://localhost` and write and read real keys. Without a Redis service on 6379 those tests fail on connection, so the workflow starts one.

### Will the lint job reformat my code?

No. In CI the hooks run with `--show-diff-on-failure` and fail the job if they would change a file; nothing is pushed back. Any rewrites the hooks produce on the current tree are made once by the agent, in their own commit, for the reviewer to accept or drop.

## More SQLAlchemy jobs

- [Add Query Timeouts to a Flask and SQLAlchemy App](https://getrush.ai/run/add-query-timeouts/sqlalchemy/)
- [Add Slow Query Logging to a Flask + SQLAlchemy App](https://getrush.ai/run/add-slow-query-logging/sqlalchemy/)
- [Add Soft Delete to SQLAlchemy Models](https://getrush.ai/run/add-soft-delete/sqlalchemy/)

## The same job on other stacks

- [Set Up GitHub Actions CI for a Strapi App](https://getrush.ai/run/add-github-actions-ci/strapi/)
- [Set Up GitHub Actions CI for a Supabase Project](https://getrush.ai/run/add-github-actions-ci/supabase/)
- [Set Up GitHub Actions CI for a SvelteKit App](https://getrush.ai/run/add-github-actions-ci/sveltekit/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
