Skip to content
Closed
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
1 change: 1 addition & 0 deletions .dagger/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/sdk/** linguist-generated
4 changes: 4 additions & 0 deletions .dagger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.venv
/**/__pycache__
/sdk
/.env
12 changes: 12 additions & 0 deletions .dagger/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[project]
name = "book"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = ["dagger-io"]

[build-system]
requires = ["uv_build>=0.8.4,<0.9.0"]
build-backend = "uv_build"

[tool.uv.sources]
dagger-io = { path = "sdk", editable = true }
16 changes: 16 additions & 0 deletions .dagger/src/book/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""A generated module for Book functions

This module has been generated via dagger init and serves as a reference to
basic module structure as you get started with Dagger.

Two functions have been pre-created. You can modify, delete, or add to them,
as needed. They demonstrate usage of arguments and return types using simple
echo and grep commands. The functions can be called from the dagger CLI or
from one of the SDKs.

The first line in this comment block is a short description line and the
rest is a long description with more detail on the module's purpose or usage,
if appropriate. All modules should have a short description.
"""

from .main import Book as Book
53 changes: 53 additions & 0 deletions .dagger/src/book/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import random

import dagger
from dagger import dag, function, object_type, Directory, Container


@object_type
class Book:
@function
def env(self, source: dagger.Directory) -> dagger.Container:
"""Returns a container with the Python environment and the source code mounted"""
return (
dag.container()
.from_("python:3.11")
.with_exec(["sh", "-c", "apt-get update && apt-get install -y libpq-dev"])
.with_directory("/app", source)
.with_workdir("/app")
.with_mounted_cache("/root/.cache/pip", dag.cache_volume("python-pip"))
.with_exec(["pip", "install", "-r", "requirements.txt"])
.with_exposed_port(8000)
.with_entrypoint(["fastapi", "run", "main.py", "--port", "8000"])
)

@function
async def test(self, source: dagger.Directory) -> str:
"""Runs the tests in the source code and returns the output"""
db = (
dag.container()
.from_("postgres:15-alpine")
.with_env_variable("POSTGRES_USER", "app_user")
.with_env_variable("POSTGRES_PASSWORD", "secret")
.with_file("/docker-entrypoint-initdb.d/init-dbs.sh", source.file("./init-dbs.sh"))
.with_exposed_port(5432)
.as_service(args=[], use_entrypoint=True)
)

return await (
self.env(source)
.with_service_binding("db", db)
.with_env_variable("DATABASE_URL", "postgresql://app_user:secret@db/app_db")
.with_env_variable("TEST_DATABASE_URL", "postgresql://app_user:secret@db/app_db_test")
.with_exec(["pytest"])
.stdout()
)

@function
async def publish(self, source: dagger.Directory) -> str:
"""Builds and publishes the application container image to a registry"""
await self.test(source)
return await (
self.env(source)
.publish(f"ttl.sh/my-fastapi-app-{random.randrange(10**8)}")
)
776 changes: 776 additions & 0 deletions .dagger/uv.lock

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions .github/workflows/dagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: dagger
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
dagger:
name: dagger
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Test
id: test
uses: dagger/dagger-for-github@8.0.0
with:
version: "0.19.2"
verb: call
args: test --source=.
cloud-token: ${{ secrets.DAGGER_CLOUD_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ curl http://localhost:8000/api/books/
## Run tests

```
docker exec -it fastapi_app bash
docker exec -it fastapi_app pytest
pytest # in the container shell
```
8 changes: 8 additions & 0 deletions dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "book",
"engineVersion": "v0.19.2",
"sdk": {
"source": "python"
},
"source": ".dagger"
}