Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions .github/workflows/ghcr-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
# Workflow that builds and pushes the automation docker image to ghcr.io
name: Docker

on:
push:
branches:
- main
tags:
- '*'
pull_request:
workflow_dispatch:
inputs:
reason:
description: Reason for manual trigger
required: true
default: ''

# If triggered by a PR, it will be in the same group. However, each commit on main will be in its own unique group
concurrency:
group: ${{ github.workflow }}-${{ (github.head_ref && github.ref) || github.run_id }}
cancel-in-progress: true

env:
REGISTRY: ghcr.io
IMAGE_NAME: openhands/automation
RELEVANT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}

jobs:
ghcr_build:
name: Build and Push Automation Image
runs-on: ubuntu-24.04
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host

- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get short SHA
id: short_sha
run: echo "SHORT_SHA=$(echo ${{ env.RELEVANT_SHA }} | cut -c1-7)" >> $GITHUB_OUTPUT

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha
type=sha,format=long
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
flavor: |
latest=auto
prefix=
suffix=
env:
DOCKER_METADATA_PR_HEAD_SHA: true

- name: Build and push Docker image
if: '!github.event.pull_request.head.repo.fork'
uses: docker/build-push-action@v5
with:
context: .
file: containers/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max
provenance: true
sbom: true

# Forked repos can't push to GHCR, so we just build to verify the image builds
- name: Build Docker image (fork)
if: github.event.pull_request.head.repo.fork
uses: docker/build-push-action@v5
with:
context: .
file: containers/Dockerfile
push: false
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.RELEVANT_SHA }}
platforms: linux/amd64
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ jobs:
CI=true uv run python -m pytest -vvs \
--cov=automation \
--cov-report=term-missing \
--cov-report=xml:coverage.xml \
--cov-fail-under=0 \
tests/unit
tests/

- name: Pytest coverage PR comment
if: always() && github.event_name == 'pull_request'
Expand Down
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
repos:
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.2.1 # or other specific tag
rev: 0.2.1
hooks:
- id: yamlfmt
exclude: ^(migrations/|alembic\.ini)
- repo: local
hooks:
- id: ruff-format
Expand Down
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ UNDERLINE := \033[4m
# Required uv version
REQUIRED_UV_VERSION := 0.8.13

.PHONY: build format lint clean help check-uv-version
.PHONY: build format lint clean help check-uv-version docker-build docker-run

# Default target
.DEFAULT_GOAL := help
Expand Down Expand Up @@ -68,6 +68,16 @@ clean:
@$(ECHO) "$(GREEN)Cache files cleaned.$(RESET)"


docker-build:
@$(ECHO) "$(YELLOW)Building Docker image...$(RESET)"
@docker build -t ghcr.io/openhands/automation:local -f containers/Dockerfile .
@$(ECHO) "$(GREEN)Docker image built successfully.$(RESET)"

docker-run:
@$(ECHO) "$(YELLOW)Running Docker container...$(RESET)"
@docker run -p 8000:8000 ghcr.io/openhands/automation:local
@$(ECHO) "$(GREEN)Docker container running.$(RESET)"

# Show help
help:
@$(ECHO) "$(CYAN)OpenHands Automation Makefile$(RESET)"
Expand All @@ -80,5 +90,7 @@ help:
@$(ECHO) " $(GREEN)lint$(RESET) Lint code with ruff"
@$(ECHO) " $(GREEN)pre-commit$(RESET) Run the pre-commit"
@$(ECHO) " $(GREEN)test$(RESET) Run tests"
@$(ECHO) " $(GREEN)docker-build$(RESET) Build Docker image"
@$(ECHO) " $(GREEN)docker-run$(RESET) Run Docker container"
@$(ECHO) " $(GREEN)clean$(RESET) Clean up cache files"
@$(ECHO) " $(GREEN)help$(RESET) Show this help message"
93 changes: 78 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,94 @@
# OpenHands Automation
# OpenHands Automation Service

OpenHands automation service.
Scheduled and event-driven automation execution for OpenHands Cloud. This service allows users to create automations that run on a schedule (cron) or in response to events (webhooks).

## Development Setup
## Features

- **Scheduled Automations**: Run OpenHands conversations on a cron schedule
- **Event-Driven**: Trigger automations via webhooks (e.g., GitHub events)
- **API Key Management**: Per-user API keys for secure automation access
- **Run History**: Track automation runs with status and results

## Development

### Prerequisites

- Python 3.12+
- [uv](https://github.com/astral-sh/uv) for dependency management
- PostgreSQL (or use testcontainers for testing)

### Setup

```bash
# Install uv (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies
uv sync --group dev

# Run the service locally (requires PostgreSQL)
uv run uvicorn automation.app:app --host 0.0.0.0 --port 8000 --reload
```

# Set up the development environment
make build
### Testing

# Run tests
make test
```bash
# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov=automation --cov-report=term-missing
```

### Code Quality

```bash
# Run pre-commit hooks
uv run pre-commit run --all-files

# Format code
make format
uv run ruff format

# Lint code
make lint
uv run ruff check --fix

# Type check
uv run pyright
```

### Database Migrations

```bash
# Create a new migration
uv run alembic revision --autogenerate -m "description"

# Apply migrations
uv run alembic upgrade head
```

## Docker

```bash
# Build the image
docker build -t automation -f containers/Dockerfile .

# Run the container
docker run -p 8000:8000 automation
```

## Project Structure

```
automation/ # Main package
tests/
β”œβ”€β”€ conftest.py # Shared test fixtures
└── unit/ # Unit tests
automation/
β”œβ”€β”€ app.py # FastAPI application entry point
β”œβ”€β”€ router.py # API routes for CRUD operations
β”œβ”€β”€ scheduler.py # Background scheduler for cron jobs
β”œβ”€β”€ dispatcher.py # Dispatches pending runs to OpenHands
β”œβ”€β”€ models.py # SQLAlchemy models
β”œβ”€β”€ schemas.py # Pydantic schemas for API
└── utils/ # Utility functions
migrations/ # Alembic database migrations
tests/ # Unit tests
containers/ # Docker configuration
```

## Deployment

This service is deployed via the [deploy repository](https://github.com/All-Hands-AI/deploy). Docker images are automatically built and pushed to `ghcr.io/openhands/automation` on every push to main and on tags.
37 changes: 37 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[alembic]
script_location = migrations
sqlalchemy.url = driver://user:pass@localhost/dbname

[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
Loading
Loading